Skip to content

Commit

Permalink
Try another way to handle location prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0thyj committed Jan 5, 2025
1 parent 53a58c0 commit fa01460
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion test/SystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Support.Extensions;
using System.Windows.Automation;

namespace WinDynamicDesktop.Tests
{
Expand All @@ -29,7 +30,7 @@ public void ShouldUpdateWallpaper()
driver.FindElementByXPath("//Button[@Name='OK']").Click();
Thread.Sleep(TimeSpan.FromSeconds(5));

System.Windows.Forms.SendKeys.SendWait("{ENTER}");
HandleLocationPrompt();
Thread.Sleep(TimeSpan.FromSeconds(2));
driver.SwitchTo().Window(driver.WindowHandles[0]);
driver.TakeScreenshot().SaveAsFile(@"..\..\..\screenshot1.png", ScreenshotImageFormat.Png);
Expand Down Expand Up @@ -61,6 +62,45 @@ public void Dispose()
driver?.Quit();
}

private void HandleLocationPrompt()
{
// Wait for the dialog to appear
AutomationElement dialog = null;
for (int i = 0; i < 10; i++) // Retry for up to 10 seconds
{
dialog = AutomationElement.RootElement.FindFirst(
TreeScope.Children,
new PropertyCondition(AutomationElement.NameProperty, "Let Windows and apps access your location?")); // Use the actual name of the dialog
if (dialog != null)
break;

Thread.Sleep(1000); // Wait 1 second before retrying
}

if (dialog == null)
{
Console.WriteLine("Dialog not found.");
return;
}

// Find the "Yes" button
AutomationElement yesButton = dialog.FindFirst(
TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, "Yes")); // Use the actual name of the button

if (yesButton == null)
{
Console.WriteLine("Yes button not found.");
return;
}

// Invoke the "Yes" button
var invokePattern = yesButton.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
invokePattern?.Invoke();

Console.WriteLine("Clicked 'Yes'.");
}

private string? GetWallpaperPath()
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop"))
Expand Down

0 comments on commit fa01460

Please sign in to comment.