Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,23 @@ public void DragEvents()
App.WaitForElement("LabelDragElement");
App.DragAndDrop("LabelDragElement", "DragTarget");

App.WaitForElement("DragStartEventsLabel");
var textAfterDragStart = App.FindElement("DragStartEventsLabel").GetText();

if (string.IsNullOrEmpty(textAfterDragStart))
{
Assert.Fail("Text was expected: Drag start event");
}
else
{
Assert.That(textAfterDragStart, Is.EqualTo("DragStarting"));
}

App.WaitForElement("DragOverEventsLabel");
var textAfterDragOver = App.FindElement("DragOverEventsLabel").GetText();
if (string.IsNullOrEmpty(textAfterDragOver))
{
Assert.Fail("Text was expected: Drag over event");
}
else
{
Assert.That(textAfterDragOver, Is.EqualTo("DragOver"));
}

App.WaitForElement("DragCompletedEventsLabel");
var textAfterDragComplete = App.FindElement("DragCompletedEventsLabel").GetText();
if (string.IsNullOrEmpty(textAfterDragComplete))
{
Assert.Fail("Text was expected: Drag complete event");
}
else
{
Assert.That(textAfterDragComplete, Is.EqualTo("DropCompleted"));
}
AssertEventText("DragStartEventsLabel", "DragStarting");
AssertEventText("DragOverEventsLabel", "DragOver");
AssertEventText("DragCompletedEventsLabel", "DropCompleted");
AssertEventText("DropEventsLabel", "Drop");
}

App.WaitForElement("DropEventsLabel");
var textAfterDrop = App.FindElement("DropEventsLabel").GetText();
if (string.IsNullOrEmpty(textAfterDrop))
{
Assert.Fail("Text was expected: Drop event");
}
else
{
Assert.That(textAfterDrop, Is.EqualTo("Drop"));
}
void AssertEventText(string automationId, string expectedText)
Comment thread
kubaflo marked this conversation as resolved.
{
// Wait for the label's text to become EXACTLY the expected value. A substring wait is
// unreliable here because each label's placeholder (e.g. "DragOverEvents: ") already
// contains the expected event name (e.g. "DragOver"), so a Contains-based wait would pass
// immediately on the placeholder. The wait already asserts the exact text, so no separate
// GetText re-read is needed (it would only re-open a window for transient Appium flakiness).
Assert.That(
App.WaitForTextEqualToElement(automationId, expectedText),
Is.True,
$"Timed out waiting for {automationId} to become '{expectedText}'.");
}

[Test]
Expand Down
34 changes: 34 additions & 0 deletions src/TestUtils/src/UITest.Appium/HelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,40 @@ public static bool WaitForTextToBePresentInElement(this IApp app, string automat
}
}

/// <summary>
/// Waits until the element's text is exactly equal to <paramref name="text"/> (ordinal), rather
/// than merely containing it. Use this when the element's placeholder/initial text already
/// contains the expected value as a substring, which would make a Contains-based wait pass
/// prematurely on the placeholder.
/// </summary>
public static bool WaitForTextEqualToElement(this IApp app, string automationId, string text, TimeSpan? timeout = null)
Comment thread
kubaflo marked this conversation as resolved.
{
timeout ??= DefaultTimeout;
TimeSpan retryFrequency = TimeSpan.FromMilliseconds(500);

DateTime start = DateTime.Now;

while (true)
{
var element = app.FindElements(automationId).FirstOrDefault();

if (element is not null && element.TryGetText(out var s) && string.Equals(s, text, StringComparison.Ordinal))
Comment thread
kubaflo marked this conversation as resolved.
Outdated
{
return true;
}

long elapsed = DateTime.Now.Subtract(start).Ticks;
if (elapsed >= timeout.Value.Ticks)
{
Debug.WriteLine($">>>>> {elapsed} ticks elapsed, timeout value is {timeout.Value.Ticks}");

return false;
}

Task.Delay(retryFrequency).Wait();
}
}

/// <summary>
/// Repeatedly executes a query until it returns a non-empty value or the specified retry count is reached.
/// </summary>
Expand Down
Loading