Skip to content

Commit

Permalink
Avoid stale elements in few more flaky tests
Browse files Browse the repository at this point in the history
  • Loading branch information
exyi committed Nov 2, 2022
1 parent cf53748 commit 9aafca0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
22 changes: 13 additions & 9 deletions src/Samples/Tests/Tests/Feature/ApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,28 @@ public void Feature_Api_GetCollection()
}, 30000, "Cannot find CompanyID = 11. Probably data are not loaded. (The page did not load in 5s.)");

// ensure that orders have been loaded
var orders = browser.FindElements(".id-order");
AssertUI.Any(orders).Attribute("data-order-id", "6");
WaitForExecutor.WaitFor(() => {
AssertUI.Any(browser.FindElements(".id-order"), waitForOptions: WaitForOptions.Disabled).Attribute("data-order-id", "6");
});

var idToDelete = orders[2].GetAttribute("data-order-id"); // every order has two elements (read-only and edit)
var idToDelete = browser.FindElements(".id-order")[2].GetAttribute("data-order-id"); // every order has two elements (read-only and edit)

// delete order (ID = 7)
browser.First($".id-order[data-order-id='{idToDelete}'] input[type=button][value=Delete]").Click();
orders = browser.FindElements(".id-order");
AssertUI.Any(orders).Attribute("data-order-id", "6");
AssertUI.All(orders).Attribute("data-order-id", s => s != idToDelete);
WaitForExecutor.WaitFor(() => {
AssertUI.Any(browser.FindElements(".id-order"), WaitForOptions.Disabled).Attribute("data-order-id", "6");
AssertUI.All(browser.FindElements(".id-order"), WaitForOptions.Disabled).Attribute("data-order-id", s => s != idToDelete);
});

// click the second button (ID = 12)
browser.First(".id-company[data-company-id='12'] input[type=button]").Click();


// ensure that orders have been loaded
orders = browser.FindElements(".id-order");
AssertUI.Any(orders).Attribute("data-order-id", "2");
AssertUI.Any(orders).Attribute("data-order-id", "9");
WaitForExecutor.WaitFor(() => {
AssertUI.Any(browser.FindElements(".id-order"), WaitForOptions.Disabled).Attribute("data-order-id", "2");
AssertUI.Any(browser.FindElements(".id-order"), WaitForOptions.Disabled).Attribute("data-order-id", "9");
});

// edit order (ID = 2)
browser.First(".id-order[data-order-id='2'] input[type=button][value=Edit]").Click();
Expand Down
8 changes: 6 additions & 2 deletions src/Samples/Tests/Tests/Feature/DictionaryTranslationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Riganti.Selenium.DotVVM;
using Xunit;
using Xunit.Abstractions;
using static DotVVM.Samples.Tests.UITestUtils;

namespace DotVVM.Samples.Tests.Feature
{
Expand All @@ -24,8 +25,11 @@ public void Feature_DictionaryTranslation_Clear()
inputs.ElementAt(4).Click();

WaitForExecutor.WaitFor(() => {
AssertUI.Text(browser.ElementAt("span", 0), s => !s.Contains("KEY: "), waitForOptions: WaitForOptions.Disabled);
AssertUI.Text(browser.ElementAt("span", 1), s => !s.Contains("VAL: "), waitForOptions: WaitForOptions.Disabled);
StaleElementRetry(() => {
AssertUI.Text(browser.ElementAt("span", 0), s => !s.Contains("KEY: "), waitForOptions: WaitForOptions.Disabled);
AssertUI.Text(browser.ElementAt("span", 1), s => !s.Contains("VAL: "), waitForOptions: WaitForOptions.Disabled);
});

}, options: WaitForOptions.LongerTimeout);

});
Expand Down
24 changes: 24 additions & 0 deletions src/Samples/Tests/Tests/UITestUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using OpenQA.Selenium;

namespace DotVVM.Samples.Tests;
public static class UITestUtils
{

public static T StaleElementRetry<T>(Func<T> action, int attempts = 5)
{
if (attempts <= 0)
return action();

try
{
return action();
}
catch (StaleElementReferenceException)
{
return StaleElementRetry<T>(action, attempts - 1);
}
}
public static void StaleElementRetry(Action action, int attempts = 5) =>
StaleElementRetry(() => { action(); return 0; }, attempts);
}

0 comments on commit 9aafca0

Please sign in to comment.