Skip to content

Commit

Permalink
Look up chrome buttons by a11y id.
Browse files Browse the repository at this point in the history
XPath is slower, and unnecessary here.
  • Loading branch information
grokys committed Mar 13, 2023
1 parent d46eb22 commit 2cce1d4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
18 changes: 10 additions & 8 deletions tests/Avalonia.IntegrationTests.Appium/ElementExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
namespace Avalonia.IntegrationTests.Appium
{
public record class WindowChrome(
AppiumWebElement Close,
AppiumWebElement Minimize,
AppiumWebElement Maximize);
AppiumWebElement? Close,
AppiumWebElement? Minimize,
AppiumWebElement? Maximize,
AppiumWebElement? FullScreen);

internal static class ElementExtensions
{
Expand All @@ -25,10 +26,11 @@ public static WindowChrome GetChromeButtons(this AppiumWebElement window)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
var closeButton = window.FindElementByXPath("//XCUIElementTypeButton[1]");
var fullscreenButton = window.FindElementByXPath("//XCUIElementTypeButton[2]");
var minimizeButton = window.FindElementByXPath("//XCUIElementTypeButton[3]");
return new(closeButton, minimizeButton, fullscreenButton);
var closeButton = window.FindElementsByAccessibilityId("_XCUI:CloseWindow").FirstOrDefault();
var fullscreenButton = window.FindElementsByAccessibilityId("_XCUI:FullScreenWindow").FirstOrDefault();
var minimizeButton = window.FindElementsByAccessibilityId("_XCUI:MinimizeWindow").FirstOrDefault();
var zoomButton = window.FindElementsByAccessibilityId("_XCUI:ZoomWindow").FirstOrDefault();
return new(closeButton, minimizeButton, zoomButton, fullscreenButton);
}

throw new NotSupportedException("GetChromeButtons not supported on this platform.");
Expand Down Expand Up @@ -143,7 +145,7 @@ public static IDisposable OpenWindowWithClick(this AppiumWebElement element)
var text = windows.Select(x => x.Text).ToList();
var newWindow = session.FindElements(By.XPath("/XCUIElementTypeApplication/XCUIElementTypeWindow"))
.First(x => x.Text == newWindowTitle);
var (close, _, _) = ((AppiumWebElement)newWindow).GetChromeButtons();
var close = ((AppiumWebElement)newWindow).GetChromeButtons().Close;
close!.Click();
Thread.Sleep(1000);
});
Expand Down
31 changes: 16 additions & 15 deletions tests/Avalonia.IntegrationTests.Appium/WindowTests_MacOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent_When_In_Fullscreen(
var mainWindow = GetWindow("MainWindow");
var buttons = mainWindow.GetChromeButtons();

buttons.Maximize.Click();
buttons.FullScreen.Click();

Thread.Sleep(500);

Expand Down Expand Up @@ -239,17 +239,18 @@ public void WindowOrder_Owned_Is_Correct_After_Closing_Window()
public void Parent_Window_Has_Disabled_ChromeButtons_When_Modal_Dialog_Shown()
{
var window = GetWindow("MainWindow");
var (closeButton, miniaturizeButton, zoomButton) = window.GetChromeButtons();
var windowChrome = window.GetChromeButtons();

Assert.True(closeButton.Enabled);
Assert.True(zoomButton.Enabled);
Assert.True(miniaturizeButton.Enabled);
Assert.True(windowChrome.Close.Enabled);
Assert.True(windowChrome.Maximize.Enabled);
Assert.True(windowChrome.Maximize.Enabled);
Assert.Null(windowChrome.FullScreen.Enabled);

using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner))
{
Assert.False(closeButton.Enabled);
Assert.False(zoomButton.Enabled);
Assert.False(miniaturizeButton.Enabled);
Assert.False(windowChrome.Close.Enabled);
Assert.False(windowChrome.Maximize.Enabled);
Assert.False(windowChrome.Minimize.Enabled);
}
}

Expand All @@ -259,11 +260,11 @@ public void Minimize_Button_Is_Disabled_On_Modal_Dialog()
using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner))
{
var secondaryWindow = GetWindow("SecondaryWindow");
var (closeButton, miniaturizeButton, zoomButton) = secondaryWindow.GetChromeButtons();
var windowChrome = secondaryWindow.GetChromeButtons();

Assert.True(closeButton.Enabled);
Assert.True(zoomButton.Enabled);
Assert.False(miniaturizeButton.Enabled);
Assert.True(windowChrome.Close.Enabled);
Assert.True(windowChrome.Maximize.Enabled);
Assert.False(windowChrome.Minimize.Enabled);
}
}

Expand All @@ -274,7 +275,7 @@ public void Minimize_Button_Disabled_Owned_Window(ShowWindowMode mode)
using (OpenWindow(new PixelSize(200, 100), mode, WindowStartupLocation.Manual))
{
var secondaryWindow = GetWindow("SecondaryWindow");
var (_, miniaturizeButton, _) = secondaryWindow.GetChromeButtons();
var miniaturizeButton = secondaryWindow.GetChromeButtons().Minimize;

Assert.False(miniaturizeButton.Enabled);
}
Expand All @@ -288,7 +289,7 @@ public void Minimize_Button_Minimizes_Window(ShowWindowMode mode)
using (OpenWindow(new PixelSize(200, 100), mode, WindowStartupLocation.Manual))
{
var secondaryWindow = GetWindow("SecondaryWindow");
var (_, miniaturizeButton, _) = secondaryWindow.GetChromeButtons();
var miniaturizeButton = secondaryWindow.GetChromeButtons().Minimize;

miniaturizeButton.Click();
Thread.Sleep(1000);
Expand Down Expand Up @@ -344,7 +345,7 @@ public void Window_Has_Disabled_Zoom_Button_When_CanResize_Is_False(ShowWindowMo
using (OpenWindow(null, mode, WindowStartupLocation.Manual, canResize: false))
{
var secondaryWindow = GetWindow("SecondaryWindow");
var (_, _, zoomButton) = secondaryWindow.GetChromeButtons();
var zoomButton = secondaryWindow.GetChromeButtons().Maximize;
Assert.False(zoomButton.Enabled);
}
}
Expand Down

0 comments on commit 2cce1d4

Please sign in to comment.