Skip to content

Commit

Permalink
#2 - Added AppBarToggleButton element for Windows and Web
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmcroft committed May 31, 2021
1 parent cddf3b5 commit c7cd2c7
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Icon="Add"
Label="Add" />
<AppBarToggleButton
x:Name="SymbolIcon"
x:Uid="SampleAppBarToggleButton"
Margin="0,0,0,12"
AutomationProperties.AutomationId="SampleAppBarToggleButton"
Expand Down
62 changes: 62 additions & 0 deletions samples/UnoSampleAppTests/Tests/AppBarToggleButtonTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace UnoSampleAppTests.Tests
{
using System;
using System.Collections.Generic;
using System.IO;
using Legerity;
using Legerity.Uno;
using Legerity.Uno.Elements;
using Legerity.Uno.Extensions;
using Legerity.Web;
using Legerity.Windows;
using NUnit.Framework;
using Shouldly;

[TestFixtureSource(nameof(TestPlatformOptions))]
public class AppBarToggleButtonTests : BaseTestClass
{
public AppBarToggleButtonTests(AppManagerOptions options) : base(options)
{
}

static IEnumerable<AppManagerOptions> TestPlatformOptions => new List<AppManagerOptions>
{
new WebAppManagerOptions(
WebAppDriverType.Edge,
Path.Combine(Environment.CurrentDirectory, "Tools\\Edge"))
{
Maximize = true, Url = "http://localhost:49247", ImplicitWait = TimeSpan.FromSeconds(10)
},
new WindowsAppManagerOptions("com.madeapps.unosampleapp_7mzr475ysvhxg!App")
{
DriverUri = "http://127.0.0.1:4723", LaunchWinAppDriver = true, Maximize = true
}
};

[Test]
public void SetToggleSymbolIconButtonOn()
{
// Arrange
AppBarToggleButton toggle = UnoAppManager.App.FindElementByAutomationId("SampleAppBarToggleButton");

// Act
toggle.ToggleOn();

// Assert
toggle.IsOn.ShouldBeTrue();
}

[Test]
public void SetToggleSymbolIconButtonOff()
{
// Arrange
AppBarToggleButton toggle = UnoAppManager.App.FindElementByAutomationId("SampleAppBarToggleButton");

// Act
toggle.ToggleOff();

// Assert
toggle.IsOn.ShouldBeFalse();
}
}
}
93 changes: 93 additions & 0 deletions src/Legerity.Uno/Elements/AppBarToggleButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
namespace Legerity.Uno.Elements
{
using System;
using Legerity.Uno.Extensions;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.iOS;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;

/// <summary>
/// Defines a <see cref="RemoteWebElement"/> wrapper for the core AppBarToggleButton control.
/// </summary>
public class AppBarToggleButton : AppBarButton
{
private const string ToggleOffValue = "0";

private const string ToggleOnValue = "1";

/// <summary>
/// Initializes a new instance of the <see cref="AppBarToggleButton"/> class.
/// </summary>
/// <param name="element">
/// The <see cref="RemoteWebElement"/> reference.
/// </param>
public AppBarToggleButton(RemoteWebElement element)
: base(element)
{
}

/// <summary>
/// Gets a value indicating whether the toggle button is in the on position.
/// </summary>
public bool IsOn => this.DetermineIsOn();

/// <summary>
/// Allows conversion of a <see cref="RemoteWebElement"/> to the <see cref="AppBarToggleButton"/> without direct casting.
/// </summary>
/// <param name="element">
/// The <see cref="RemoteWebElement"/>.
/// </param>
/// <returns>
/// The <see cref="AppBarToggleButton"/>.
/// </returns>
public static implicit operator AppBarToggleButton(RemoteWebElement element)
{
return new AppBarToggleButton(element);
}

/// <summary>
/// Toggles the button on.
/// </summary>
public void ToggleOn()
{
if (this.IsOn)
{
return;
}

this.Click();
}

/// <summary>
/// Toggles the button off.
/// </summary>
public void ToggleOff()
{
if (!this.IsOn)
{
return;
}

this.Click();
}

private bool DetermineIsOn()
{
return this.Element switch
{
AndroidElement _ =>
throw new PlatformNotSupportedException(
"An implementation for Android has not been implemented yet."),
IOSElement _ =>
throw new PlatformNotSupportedException(
"An implementation for iOS has not been implemented yet."),
WindowsElement _ =>
this.Element.GetAttribute("Toggle.ToggleState") == ToggleOnValue,
_ =>
this.Element.FindElementByXamlName("CheckedHighlightBackground")
.GetCssValue("opacity") != ToggleOffValue
};
}
}
}
18 changes: 15 additions & 3 deletions src/Legerity.Uno/Extensions/RemoteWebDriverExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace Legerity.Uno.Extensions
{
using Legerity.Windows.Extensions;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.iOS;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;

Expand Down Expand Up @@ -29,7 +31,7 @@ public static RemoteWebElement FindWebElementByXamlType(this RemoteWebDriver dri
/// <summary>
/// Finds the first Uno Platform web element in the page that matches the given x:Name value.
/// <para>
/// To find elements with this method, set the following in your App.xaml.cs.
/// To find elements with this method for web applications, set the following in your App.xaml.cs.
/// <code>
/// Uno.UI.FeatureConfiguration.UIElement.AssignDOMXamlName = true;
/// </code>
Expand All @@ -38,9 +40,19 @@ public static RemoteWebElement FindWebElementByXamlType(this RemoteWebDriver dri
/// <param name="driver">The web application driver.</param>
/// <param name="name">The x:Name of the element to find.</param>
/// <returns>The <see cref="RemoteWebElement"/> if found.</returns>
public static RemoteWebElement FindWebElementByXamlName(this RemoteWebDriver driver, string name)
public static RemoteWebElement FindElementByXamlName(this RemoteWebDriver driver, string name)
{
return driver.FindElement(By.XPath($".//*[@xamlname='{name}']")) as RemoteWebElement;
return driver switch
{
IOSDriver<IOSElement> _ =>
driver.FindElement(By.Name(name)) as RemoteWebElement,
AndroidDriver<AndroidElement> _ =>
driver.FindElement(By.Name(name)) as RemoteWebElement,
WindowsDriver<WindowsElement> _ =>
driver.FindElement(By.Name(name)) as RemoteWebElement,
_ =>
driver.FindElement(By.XPath($".//*[@xamlname='{name}']")) as RemoteWebElement
};
}

/// <summary>
Expand Down
18 changes: 15 additions & 3 deletions src/Legerity.Uno/Extensions/RemoteWebElementExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace Legerity.Uno.Extensions
{
using Legerity.Windows.Extensions;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.iOS;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;

Expand Down Expand Up @@ -29,7 +31,7 @@ public static RemoteWebElement FindWebElementByXamlType(this RemoteWebElement el
/// <summary>
/// Finds the first Uno Platform web element under the given element that matches the given x:Name value.
/// <para>
/// To find elements with this method, set the following in your App.xaml.cs.
/// To find elements with this method for web applications, set the following in your App.xaml.cs.
/// <code>
/// Uno.UI.FeatureConfiguration.UIElement.AssignDOMXamlName = true;
/// </code>
Expand All @@ -38,9 +40,19 @@ public static RemoteWebElement FindWebElementByXamlType(this RemoteWebElement el
/// <param name="element">The web element.</param>
/// <param name="name">The x:Name of the element to find.</param>
/// <returns>The <see cref="RemoteWebElement"/> if found.</returns>
public static RemoteWebElement FindWebElementByXamlName(this RemoteWebElement element, string name)
public static RemoteWebElement FindElementByXamlName(this RemoteWebElement element, string name)
{
return element.FindElement(By.XPath($".//*[@xamlname='{name}']")) as RemoteWebElement;
return UnoAppManager.App switch
{
IOSDriver<IOSElement> _ =>
element.FindElement(By.Name(name)) as RemoteWebElement,
AndroidDriver<AndroidElement> _ =>
element.FindElement(By.Name(name)) as RemoteWebElement,
WindowsDriver<WindowsElement> _ =>
element.FindElement(By.Name(name)) as RemoteWebElement,
_ =>
element.FindElement(By.XPath($".//*[@xamlname='{name}']")) as RemoteWebElement
};
}

/// <summary>
Expand Down

0 comments on commit c7cd2c7

Please sign in to comment.