Skip to content

Commit

Permalink
Try simplified logic for handling location prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0thyj committed Jan 5, 2025
1 parent fa01460 commit 5102581
Showing 1 changed file with 10 additions and 41 deletions.
51 changes: 10 additions & 41 deletions test/SystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Support.Extensions;
using System.Windows.Automation;

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

HandleLocationPrompt();
Thread.Sleep(TimeSpan.FromSeconds(2));
if (HandleLocationPrompt()) Thread.Sleep(TimeSpan.FromSeconds(2));
driver.SwitchTo().Window(driver.WindowHandles[0]);
driver.TakeScreenshot().SaveAsFile(@"..\..\..\screenshot1.png", ScreenshotImageFormat.Png);
driver.FindElementByXPath("//Window[@Name='Configure Schedule']").Click();
driver.TakeScreenshot().SaveAsFile(@"..\..\..\screenshot2.png", ScreenshotImageFormat.Png);
driver.FindElementByAccessibilityId("radioButton1").Click();
driver.FindElementByAccessibilityId("locationBox").SendKeys("New York NY");
driver.TakeScreenshot().SaveAsFile(@"..\..\..\screenshot3.png", ScreenshotImageFormat.Png);
driver.FindElementByXPath("//Button[@Name='OK']").Click();
Thread.Sleep(TimeSpan.FromSeconds(2));
//driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.FindElementByXPath("//Button[@Name='Yes']").Click();
Thread.Sleep(TimeSpan.FromSeconds(5));

Expand All @@ -62,43 +56,18 @@ public void Dispose()
driver?.Quit();
}

private void HandleLocationPrompt()
private bool 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)
var dialogMatcher = new PropertyCondition(AutomationElement.NameProperty, "Let Windows and apps access your location?");
var buttonMatcher = new PropertyCondition(AutomationElement.NameProperty, "Yes");
AutomationElement dialog = AutomationElement.RootElement.FindFirst(TreeScope.Children, dialogMatcher);
if (dialog?.FindFirst(TreeScope.Descendants, buttonMatcher) is AutomationElement yesButton &&
yesButton.GetCurrentPattern(InvokePattern.Pattern) is InvokePattern invokePattern)
{
Console.WriteLine("Dialog not found.");
return;
invokePattern.Invoke();
return true;
}

// 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'.");
return false;
}

private string? GetWallpaperPath()
Expand Down

0 comments on commit 5102581

Please sign in to comment.