Skip to content

Commit

Permalink
Extension for finding any element (dotnet#26657)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaflo authored Dec 16, 2024
1 parent 208d919 commit b615b73
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ public void Issue7167Test()
// App.Print.Tree();

App.ScrollDown(ListViewId, ScrollStrategy.Auto, 0.65, 200);
App.WaitForElement("23");
App.WaitForAnyElement(["20", "40", "60", "80"]);

// when adding additional items via a addrange and a CollectionChangedEventArgs.Action.Reset is sent
// then the listview shouldnt reset or it should not scroll to the top
App.Tap(AddRangeCommandId);

// Verify that item "23" is still visible
App.WaitForElement("23");
App.WaitForAnyElement(["20", "40", "60", "80"]);
}
}
#endif
29 changes: 29 additions & 0 deletions src/TestUtils/src/UITest.Appium/HelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,24 @@ public static IReadOnlyCollection<string> GetAlertText(this IUIElement alertElem
return results;
}

/// <summary>
/// Wait function that will repeatly query the app until any matching element is found.
/// Throws a TimeoutException if no element is found within the time limit.
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
/// <param name="marked">Collection of target Elements.</param>
/// <param name="timeoutMessage">The message used in the TimeoutException.</param>
/// <param name="timeout">The TimeSpan to wait before failing.</param>
/// <param name="retryFrequency">The TimeSpan to wait between each query call to the app.</param>
/// <param name="postTimeout">The final TimeSpan to wait after the element has been found.</param>
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)
{
IUIElement result() => FindAnyElement(app, marked);
var results = WaitForAtLeastOne(result, timeoutMessage, timeout, retryFrequency);

return results;
}

/// <summary>
/// Wait function that will repeatly query the app until a matching element is found.
/// Throws a TimeoutException if no element is found within the time limit.
Expand Down Expand Up @@ -2095,6 +2113,17 @@ static IUIElement FindElement(IApp app, string element)
return result;
}

static IUIElement FindAnyElement(IApp app, string[] elements)
{
foreach (var element in elements)
{
if (FindElement(app, element) is IUIElement result)
return result;
}

throw new InvalidOperationException($"Did not find any elements in the list: {string.Join(", ", elements)}");
}

static IReadOnlyCollection<IUIElement> FindElements(IApp app, string element)
{
var result = app.FindElements(element);
Expand Down

0 comments on commit b615b73

Please sign in to comment.