Skip to content

Commit 5d58631

Browse files
Tolerate another way that stale elements can be reported
1 parent 77e6dad commit 5d58631

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using OpenQA.Selenium;
5+
6+
namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
7+
8+
internal static class WebDriverStaleElementAssertion
9+
{
10+
public static void AssertThrowsDueToElementRemovedFromPage<T>(Func<T> callback)
11+
=> AssertThrowsDueToElementRemovedFromPage(() => callback());
12+
13+
public static void AssertThrowsDueToElementRemovedFromPage(Action callback)
14+
{
15+
var ex = Assert.Throws<Exception>(callback);
16+
if (!ExceptionMeansElementWasRemoved(ex))
17+
{
18+
throw ex;
19+
}
20+
}
21+
22+
public static bool ExceptionMeansElementWasRemoved(Exception ex)
23+
{
24+
if (ex is StaleElementReferenceException)
25+
{
26+
// This is the normal exception that occurs if you accessed something
27+
// that was already removed from the page
28+
return true;
29+
}
30+
else if (ex is WebDriverException)
31+
{
32+
// Sometimes we get this exception instead if the element is stale
33+
// It may depend on timing
34+
if (ex.Message.Contains("Node with given id does not belong to the document", StringComparison.Ordinal))
35+
{
36+
return true;
37+
}
38+
}
39+
40+
return false;
41+
}
42+
}

src/Components/test/E2ETest/ServerRenderingTests/EnhancedNavigationTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void EnhancedNavCanBeDisabledHierarchically()
9797

9898
// Check we got there, but we did *not* retain the <h1> element
9999
Browser.Equal("Other", () => Browser.Exists(By.TagName("h1")).Text);
100-
Assert.Throws<StaleElementReferenceException>(() => originalH1Elem.Text);
100+
WebDriverStaleElementAssertion.AssertThrowsDueToElementRemovedFromPage(() => originalH1Elem.Text);
101101
}
102102

103103
[Fact]
@@ -142,7 +142,7 @@ public void EnhancedNavCanBeDisabledInSVGElementContainingAnchor()
142142

143143
// Check we got there, but we did *not* retain the <h1> element
144144
Browser.Equal("Other", () => Browser.Exists(By.TagName("h1")).Text);
145-
Assert.Throws<StaleElementReferenceException>(() => originalH1Elem.Text);
145+
WebDriverStaleElementAssertion.AssertThrowsDueToElementRemovedFromPage(() => originalH1Elem.Text);
146146
}
147147

148148
[Fact]
@@ -157,7 +157,7 @@ public void EnhancedNavCanBeDisabledInSVGElementInsideAnchor()
157157

158158
// Check we got there, but we did *not* retain the <h1> element
159159
Browser.Equal("Other", () => Browser.Exists(By.TagName("h1")).Text);
160-
Assert.Throws<StaleElementReferenceException>(() => originalH1Elem.Text);
160+
WebDriverStaleElementAssertion.AssertThrowsDueToElementRemovedFromPage(() => originalH1Elem.Text);
161161
}
162162

163163
[Fact]
@@ -653,7 +653,7 @@ private static bool IsElementStale(IWebElement element)
653653
_ = element.Enabled;
654654
return false;
655655
}
656-
catch (StaleElementReferenceException)
656+
catch (Exception ex) when (WebDriverStaleElementAssertion.ExceptionMeansElementWasRemoved(ex))
657657
{
658658
return true;
659659
}

src/Components/test/E2ETest/ServerRenderingTests/FormHandlingTests/FormWithParentBindingContextTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,8 @@ private void DispatchToFormCore(DispatchToForm dispatch)
15921592
if (!dispatch.FormIsEnhanced)
15931593
{
15941594
// Verify the same form element is *not* still in the page
1595-
Assert.Throws<StaleElementReferenceException>(() => form.GetAttribute("method"));
1595+
WebDriverStaleElementAssertion.AssertThrowsDueToElementRemovedFromPage(
1596+
() => form.GetAttribute("method"));
15961597
}
15971598
else if (!dispatch.SuppressEnhancedNavigation)
15981599
{

src/Components/test/E2ETest/ServerRenderingTests/RedirectionTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ private void AssertElementRemoved(IWebElement element)
229229
{
230230
element.GetDomProperty("tagName");
231231
}
232-
catch (StaleElementReferenceException)
232+
catch (Exception ex) when (WebDriverStaleElementAssertion.ExceptionMeansElementWasRemoved(ex))
233233
{
234234
return true;
235235
}

src/Components/test/E2ETest/Tests/RoutingTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ public void CanNavigateProgrammaticallyWithForceLoad()
433433
Browser.True(() => Browser.Url.EndsWith("/Other", StringComparison.Ordinal));
434434

435435
// Because this was a full-page load, our element references should no longer be valid
436-
Assert.Throws<StaleElementReferenceException>(() =>
436+
WebDriverStaleElementAssertion.AssertThrowsDueToElementRemovedFromPage(() =>
437437
{
438438
testSelector.SelectedOption.GetAttribute("value");
439439
});
@@ -470,7 +470,7 @@ public void CanNavigateProgrammaticallyWithStateValidateNoReplaceHistoryEntry()
470470
Browser.DoesNotExist(By.Id("test-state"));
471471

472472
// We check if we had a force load
473-
Assert.Throws<StaleElementReferenceException>(() =>
473+
WebDriverStaleElementAssertion.AssertThrowsDueToElementRemovedFromPage(() =>
474474
testSelector.SelectedOption.GetAttribute("value"));
475475

476476
// But still we should be able to navigate back, and end up at the "/ProgrammaticNavigationCases" page
@@ -549,7 +549,7 @@ public void CanNavigateProgrammaticallyValidateNoReplaceHistoryEntry()
549549
Browser.True(() => Browser.Url.EndsWith("/Other", StringComparison.Ordinal));
550550

551551
// We check if we had a force load
552-
Assert.Throws<StaleElementReferenceException>(() =>
552+
WebDriverStaleElementAssertion.AssertThrowsDueToElementRemovedFromPage(() =>
553553
testSelector.SelectedOption.GetAttribute("value"));
554554

555555
// But still we should be able to navigate back, and end up at the "/ProgrammaticNavigationCases" page
@@ -602,7 +602,7 @@ public void CanNavigateProgrammaticallyWithForceLoadAndReplaceHistoryEntry()
602602
Browser.True(() => Browser.Url.EndsWith("/Other", StringComparison.Ordinal));
603603

604604
// We check if we had a force load
605-
Assert.Throws<StaleElementReferenceException>(() =>
605+
WebDriverStaleElementAssertion.AssertThrowsDueToElementRemovedFromPage(() =>
606606
testSelector.SelectedOption.GetAttribute("value"));
607607

608608
// After we press back, we should end up at the "/" page so we know browser history has been replaced

0 commit comments

Comments
 (0)