Fix Race condition in tests involving context menu (#1373)
* Fix Race condition in tests involving context menu * remove extraneous logging
This commit is contained in:
parent
d256fb6c19
commit
6d059aa126
@ -13,8 +13,8 @@ public class CalculatorResults
|
|||||||
private WindowsElement CalculatorAlwaysOnTopResults => this.session.TryFindElementByAccessibilityId("CalculatorAlwaysOnTopResults");
|
private WindowsElement CalculatorAlwaysOnTopResults => this.session.TryFindElementByAccessibilityId("CalculatorAlwaysOnTopResults");
|
||||||
private WindowsElement CalculatorResult => this.session.TryFindElementByAccessibilityId("CalculatorResults");
|
private WindowsElement CalculatorResult => this.session.TryFindElementByAccessibilityId("CalculatorResults");
|
||||||
private WindowsElement CalculatorExpression => this.session.TryFindElementByAccessibilityId("CalculatorExpression");
|
private WindowsElement CalculatorExpression => this.session.TryFindElementByAccessibilityId("CalculatorExpression");
|
||||||
private WindowsElement MenuItemCopy => this.session.TryFindElementByAccessibilityId("CopyMenuItem");
|
private WindowsElement MenuItemCopy => this.session.WaitForElementByAccessibilityId("CopyMenuItem");
|
||||||
private WindowsElement MenuItemPaste => this.session.TryFindElementByAccessibilityId("PasteMenuItem");
|
private WindowsElement MenuItemPaste => this.session.WaitForElementByAccessibilityId("PasteMenuItem");
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the text from the display control in AoT mode and removes the narrator text that is not displayed in the UI.
|
/// Gets the text from the display control in AoT mode and removes the narrator text that is not displayed in the UI.
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||||
// Licensed under the MIT License.
|
// Licensed under the MIT License.
|
||||||
|
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting.Logging;
|
||||||
using OpenQA.Selenium;
|
using OpenQA.Selenium;
|
||||||
using OpenQA.Selenium.Appium.Windows;
|
using OpenQA.Selenium.Appium.Windows;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace CalculatorUITestFramework
|
namespace CalculatorUITestFramework
|
||||||
{
|
{
|
||||||
@ -68,5 +71,43 @@ public static void SwitchToCurrentWindowHandle(this WindowsDriver<WindowsElement
|
|||||||
// process id (any entry of allWindowHandles)
|
// process id (any entry of allWindowHandles)
|
||||||
driver.SwitchTo().Window(allWindowHandles[0]);
|
driver.SwitchTo().Window(allWindowHandles[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Waits for an element to be created.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="driver">this</param>
|
||||||
|
/// <param name="id">the automation id</param>
|
||||||
|
/// <param name="timeout">optional timeout in ms</param>
|
||||||
|
/// <returns>the element with the matching automation id</returns>
|
||||||
|
public static WindowsElement WaitForElementByAccessibilityId(this WindowsDriver<WindowsElement> driver, string id, int timeout = 1000)
|
||||||
|
{
|
||||||
|
Stopwatch timer = new Stopwatch();
|
||||||
|
timer.Reset();
|
||||||
|
timer.Start();
|
||||||
|
while (timer.ElapsedMilliseconds < timeout)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = driver.TryFindElementByAccessibilityId(id);
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
catch(WebDriverException ex)
|
||||||
|
{
|
||||||
|
if (ex.Message.Contains("An element could not be located on the page using the given search parameters"))
|
||||||
|
{
|
||||||
|
Logger.LogMessage("Element not found. Waiting for 10ms in WaitForElementByAccessibilityId");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Thread.Sleep(10);
|
||||||
|
}
|
||||||
|
timer.Stop();
|
||||||
|
|
||||||
|
// one last attempt. Throws the not found exception if this fails
|
||||||
|
return driver.TryFindElementByAccessibilityId(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user