Skip to content

Commit b615b73

Browse files
authored
Extension for finding any element (#26657)
1 parent 208d919 commit b615b73

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/XFIssue/Issue7167.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ public void Issue7167Test()
3030
// App.Print.Tree();
3131

3232
App.ScrollDown(ListViewId, ScrollStrategy.Auto, 0.65, 200);
33-
App.WaitForElement("23");
34-
33+
App.WaitForAnyElement(["20", "40", "60", "80"]);
34+
3535
// when adding additional items via a addrange and a CollectionChangedEventArgs.Action.Reset is sent
3636
// then the listview shouldnt reset or it should not scroll to the top
3737
App.Tap(AddRangeCommandId);
3838

39-
// Verify that item "23" is still visible
40-
App.WaitForElement("23");
39+
App.WaitForAnyElement(["20", "40", "60", "80"]);
4140
}
4241
}
4342
#endif

src/TestUtils/src/UITest.Appium/HelperExtensions.cs

+29
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,24 @@ public static IReadOnlyCollection<string> GetAlertText(this IUIElement alertElem
651651
return results;
652652
}
653653

654+
/// <summary>
655+
/// Wait function that will repeatly query the app until any matching element is found.
656+
/// Throws a TimeoutException if no element is found within the time limit.
657+
/// </summary>
658+
/// <param name="app">Represents the main gateway to interact with an app.</param>
659+
/// <param name="marked">Collection of target Elements.</param>
660+
/// <param name="timeoutMessage">The message used in the TimeoutException.</param>
661+
/// <param name="timeout">The TimeSpan to wait before failing.</param>
662+
/// <param name="retryFrequency">The TimeSpan to wait between each query call to the app.</param>
663+
/// <param name="postTimeout">The final TimeSpan to wait after the element has been found.</param>
664+
public static IUIElement WaitForAnyElement(this IApp app, string[] marked, string timeoutMessage = "Timed out waiting for element...", TimeSpan? timeout = null, TimeSpan? retryFrequency = null, TimeSpan? postTimeout = null)
665+
{
666+
IUIElement result() => FindAnyElement(app, marked);
667+
var results = WaitForAtLeastOne(result, timeoutMessage, timeout, retryFrequency);
668+
669+
return results;
670+
}
671+
654672
/// <summary>
655673
/// Wait function that will repeatly query the app until a matching element is found.
656674
/// Throws a TimeoutException if no element is found within the time limit.
@@ -2095,6 +2113,17 @@ static IUIElement FindElement(IApp app, string element)
20952113
return result;
20962114
}
20972115

2116+
static IUIElement FindAnyElement(IApp app, string[] elements)
2117+
{
2118+
foreach (var element in elements)
2119+
{
2120+
if (FindElement(app, element) is IUIElement result)
2121+
return result;
2122+
}
2123+
2124+
throw new InvalidOperationException($"Did not find any elements in the list: {string.Join(", ", elements)}");
2125+
}
2126+
20982127
static IReadOnlyCollection<IUIElement> FindElements(IApp app, string element)
20992128
{
21002129
var result = app.FindElements(element);

0 commit comments

Comments
 (0)