From 6d059aa12604bfa00070e2c2be96e675ade2f9bf Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 15 Sep 2020 11:08:22 -0700 Subject: [PATCH] Fix Race condition in tests involving context menu (#1373) * Fix Race condition in tests involving context menu * remove extraneous logging --- .../CalculatorResults.cs | 4 +- .../WindowsDriverExtensions.cs | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/CalculatorUITestFramework/CalculatorResults.cs b/src/CalculatorUITestFramework/CalculatorResults.cs index 9371fa1..6e6815d 100644 --- a/src/CalculatorUITestFramework/CalculatorResults.cs +++ b/src/CalculatorUITestFramework/CalculatorResults.cs @@ -13,8 +13,8 @@ public class CalculatorResults private WindowsElement CalculatorAlwaysOnTopResults => this.session.TryFindElementByAccessibilityId("CalculatorAlwaysOnTopResults"); private WindowsElement CalculatorResult => this.session.TryFindElementByAccessibilityId("CalculatorResults"); private WindowsElement CalculatorExpression => this.session.TryFindElementByAccessibilityId("CalculatorExpression"); - private WindowsElement MenuItemCopy => this.session.TryFindElementByAccessibilityId("CopyMenuItem"); - private WindowsElement MenuItemPaste => this.session.TryFindElementByAccessibilityId("PasteMenuItem"); + private WindowsElement MenuItemCopy => this.session.WaitForElementByAccessibilityId("CopyMenuItem"); + private WindowsElement MenuItemPaste => this.session.WaitForElementByAccessibilityId("PasteMenuItem"); /// /// Gets the text from the display control in AoT mode and removes the narrator text that is not displayed in the UI. diff --git a/src/CalculatorUITestFramework/WindowsDriverExtensions.cs b/src/CalculatorUITestFramework/WindowsDriverExtensions.cs index 399584a..af36507 100644 --- a/src/CalculatorUITestFramework/WindowsDriverExtensions.cs +++ b/src/CalculatorUITestFramework/WindowsDriverExtensions.cs @@ -1,8 +1,11 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using Microsoft.VisualStudio.TestTools.UnitTesting.Logging; using OpenQA.Selenium; using OpenQA.Selenium.Appium.Windows; +using System.Diagnostics; +using System.Threading; namespace CalculatorUITestFramework { @@ -68,5 +71,43 @@ public static void SwitchToCurrentWindowHandle(this WindowsDriver + /// Waits for an element to be created. + /// + /// this + /// the automation id + /// optional timeout in ms + /// the element with the matching automation id + public static WindowsElement WaitForElementByAccessibilityId(this WindowsDriver 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); + } } }