Skip to content

Commit 63baa06

Browse files
authored
Use assertion library more consistently (#26519)
Using Browser.Exists is equivalent to Browser.FindElement except it provides better logs and diagnostics when the assertion fails. The additional waits will also rule out failures due to the browser taking time to update possibly improving stability. I looked at the implementation of WebDriverWait to verify that using it will not introduce additional delays to our tests.
1 parent 854b4aa commit 63baa06

36 files changed

+415
-414
lines changed

src/Components/test/E2ETest/Infrastructure/WebDriverExtensions/BasicTestAppWebDriverExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static IWebElement MountTestComponent<TComponent>(this IWebDriver browser
1515
var testSelector = browser.WaitUntilTestSelectorReady();
1616
testSelector.SelectByValue("none");
1717
testSelector.SelectByValue(componentTypeName);
18-
return browser.FindElement(By.TagName("app"));
18+
return browser.Exists(By.TagName("app"));
1919
}
2020

2121
public static SelectElement WaitUntilTestSelectorReady(this IWebDriver browser)

src/Components/test/E2ETest/ServerExecutionTests/CircuitGracefulTerminationTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected override void InitializeAsyncCore()
4545
{
4646
Navigate(ServerPathBase, noReload: false);
4747
Browser.MountTestComponent<GracefulTermination>();
48-
Browser.Equal("Graceful Termination", () => Browser.FindElement(By.TagName("h1")).Text);
48+
Browser.Equal("Graceful Termination", () => Browser.Exists(By.TagName("h1")).Text);
4949

5050
GracefulDisconnectCompletionSource = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
5151
Sink = _serverFixture.Host.Services.GetRequiredService<TestSink>();
@@ -95,7 +95,7 @@ public async Task ClosingTheBrowserWindow_GracefullyDisconnects_WhenNavigatingAw
9595
public async Task NavigatingToProtocolLink_DoesNotGracefullyDisconnect_TheCurrentCircuit()
9696
{
9797
// Arrange & Act
98-
var element = Browser.FindElement(By.Id("mailto-link"));
98+
var element = Browser.Exists(By.Id("mailto-link"));
9999
element.Click();
100100
await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);
101101

@@ -108,7 +108,7 @@ public async Task NavigatingToProtocolLink_DoesNotGracefullyDisconnect_TheCurren
108108
public async Task DownloadAction_DoesNotGracefullyDisconnect_TheCurrentCircuit()
109109
{
110110
// Arrange & Act
111-
var element = Browser.FindElement(By.Id("download-link"));
111+
var element = Browser.Exists(By.Id("download-link"));
112112
element.Click();
113113
await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);
114114

@@ -121,7 +121,7 @@ public async Task DownloadAction_DoesNotGracefullyDisconnect_TheCurrentCircuit()
121121
public async Task DownloadHref_DoesNotGracefullyDisconnect_TheCurrentCircuit()
122122
{
123123
// Arrange & Act
124-
var element = Browser.FindElement(By.Id("download-href"));
124+
var element = Browser.Exists(By.Id("download-href"));
125125
element.Click();
126126
await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);
127127

src/Components/test/E2ETest/ServerExecutionTests/ComponentWithParametersTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ public void PassingParametersToComponentsFromThePageWorks()
3535

3636
Browser.Exists(By.CssSelector(".interactive"));
3737

38-
var parameter1 = Browser.FindElement(By.CssSelector(".Param1"));
38+
var parameter1 = Browser.Exists(By.CssSelector(".Param1"));
3939
Assert.Equal(100, parameter1.FindElements(By.CssSelector("li")).Count);
4040
Assert.Equal("99 99", parameter1.FindElement(By.CssSelector("li:last-child")).Text);
4141

4242
// The assigned value is of a more derived type than the declared model type. This check
4343
// verifies we use the actual model type during round tripping.
44-
var parameter2 = Browser.FindElement(By.CssSelector(".Param2"));
44+
var parameter2 = Browser.Exists(By.CssSelector(".Param2"));
4545
Assert.Equal("Value Derived-Value", parameter2.Text);
4646

4747
// This check verifies CaptureUnmatchedValues works
@@ -54,7 +54,7 @@ public void PassingParametersToComponentsFromThePageWorks()
5454

5555
private void BeginInteractivity()
5656
{
57-
Browser.FindElement(By.Id("load-boot-script")).Click();
57+
Browser.Exists(By.Id("load-boot-script")).Click();
5858
}
5959
}
6060
}

src/Components/test/E2ETest/ServerExecutionTests/MultipleRootComponentsTest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public void CanRenderMultipleRootComponents()
6565
{
6666
Navigate("/multiple-components");
6767

68+
Browser.Exists(By.CssSelector(".greet-wrapper .greet"));
6869
var greets = Browser.FindElements(By.CssSelector(".greet-wrapper .greet")).Select(e => e.Text).ToArray();
6970

7071
Assert.Equal(7, greets.Length); // 1 statically rendered + 5 prerendered + 1 server prerendered
@@ -73,7 +74,7 @@ public void CanRenderMultipleRootComponents()
7374
Assert.Single(greets, "Hello Abraham");
7475
Assert.Equal(2, greets.Where(g => g == "Hello Blue fish").Count());
7576
Assert.Equal(3, greets.Where(g => string.Equals("Hello", g)).Count()); // 3 server prerendered without parameters
76-
var content = Browser.FindElement(By.Id("test-container")).GetAttribute("innerHTML");
77+
var content = Browser.Exists(By.Id("test-container")).GetAttribute("innerHTML");
7778
var markers = ReadMarkers(content);
7879
var componentSequence = markers.Select(m => m.Item1.PrerenderId != null).ToArray();
7980
var expectedComponentSequence = new bool[]
@@ -123,7 +124,7 @@ public void CanRenderMultipleRootComponents()
123124

124125
private void BeginInteractivity()
125126
{
126-
Browser.FindElement(By.Id("load-boot-script")).Click();
127+
Browser.Exists(By.Id("load-boot-script")).Click();
127128
}
128129
}
129130
}

src/Components/test/E2ETest/ServerExecutionTests/PrerenderingTest.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ public void CanTransitionFromPrerenderedToInteractiveMode()
3434
Navigate("/prerendered/prerendered-transition");
3535

3636
// Prerendered output shows "not connected"
37-
Browser.Equal("not connected", () => Browser.FindElement(By.Id("connected-state")).Text);
37+
Browser.Equal("not connected", () => Browser.Exists(By.Id("connected-state")).Text);
3838

3939
// Once connected, output changes
4040
BeginInteractivity();
41-
Browser.Equal("connected", () => Browser.FindElement(By.Id("connected-state")).Text);
41+
Browser.Equal("connected", () => Browser.Exists(By.Id("connected-state")).Text);
4242

4343
// ... and now the counter works
44-
Browser.FindElement(By.Id("increment-count")).Click();
45-
Browser.Equal("1", () => Browser.FindElement(By.Id("count")).Text);
44+
Browser.Exists(By.Id("increment-count")).Click();
45+
Browser.Equal("1", () => Browser.Exists(By.Id("count")).Text);
4646
}
4747

4848
[Fact]
@@ -51,7 +51,7 @@ public void PrerenderingWaitsForAsyncDisposableComponents()
5151
Navigate("/prerendered/prerendered-async-disposal");
5252

5353
// Prerendered output shows "not connected"
54-
Browser.Equal("After async disposal", () => Browser.FindElement(By.Id("disposal-message")).Text);
54+
Browser.Equal("After async disposal", () => Browser.Exists(By.Id("disposal-message")).Text);
5555
}
5656

5757
[Fact]
@@ -60,22 +60,22 @@ public void CanUseJSInteropFromOnAfterRenderAsync()
6060
Navigate("/prerendered/prerendered-interop");
6161

6262
// Prerendered output can't use JSInterop
63-
Browser.Equal("No value yet", () => Browser.FindElement(By.Id("val-get-by-interop")).Text);
64-
Browser.Equal(string.Empty, () => Browser.FindElement(By.Id("val-set-by-interop")).GetAttribute("value"));
63+
Browser.Equal("No value yet", () => Browser.Exists(By.Id("val-get-by-interop")).Text);
64+
Browser.Equal(string.Empty, () => Browser.Exists(By.Id("val-set-by-interop")).GetAttribute("value"));
6565

6666
BeginInteractivity();
6767

6868
// Once connected, we can
69-
Browser.Equal("Hello from interop call", () => Browser.FindElement(By.Id("val-get-by-interop")).Text);
70-
Browser.Equal("Hello from interop call", () => Browser.FindElement(By.Id("val-set-by-interop")).GetAttribute("value"));
69+
Browser.Equal("Hello from interop call", () => Browser.Exists(By.Id("val-get-by-interop")).Text);
70+
Browser.Equal("Hello from interop call", () => Browser.Exists(By.Id("val-set-by-interop")).GetAttribute("value"));
7171
}
7272

7373
[Fact]
7474
public void IsCompatibleWithLazyLoadWebAssembly()
7575
{
7676
Navigate("/prerendered/WithLazyAssembly");
7777

78-
var button = Browser.FindElement(By.Id("use-package-button"));
78+
var button = Browser.Exists(By.Id("use-package-button"));
7979

8080
button.Click();
8181

@@ -93,13 +93,13 @@ public void CanReadUrlHashOnlyOnceConnected()
9393
Navigate(url);
9494
Browser.Equal(
9595
_serverFixture.RootUri + urlWithoutHash,
96-
() => Browser.FindElement(By.TagName("strong")).Text);
96+
() => Browser.Exists(By.TagName("strong")).Text);
9797

9898
// Once connected, you do have access to the full URL
9999
BeginInteractivity();
100100
Browser.Equal(
101101
_serverFixture.RootUri + url,
102-
() => Browser.FindElement(By.TagName("strong")).Text);
102+
() => Browser.Exists(By.TagName("strong")).Text);
103103
}
104104

105105
[Theory]
@@ -130,17 +130,17 @@ public void CanAccessAuthenticationStateDuringStaticPrerendering(string initialU
130130
// See that the authentication state is usable during the initial prerendering
131131
SignInAs(initialUsername, null);
132132
Navigate("/prerendered/prerendered-transition");
133-
Browser.Equal($"Hello, {initialUsername ?? "anonymous"}!", () => Browser.FindElement(By.TagName("h1")).Text);
133+
Browser.Equal($"Hello, {initialUsername ?? "anonymous"}!", () => Browser.Exists(By.TagName("h1")).Text);
134134

135135
// See that during connection, we update to whatever the latest authentication state now is
136136
SignInAs(interactiveUsername, null, useSeparateTab: true);
137137
BeginInteractivity();
138-
Browser.Equal($"Hello, {interactiveUsername ?? "anonymous"}!", () => Browser.FindElement(By.TagName("h1")).Text);
138+
Browser.Equal($"Hello, {interactiveUsername ?? "anonymous"}!", () => Browser.Exists(By.TagName("h1")).Text);
139139
}
140140

141141
private void BeginInteractivity()
142142
{
143-
Browser.FindElement(By.Id("load-boot-script")).Click();
143+
Browser.Exists(By.Id("load-boot-script")).Click();
144144
}
145145

146146
private void AssertLogDoesNotContainCriticalMessages(params string[] messages)

src/Components/test/E2ETest/ServerExecutionTests/ProtectedBrowserStorageUsageTest.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ protected override void InitializeAsyncCore()
4343
public void LocalStoragePersistsOnRefresh()
4444
{
4545
// Local storage initially cleared
46-
var incrementLocalButton = Browser.FindElement(By.Id("increment-local"));
47-
var localCount = Browser.FindElement(By.Id("local-count"));
46+
var incrementLocalButton = Browser.Exists(By.Id("increment-local"));
47+
var localCount = Browser.Exists(By.Id("local-count"));
4848
Browser.Equal("0", () => localCount.Text);
4949

5050
// Local storage updates
@@ -55,16 +55,16 @@ public void LocalStoragePersistsOnRefresh()
5555
Browser.Navigate().Refresh();
5656
Browser.MountTestComponent<ProtectedBrowserStorageUsageComponent>();
5757

58-
localCount = Browser.FindElement(By.Id("local-count"));
58+
localCount = Browser.Exists(By.Id("local-count"));
5959
Browser.Equal("1", () => localCount.Text);
6060
}
6161

6262
[Fact]
6363
public void LocalStoragePersistsAcrossTabs()
6464
{
6565
// Local storage initially cleared
66-
var incrementLocalButton = Browser.FindElement(By.Id("increment-local"));
67-
var localCount = Browser.FindElement(By.Id("local-count"));
66+
var incrementLocalButton = Browser.Exists(By.Id("increment-local"));
67+
var localCount = Browser.Exists(By.Id("local-count"));
6868
Browser.Equal("0", () => localCount.Text);
6969

7070
// Local storage updates in current tab
@@ -73,16 +73,16 @@ public void LocalStoragePersistsAcrossTabs()
7373

7474
// Local storage persists across tabs
7575
OpenNewSession();
76-
localCount = Browser.FindElement(By.Id("local-count"));
76+
localCount = Browser.Exists(By.Id("local-count"));
7777
Browser.Equal("1", () => localCount.Text);
7878
}
7979

8080
[Fact]
8181
public void SessionStoragePersistsOnRefresh()
8282
{
8383
// Session storage initially cleared
84-
var incrementSessionButton = Browser.FindElement(By.Id("increment-session"));
85-
var sessionCount = Browser.FindElement(By.Id("session-count"));
84+
var incrementSessionButton = Browser.Exists(By.Id("increment-session"));
85+
var sessionCount = Browser.Exists(By.Id("session-count"));
8686
Browser.Equal("0", () => sessionCount.Text);
8787

8888
// Session storage updates
@@ -93,16 +93,16 @@ public void SessionStoragePersistsOnRefresh()
9393
Browser.Navigate().Refresh();
9494
Browser.MountTestComponent<ProtectedBrowserStorageUsageComponent>();
9595

96-
sessionCount = Browser.FindElement(By.Id("session-count"));
96+
sessionCount = Browser.Exists(By.Id("session-count"));
9797
Browser.Equal("1", () => sessionCount.Text);
9898
}
9999

100100
[Fact]
101101
public void SessionStorageDoesNotPersistAcrossTabs()
102102
{
103103
// Session storage initially cleared
104-
var incrementSessionButton = Browser.FindElement(By.Id("increment-session"));
105-
var sessionCount = Browser.FindElement(By.Id("session-count"));
104+
var incrementSessionButton = Browser.Exists(By.Id("increment-session"));
105+
var sessionCount = Browser.Exists(By.Id("session-count"));
106106
Browser.Equal("0", () => sessionCount.Text);
107107

108108
// Session storage updates in current tab
@@ -111,7 +111,7 @@ public void SessionStorageDoesNotPersistAcrossTabs()
111111

112112
// Session storage does not persist across tabs
113113
OpenNewSession();
114-
sessionCount = Browser.FindElement(By.Id("session-count"));
114+
sessionCount = Browser.Exists(By.Id("session-count"));
115115
Browser.Equal("0", () => sessionCount.Text);
116116
}
117117

@@ -130,7 +130,7 @@ private void OpenNewSession()
130130
Keys.Command :
131131
Keys.Control;
132132

133-
var newTabLink = Browser.FindElement(By.Id("new-tab"));
133+
var newTabLink = Browser.Exists(By.Id("new-tab"));
134134
var action = new Actions(Browser);
135135
action.KeyDown(modifierKey).MoveToElement(newTabLink).Click().KeyUp(modifierKey).Perform();
136136

src/Components/test/E2ETest/ServerExecutionTests/ServerGlobalizationTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public override void CanSetCultureAndParseCultureSensitiveNumbersAndDates(string
4545

4646
protected override void SetCulture(string culture)
4747
{
48-
var selector = new SelectElement(Browser.FindElement(By.Id("culture-selector")));
48+
var selector = new SelectElement(Browser.Exists(By.Id("culture-selector")));
4949
selector.SelectByValue(culture);
5050

5151
// Click the link to return back to the test page

src/Components/test/E2ETest/ServerExecutionTests/ServerInteropTestDefaultExceptionsBehavior.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ public void DotNetExceptionDetailsAreNotLoggedByDefault()
4545
var actualValues = new Dictionary<string, string>();
4646

4747
// Act
48-
var interopButton = Browser.FindElement(By.Id("btn-interop"));
48+
var interopButton = Browser.Exists(By.Id("btn-interop"));
4949
interopButton.Click();
5050

5151
Browser.Exists(By.Id("done-with-interop"));
5252

5353
foreach (var expectedValue in expectedValues)
5454
{
55-
var currentValue = Browser.FindElement(By.Id(expectedValue.Key));
55+
var currentValue = Browser.Exists(By.Id(expectedValue.Key));
5656
actualValues.Add(expectedValue.Key, currentValue.Text);
5757
}
5858

src/Components/test/E2ETest/ServerExecutionTests/ServerInteropTestJsInvocationsTimeoutsBehavior.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected override void InitializeAsyncCore()
3333
public async Task LongRunningJavaScriptFunctionsResultInCancellationAndWorkingAppAfterFunctionCompletion()
3434
{
3535
// Act & Assert
36-
var interopButton = Browser.FindElement(By.Id("btn-interop"));
36+
var interopButton = Browser.Exists(By.Id("btn-interop"));
3737
interopButton.Click();
3838

3939
Browser.Exists(By.Id("done-with-interop"));
@@ -43,7 +43,7 @@ public async Task LongRunningJavaScriptFunctionsResultInCancellationAndWorkingAp
4343
// wait 10 seconds, js method completes in 5 seconds, after this point it would have triggered a completion for sure.
4444
await Task.Delay(10000);
4545

46-
var circuitFunctional = Browser.FindElement(By.Id("circuit-functional"));
46+
var circuitFunctional = Browser.Exists(By.Id("circuit-functional"));
4747
circuitFunctional.Click();
4848

4949
Browser.Exists(By.Id("done-circuit-functional"));

src/Components/test/E2ETest/ServerExecutionTests/ServerLocalizationTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected override void InitializeAsyncCore()
3535
[InlineData("fr-FR", "Bonjour!")]
3636
public void CanSetCultureAndReadLocalizedResources(string culture, string message)
3737
{
38-
var selector = new SelectElement(Browser.FindElement(By.Id("culture-selector")));
38+
var selector = new SelectElement(Browser.Exists(By.Id("culture-selector")));
3939
selector.SelectByValue(culture);
4040

4141
// Click the link to return back to the test page
@@ -47,7 +47,7 @@ public void CanSetCultureAndReadLocalizedResources(string culture, string messag
4747
var cultureDisplay = Browser.Exists(By.Id("culture-name-display"));
4848
Assert.Equal($"Culture is: {culture}", cultureDisplay.Text);
4949

50-
var messageDisplay = Browser.FindElement(By.Id("message-display"));
50+
var messageDisplay = Browser.Exists(By.Id("message-display"));
5151
Assert.Equal(message, messageDisplay.Text);
5252
}
5353
}

0 commit comments

Comments
 (0)