I have the following test:
namespace RazorSharp.Pages;
using Microsoft.Playwright;
using Xunit;
public class TextBoxPageTests : IClassFixture<TestServerHostFixture>
{
private readonly string _serverAddress;
public TextBoxPageTests(TestServerHostFixture testServerHost)
=> _serverAddress = testServerHost.ServerAddress;
[Fact]
public async Task Should_set_focus()
{
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
Headless = true
});
var context = await browser.NewContextAsync();
var page = await context.NewPageAsync();
await page.RunAndWaitForResponseAsync(async () => await page.GotoAsync($"{_serverAddress}/textbox"),
response => response.Ok);
var button = page.GetByTestId("set-focus");
await button.WaitForAsync();
var textbox = page.GetByTestId("textbox");
await textbox.WaitForAsync();
await button.ClickAsync();
await Assertions.Expect(textbox).ToBeFocusedAsync();
await browser.CloseAsync();
}
}
But for some reason it occasionally fails with the following error:
Microsoft.Playwright.PlaywrightException: Locator expected to be focused
Microsoft.Playwright.PlaywrightException
Locator expected to be focused
Call log:
LocatorAssertions.ToBeFocusedAsync with timeout 5000ms
waiting for GetByTestId("textbox")
locator resolved to <input type="text" minlength="-1" maxlength="-1" data-t…/>
unexpected value "not focused"
waiting for GetByTestId("textbox")
locator resolved to <input type="text" minlength="-1" maxlength="-1" data-t…/>
unexpected value "not focused"
locator resolved to <input type="text" minlength="-1" maxlength="-1" data-t…/>
unexpected value "not focused"
locator resolved to <input type="text" minlength="-1" maxlength="-1" data-t…/>
unexpected value "not focused"
locator resolved to <input type="text" minlength="-1" maxlength="-1" data-t…/>
unexpected value "not focused"
locator resolved to <input type="text" minlength="-1" maxlength="-1" data-t…/>
unexpected value "not focused"
locator resolved to <input type="text" minlength="-1" maxlength="-1" data-t…/>
unexpected value "not focused"
locator resolved to <input type="text" minlength="-1" maxlength="-1" data-t…/>
unexpected value "not focused"
locator resolved to <input type="text" minlength="-1" maxlength="-1" data-t…/>
unexpected value "not focused"
at Microsoft.Playwright.Core.AssertionsBase.ExpectImplAsync(String expression, FrameExpectOptions expectOptions, Object expected, String message) in /_/src/Playwright/Core/AssertionsBase.cs:line 87
at Microsoft.Playwright.Core.AssertionsBase.ExpectImplAsync(String expression, ExpectedTextValue[] expectedText, Object expected, String message, FrameExpectOptions options) in /_/src/Playwright/Core/AssertionsBase.cs:line 63
at RazorSharp.Pages.TextBoxPageTests.Should_set_focus() in /Users/eyal/Development/Projects/RazorSharp/src/RazorSharp.Tests.E2E/Pages/TextBox.page.tests.cs:line 41
at RazorSharp.Pages.TextBoxPageTests.Should_set_focus() in /Users/eyal/Development/Projects/RazorSharp/src/RazorSharp.Tests.E2E/Pages/TextBox.page.tests.cs:line 43
at Xunit.Sdk.TestInvoker`1.<>c__DisplayClass48_0.<<InvokeTestMethodAsync>b__1>d.MoveNext() in /_/src/xunit.execution/Sdk/Frameworks/Runners/TestInvoker.cs:line 264
--- End of stack trace from previous location ---
at Xunit.Sdk.ExecutionTimer.AggregateAsync(Func`1 asyncAction) in /_/src/xunit.execution/Sdk/Frameworks/ExecutionTimer.cs:line 48
at Xunit.Sdk.ExceptionAggregator.RunAsync(Func`1 code) in /_/src/xunit.core/Sdk/ExceptionAggregator.cs:line 90
The things I've tried are as followed:
- Call
RunAndWaitForResponseAsync
and only then callGotoAsync
and finally check the response status like this:
await page.RunAndWaitForResponseAsync(async () => await page.GotoAsync($"{_serverAddress}/textbox"),
response => response.Ok);
- Call
WaitForAsync
belowGetByTestId
calls like this:
var button = page.GetByTestId("set-focus");
await button.WaitForAsync();
var textbox = page.GetByTestId("textbox");
await textbox.WaitForAsync();
I'm new to Playwright so I might be doing something completely wrong but I can't figure what exactly and where but my manual tests and unit tests are all passing so I know the components participating and existing in the page work correctly.