// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium.Windows;
[assembly: InternalsVisibleTo("Element")]
[assembly: InternalsVisibleTo("Session")]
namespace Microsoft.PowerToys.UITest
{
///
/// Helper class for finding elements.
///
internal static class FindHelper
{
public static ReadOnlyCollection? FindAll(Func> findElementsFunc, WindowsDriver? driver, int timeoutMS)
where T : Element, new()
{
var items = FindElementsWithRetry(findElementsFunc, timeoutMS);
var res = items.Select(item =>
{
var element = item as WindowsElement;
return NewElement(element, driver, timeoutMS);
}).Where(item => item.IsMatchingTarget()).ToList();
return new ReadOnlyCollection(res);
}
private static ReadOnlyCollection FindElementsWithRetry(Func> findElementsFunc, int timeoutMS)
{
var timeout = TimeSpan.FromMilliseconds(timeoutMS);
var retryIntervalMS = TimeSpan.FromMilliseconds(500);
DateTime startTime = DateTime.Now;
while (DateTime.Now - startTime < timeout)
{
try
{
var items = findElementsFunc();
if (items.Count > 0)
{
return new ReadOnlyCollection((IList)items);
}
Task.Delay(retryIntervalMS).Wait();
}
catch (Exception)
{
}
}
return new ReadOnlyCollection(new List());
}
public static T NewElement(WindowsElement? element, WindowsDriver? driver, int timeoutMS)
where T : Element, new()
{
Assert.IsNotNull(driver, $"New Element {typeof(T).Name} error: driver is null.");
Assert.IsNotNull(element, $"New Element {typeof(T).Name} error: element is null.");
T newElement = new T();
newElement.SetSession(driver);
newElement.SetWindowsElement(element);
return newElement;
}
}
}