Skip to content

Commit

Permalink
Integration tests: add retries to OpenWindowWithClick (#17259)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrJul authored Oct 22, 2024
1 parent 63a4eaf commit b300ef6
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions tests/Avalonia.IntegrationTests.Appium/ElementExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,25 @@ public static IDisposable OpenWindowWithClick(this AppiumWebElement element, Tim
if (delay is not null)
Thread.Sleep((int)delay.Value.TotalMilliseconds);

var newHandle = session.WindowHandles.Except(oldHandles).SingleOrDefault();
string? newHandle = null;
IWebElement? newChildWindow = null;

for (var i = 0; i < 10; ++i)
{
newHandle = session.WindowHandles.Except(oldHandles).SingleOrDefault();
if (newHandle is not null)
break;

// If a new window handle hasn't been added to the session then it's likely
// that a child window was opened. These don't appear in session.WindowHandles
// so we have to use an XPath query to get hold of it.
var newChildWindows = session.FindElements(By.XPath("//Window"));
newChildWindow = newChildWindows.Except(oldChildWindows).SingleOrDefault();
if (newChildWindow is not null)
break;

Thread.Sleep(100);
}

if (newHandle is not null)
{
Expand All @@ -164,20 +182,17 @@ public static IDisposable OpenWindowWithClick(this AppiumWebElement element, Tim
session.SwitchTo().Window(oldHandle);
});
}
else
{
// If a new window handle hasn't been added to the session then it's likely
// that a child window was opened. These don't appear in session.WindowHandles
// so we have to use an XPath query to get hold of it.
var newChildWindows = session.FindElements(By.XPath("//Window"));
var pageSource = session.PageSource;
var childWindow = Assert.Single(newChildWindows.Except(oldChildWindows));

if (newChildWindow is not null)
{
return Disposable.Create(() =>
{
childWindow.SendKeys(Keys.Alt + Keys.F4 + Keys.Alt);
newChildWindow.SendKeys(Keys.Alt + Keys.F4 + Keys.Alt);
});
}

Assert.Fail("Could not find the newly opened window");
return Disposable.Empty;
}
else
{
Expand Down

0 comments on commit b300ef6

Please sign in to comment.