Added Calculator Standard Mode UI Tests (#501)

- Added the CalculatorUIFramework to handle the WinAppDriver logic.
- Added Standard Mode smoke tests and BVTs to the CalculatorUITests project.
- Removed old UI tests that did not use the CalculatorUIFramework
This commit is contained in:
Stephanie Anderl
2019-06-21 14:54:36 -07:00
committed by Matt Cooley
parent e9551e3774
commit 2517854836
19 changed files with 1427 additions and 169 deletions

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Appium.WebDriver" Version="4.0.0.6-beta" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,55 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using System.Collections.ObjectModel;
namespace CalculatorUITestFramework
{
public class HistoryPanel
{
private WindowsDriver<WindowsElement> session => WinAppDriver.Instance.CalculatorSession;
public WindowsElement HistoryLabel => this.session.TryFindElementByAccessibilityId("HistoryLabel");
public WindowsElement HistoryListView => this.session.TryFindElementByAccessibilityId("HistoryListView");
public WindowsElement ClearHistoryButton => this.session.TryFindElementByAccessibilityId("ClearHistory");
public WindowsElement HistoryEmptyLabel => this.session.TryFindElementByAccessibilityId("HistoryEmpty");
/// <summary>
/// Gets all of the history items listed in the History Pane.
/// </summary>
/// <returns>A readonly collection of history items.</returns>
public ReadOnlyCollection<AppiumWebElement> GetAllHistoryListViewItems()
{
this.HistoryLabel.Click();
this.HistoryListView.WaitForDisplayed();
return this.HistoryListView.FindElementsByClassName("ListViewItem");
}
/// <summary>
/// Opens the History Pane and clicks the delete button if it is visible.
/// </summary>
public void ClearHistory()
{
this.HistoryLabel.Click();
try
{
this.ClearHistoryButton.Click();
}
catch(WebDriverException ex)
{
if (ex.Message.Contains("element could not be located"))
{
Assert.IsNotNull(this.HistoryEmptyLabel);
return;
}
throw;
}
}
}
}

View File

@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using System.Collections.ObjectModel;
namespace CalculatorUITestFramework
{
public class MemoryPanel
{
private WindowsDriver<WindowsElement> session => WinAppDriver.Instance.CalculatorSession;
public WindowsElement MemoryClear => this.session.TryFindElementByAccessibilityId("ClearMemoryButton");
public WindowsElement MemRecall => this.session.TryFindElementByAccessibilityId("MemRecall");
public WindowsElement MemPlus => this.session.TryFindElementByAccessibilityId("MemPlus");
public WindowsElement MemMinus => this.session.TryFindElementByAccessibilityId("MemMinus");
public WindowsElement MemButton => this.session.TryFindElementByAccessibilityId("memButton");
public WindowsElement MemoryPane => this.session.TryFindElementByAccessibilityId("MemoryPanel");
public WindowsElement MemoryLabel => this.session.TryFindElementByAccessibilityId("MemoryLabel");
public WindowsElement MemoryListView => this.session.TryFindElementByAccessibilityId("MemoryListView");
public WindowsElement MemoryPaneEmptyLabel => this.session.TryFindElementByAccessibilityId("MemoryPaneEmpty");
/// <summary>
/// Opens the Memory Pane by clicking the Memory pivot label.
/// </summary>
public void OpenMemoryPanel()
{
this.MemoryLabel.Click();
this.MemoryPane.WaitForDisplayed();
}
/// <summary>
/// Gets all of the memory items listed in the Memory Pane.
/// </summary>
/// <returns>A readonly collection of memory items.</returns>
public ReadOnlyCollection<AppiumWebElement> GetAllMemoryListViewItems()
{
OpenMemoryPanel();
return this.MemoryListView.FindElementsByClassName("ListViewItem");
}
}
}

View File

@@ -0,0 +1,105 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using OpenQA.Selenium.Appium.Windows;
using System;
namespace CalculatorUITestFramework
{
public enum CalculatorMode
{
StandardCalculator,
ScientificCalculator,
ProgrammerCalculator,
DateCalculator,
Currency,
Volume,
Length,
Weight,
Temperature,
Energy,
Area,
Speed,
Time,
Power,
Data,
Pressure,
Angle
}
public class NavigationMenu
{
private WindowsDriver<WindowsElement> session => WinAppDriver.Instance.CalculatorSession;
public WindowsElement NavigationMenuButton => this.session.TryFindElementByAccessibilityId("TogglePaneButton");
public WindowsElement NavigationMenuPane => this.session.TryFindElementByClassName("SplitViewPane");
/// <summary>
/// Changes the mode using the navigation menu in the UI
/// </summary>
/// <param name="mode">The mode to be changed to</param>
public void ChangeCalculatorMode(CalculatorMode mode)
{
string modeAccessibilityId;
switch (mode)
{
case CalculatorMode.StandardCalculator:
modeAccessibilityId = "Standard";
break;
case CalculatorMode.ScientificCalculator:
modeAccessibilityId = "Scientific";
break;
case CalculatorMode.ProgrammerCalculator:
modeAccessibilityId = "Programmer";
break;
case CalculatorMode.DateCalculator:
modeAccessibilityId = "Date";
break;
case CalculatorMode.Currency:
modeAccessibilityId = "Currency";
break;
case CalculatorMode.Volume:
modeAccessibilityId = "Volume";
break;
case CalculatorMode.Length:
modeAccessibilityId = "Length";
break;
case CalculatorMode.Weight:
modeAccessibilityId = "Weight";
break;
case CalculatorMode.Temperature:
modeAccessibilityId = "Temperature";
break;
case CalculatorMode.Energy:
modeAccessibilityId = "Energy";
break;
case CalculatorMode.Area:
modeAccessibilityId = "Area";
break;
case CalculatorMode.Speed:
modeAccessibilityId = "Speed";
break;
case CalculatorMode.Time:
modeAccessibilityId = "Time";
break;
case CalculatorMode.Power:
modeAccessibilityId = "Power";
break;
case CalculatorMode.Data:
modeAccessibilityId = "Data";
break;
case CalculatorMode.Pressure:
modeAccessibilityId = "Pressure";
break;
case CalculatorMode.Angle:
modeAccessibilityId = "Angle";
break;
default:
throw (new ArgumentException("The mode is not valid"));
}
this.NavigationMenuButton.Click();
this.NavigationMenuPane.WaitForDisplayed();
this.session.TryFindElementByAccessibilityId(modeAccessibilityId).Click();
}
}
}

View File

@@ -0,0 +1,82 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using OpenQA.Selenium.Appium.Windows;
using System;
namespace CalculatorUITestFramework
{
public class NumberPad
{
private WindowsDriver<WindowsElement> session => WinAppDriver.Instance.CalculatorSession;
public WindowsElement Num0Button => this.session.TryFindElementByAccessibilityId("num0Button");
public WindowsElement Num1Button => this.session.TryFindElementByAccessibilityId("num1Button");
public WindowsElement Num2Button => this.session.TryFindElementByAccessibilityId("num2Button");
public WindowsElement Num3Button => this.session.TryFindElementByAccessibilityId("num3Button");
public WindowsElement Num4Button => this.session.TryFindElementByAccessibilityId("num4Button");
public WindowsElement Num5Button => this.session.TryFindElementByAccessibilityId("num5Button");
public WindowsElement Num6Button => this.session.TryFindElementByAccessibilityId("num6Button");
public WindowsElement Num7Button => this.session.TryFindElementByAccessibilityId("num7Button");
public WindowsElement Num8Button => this.session.TryFindElementByAccessibilityId("num8Button");
public WindowsElement Num9Button => this.session.TryFindElementByAccessibilityId("num9Button");
public WindowsElement DecimalButton => this.session.TryFindElementByAccessibilityId("decimalSeparatorButton");
public WindowsElement NegateButton => this.session.TryFindElementByAccessibilityId("negateButton");
/// <summary>
/// Translates a number into the Calculator button clicks.
/// </summary>
/// <param name="number">Number to be entered into the calculator.</param>
public void Input(double number)
{
string numberStr = number.ToString();
if (numberStr.StartsWith("-"))
{
numberStr = numberStr.Substring(1) + "-";
}
foreach (char digit in numberStr)
{
switch (digit)
{
case '0':
this.Num0Button.Click();
break;
case '1':
this.Num1Button.Click();
break;
case '2':
this.Num2Button.Click();
break;
case '3':
this.Num3Button.Click();
break;
case '4':
this.Num4Button.Click();
break;
case '5':
this.Num5Button.Click();
break;
case '6':
this.Num6Button.Click();
break;
case '7':
this.Num7Button.Click();
break;
case '8':
this.Num8Button.Click();
break;
case '9':
this.Num9Button.Click();
break;
case '.':
this.DecimalButton.Click();
break;
case '-':
this.NegateButton.Click();
break;
default:
throw (new ArgumentException(String.Format("{0} is not valid", digit)));
}
}
}
}
}

View File

@@ -0,0 +1,49 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium.Windows;
namespace CalculatorUITestFramework
{
/// <summary>
/// This class contains the UI automation objects and helper methods available when the Calculator is in Standard Mode.
/// </summary>
public class StandardCalculatorPage
{
private WindowsDriver<WindowsElement> session => WinAppDriver.Instance.CalculatorSession;
public StandardOperatorsPanel StandardOperators = new StandardOperatorsPanel();
public MemoryPanel MemoryPanel = new MemoryPanel();
public HistoryPanel HistoryPanel = new HistoryPanel();
public NavigationMenu NavigationMenu = new NavigationMenu();
public WindowsElement Header => this.session.TryFindElementByAccessibilityId("Header");
public WindowsElement CalculatorResult => this.session.TryFindElementByAccessibilityId("CalculatorResults");
public void NavigateToStandardCalculator()
{
// Ensure that calculator is in standard mode
this.NavigationMenu.ChangeCalculatorMode(CalculatorMode.StandardCalculator);
Assert.IsNotNull(CalculatorResult);
}
/// <summary>
/// Clear the Calculator display, Memory Panel and optionally the History Panel
/// </summary>
public void ClearAll()
{
this.StandardOperators.ClearButton.Click();
this.MemoryPanel.MemoryClear.Click();
this.HistoryPanel.ClearHistory();
}
/// <summary>
/// Gets the text from the display control and removes the narrator text that is not displayed in the UI.
/// </summary>
/// <returns>The string shown in the UI.</returns>
public string GetCalculatorResultText()
{
return this.CalculatorResult.Text.Replace("Display is", string.Empty).Trim();
}
}
}

View File

@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using OpenQA.Selenium.Appium.Windows;
using System.Collections.Generic;
namespace CalculatorUITestFramework
{
/// <summary>
/// UI elements and helper methods to perform common mathematical standard operations.
/// </summary>
public class StandardOperatorsPanel
{
private WindowsDriver<WindowsElement> session => WinAppDriver.Instance.CalculatorSession;
public NumberPad NumberPad = new NumberPad();
public WindowsElement PercentButton => this.session.TryFindElementByAccessibilityId("percentButton");
public WindowsElement SquareRootButton => this.session.TryFindElementByAccessibilityId("squareRootButton");
public WindowsElement XPower2Button => this.session.TryFindElementByAccessibilityId("xpower2Button");
public WindowsElement XPower3Button => this.session.TryFindElementByAccessibilityId("xpower3Button");
public WindowsElement InvertButton => this.session.TryFindElementByAccessibilityId("invertButton");
public WindowsElement DivideButton => this.session.TryFindElementByAccessibilityId("divideButton");
public WindowsElement MultiplyButton => this.session.TryFindElementByAccessibilityId("multiplyButton");
public WindowsElement MinusButton => this.session.TryFindElementByAccessibilityId("minusButton");
public WindowsElement PlusButton => this.session.TryFindElementByAccessibilityId("plusButton");
public WindowsElement EqualButton => this.session.TryFindElementByAccessibilityId("equalButton");
public WindowsElement ClearEntryButton => this.session.TryFindElementByAccessibilityId("clearEntryButton");
public WindowsElement ClearButton => this.session.TryFindElementByAccessibilityId("clearButton");
public WindowsElement BackSpaceButton => this.session.TryFindElementByAccessibilityId("backSpaceButton");
}
}

View File

@@ -0,0 +1,84 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using System;
using System.Diagnostics;
namespace CalculatorUITestFramework
{
public sealed class WinAppDriver
{
private WindowsDriverLocalService windowsDriverService = null;
private const string calculatorAppId = "Microsoft.WindowsCalculator.Dev_8wekyb3d8bbwe!App";
private static WinAppDriver instance = null;
public static WinAppDriver Instance
{
get
{
if (instance == null)
{
instance = new WinAppDriver();
}
return instance;
}
}
public WindowsDriver<WindowsElement> CalculatorSession { get; private set; }
private WinAppDriver()
{
}
public void SetupCalculatorSession(TestContext context)
{
this.windowsDriverService = new WindowsDriverServiceBuilder().Build();
this.windowsDriverService.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
var outputData = e.Data?.Replace("\0", string.Empty);
if (!String.IsNullOrEmpty(outputData))
{
Console.WriteLine(outputData);
}
});
this.windowsDriverService.Start();
// Launch Calculator application if it is not yet launched
if (this.CalculatorSession == null)
{
// Create a new WinAppDriver session to bring up an instance of the Calculator application
// Note: Multiple calculator windows (instances) share the same process Id
var options = new AppiumOptions();
options.AddAdditionalCapability("app", calculatorAppId);
options.AddAdditionalCapability("deviceName", "WindowsPC");
this.CalculatorSession = new WindowsDriver<WindowsElement>(this.windowsDriverService.ServiceUrl, options);
this.CalculatorSession.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
Assert.IsNotNull(this.CalculatorSession);
}
}
public void TearDownCalculatorSession()
{
// Close the application and delete the session
if (this.CalculatorSession != null)
{
this.CalculatorSession.Quit();
this.CalculatorSession = null;
}
if (this.windowsDriverService != null)
{
this.windowsDriverService.Dispose();
this.windowsDriverService = null;
}
}
}
}

View File

@@ -0,0 +1,72 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using OpenQA.Selenium;
using OpenQA.Selenium.Appium.Windows;
namespace CalculatorUITestFramework
{
public static class WindowsDriverExtensions
{
/// <summary>
/// Wraps the WindowsDriver.FindElementByAccessibilityId and adds retry logic for when the element cannot be found due to WinAppDriver losing the window.
/// If FindElementByAccessibilityId fails for a different reason rethrow the error.
/// </summary>
public static WindowsElement TryFindElementByAccessibilityId(this WindowsDriver<WindowsElement> driver, string id)
{
try
{
return driver.FindElementByAccessibilityId(id);
}
catch (WebDriverException ex)
{
if (ex.Message.Contains("Currently selected window has been closed"))
{
driver.SwitchToCurrentWindowHandle();
return driver.FindElementByAccessibilityId(id);
}
throw;
}
}
/// <summary>
/// Wraps the WindowsDriver.FindElementByClassName and adds retry logic for when the element cannot be found due to WinAppDriver losing the window.
/// If FindElementByAccessibilityId fails for a different reason rethrow the error.
/// </summary>
public static WindowsElement TryFindElementByClassName(this WindowsDriver<WindowsElement> driver, string name)
{
try
{
return driver.FindElementByClassName(name);
}
catch (WebDriverException ex)
{
if (ex.Message.Contains("Currently selected window has been closed"))
{
driver.SwitchToCurrentWindowHandle();
return driver.FindElementByClassName(name);
}
throw;
}
}
/// <summary>
/// Gets the window handles for the current CalculatorSession and switches to the first one.
/// </summary>
public static void SwitchToCurrentWindowHandle(this WindowsDriver<WindowsElement> driver)
{
// Identify the current window handle. You can check through inspect.exe which window this is.
var currentWindowHandle = driver.CurrentWindowHandle;
// Return all window handles associated with this process/application.
// At this point hopefully you have one to pick from. Otherwise you can
// simply iterate through them to identify the one you want.
var allWindowHandles = driver.WindowHandles;
// Assuming you only have only one window entry in allWindowHandles and it is in fact the correct one,
// switch the session to that window as follows. You can repeat this logic with any top window with the same
// process id (any entry of allWindowHandles)
driver.SwitchTo().Window(allWindowHandles[0]);
}
}
}

View File

@@ -0,0 +1,191 @@
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//See the NOTICE file distributed with this work for additional
//information regarding copyright ownership.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//Portions Copyright(c) Microsoft Corporation
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Runtime.CompilerServices;
namespace CalculatorUITestFramework
{
public class WindowsDriverLocalService : IDisposable
{
private FileInfo FileName;
private string Arguments;
private IPAddress IP;
private int Port;
private TimeSpan InitializationTimeout;
private Process Service;
public event DataReceivedEventHandler OutputDataReceived;
internal WindowsDriverLocalService(
FileInfo fileName,
string arguments,
IPAddress ip,
int port,
TimeSpan initializationTimeout)
{
this.FileName = fileName;
this.Arguments = arguments;
this.IP = ip;
this.Port = port;
this.InitializationTimeout = initializationTimeout;
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void Start()
{
if (this.IsRunning)
{
return;
}
this.Service = new Process();
this.Service.StartInfo.FileName = FileName.FullName;
this.Service.StartInfo.Arguments = Arguments;
this.Service.StartInfo.UseShellExecute = false;
this.Service.StartInfo.CreateNoWindow = true;
this.Service.StartInfo.RedirectStandardOutput = true;
this.Service.OutputDataReceived += (sender, e) => OutputDataReceived?.Invoke(this, e);
bool isLaunched = false;
string msgTxt =
$"The local WinAppDriver server has not been started: {this.FileName.FullName} Arguments: {this.Arguments}. " +
"\n";
try
{
Service.Start();
Service.BeginOutputReadLine();
}
catch (Exception e)
{
DestroyProcess();
throw new Exception(msgTxt, e);
}
isLaunched = Ping();
if (!isLaunched)
{
DestroyProcess();
throw new Exception(
msgTxt +
$"Time {InitializationTimeout.TotalMilliseconds} ms for the service starting has been expired!");
}
}
public bool IsRunning
{
get
{
if (this.Service == null)
{
return false;
}
try
{
var pid = this.Service.Id;
}
catch (Exception)
{
return false;
}
return Ping();
}
}
public void Dispose()
{
DestroyProcess();
GC.SuppressFinalize(this);
}
public Uri ServiceUrl
{
// Note: append /wd/hub to the URL if you're directing the test at Appium
get { return new Uri($"http://{this.IP.ToString()}:{Convert.ToString(this.Port)}"); }
}
private void DestroyProcess()
{
if (this.Service == null)
{
return;
}
try
{
this.Service.Kill();
}
catch (Exception)
{
}
finally
{
this.Service.Close();
}
}
private bool Ping()
{
bool pinged = false;
Uri status;
Uri service = this.ServiceUrl;
if (service.IsLoopback)
{
status = new Uri("http://localhost:" + Convert.ToString(this.Port) + "/status");
}
else
{
status = new Uri(service.ToString() + "/status");
}
DateTime endTime = DateTime.Now.Add(this.InitializationTimeout);
while (!pinged & DateTime.Now < endTime)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(status);
HttpWebResponse response = null;
try
{
using (response = (HttpWebResponse)request.GetResponse())
{
pinged = true;
}
}
catch (Exception)
{
pinged = false;
}
finally
{
if (response != null)
{
response.Close();
}
}
}
return pinged;
}
}
}

View File

@@ -0,0 +1,103 @@
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//See the NOTICE file distributed with this work for additional
//information regarding copyright ownership.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//Portions Copyright(c) Microsoft Corporation
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace CalculatorUITestFramework
{
public class WindowsDriverServiceBuilder
{
private string IpAddress = "127.0.0.1";
private int Port = 4723;
private TimeSpan StartUpTimeout = new TimeSpan(0, 2, 0);
private FileInfo FileInfo;
public WindowsDriverLocalService Build()
{
if (this.FileInfo == null)
{
this.FileInfo = new FileInfo(@"c:\Program Files (x86)\Windows Application Driver\winappdriver.exe");
}
return new WindowsDriverLocalService(this.FileInfo, string.Empty, IPAddress.Parse(this.IpAddress), this.Port, this.StartUpTimeout);
}
public WindowsDriverServiceBuilder WithFileInfo(FileInfo fileInfo)
{
if (fileInfo == null)
{
throw new ArgumentNullException("FileInfo should not be NULL");
}
this.FileInfo = fileInfo;
return this;
}
public WindowsDriverServiceBuilder WithStartUpTimeOut(TimeSpan startUpTimeout)
{
if (startUpTimeout == null)
{
throw new ArgumentNullException("A startup timeout should not be NULL");
}
this.StartUpTimeout = startUpTimeout;
return this;
}
public WindowsDriverServiceBuilder WithIPAddress(string ipAddress)
{
this.IpAddress = ipAddress;
return this;
}
public WindowsDriverServiceBuilder UsingPort(int port)
{
if (port < 0)
{
throw new ArgumentException("The port parameter should not be negative");
}
if (port == 0)
{
return UsingAnyFreePort();
}
this.Port = port;
return this;
}
public WindowsDriverServiceBuilder UsingAnyFreePort()
{
Socket sock = null;
try
{
sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
sock.Bind(new IPEndPoint(IPAddress.Any, 0));
this.Port = ((IPEndPoint)sock.LocalEndPoint).Port;
return this;
}
finally
{
if (sock != null)
{
sock.Dispose();
}
}
}
}
}

View File

@@ -0,0 +1,39 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Logging;
using OpenQA.Selenium.Appium.Windows;
using System;
using System.Diagnostics;
using System.Threading;
namespace CalculatorUITestFramework
{
public static class WindowsElementExtensions
{
/// <summary>
/// Waits for an element to be displayed until the timeout is reached.
/// </summary>
/// <param name="element">WindowsElement in the Calculator application.</param>
/// <param name="timeout">Timeout in ms.</param>
public static void WaitForDisplayed(this WindowsElement element, int timeout = 2000)
{
Stopwatch timer = new Stopwatch();
timer.Reset();
timer.Start();
while (timer.ElapsedMilliseconds < timeout)
{
if (element.Displayed)
{
timer.Stop();
return;
}
Logger.LogMessage("Waiting for 10ms in WaitForDisplayed");
Thread.Sleep(10);
}
timer.Stop();
Assert.Fail(String.Format("{0} was not displayed in {1} ms", element, timeout));
}
}
}