1

Hello dear Playwright community! I am pretty new to this thing, and I ran into a problem as soon as I tried to write some basic form validation UI tests.

When I run my code step-by-step, it passes, so the logic seems to be correct. However, when I run it in headless mode, all the steps seem to be performed in wrong order, so I cannot catch my validation error message properly.

This is a really simple case for incorrect email, which is supposed to show an email validation error:

public async Task InvalidEmail(string email)
{
        await Page.GetByPlaceholder("Email").FillAsync(email);
        await Page.GetByPlaceholder("Password").FillAsync("123");
        await Page.Locator("text=Login").ClickAsync();

        await Expect(Page).ToHaveURLAsync(new Regex(".*Account/Login*"));

        ILocator emailValidationErrorMessage = Page.Locator("text='Wrong email format.'");
        await Expect(emailValidationErrorMessage).ToBeVisibleAsync();
}

When I run this code in Debug mode, everything works just fine. However, as soon as I run it in headless mode without clicking F10 for each step, this code seems to start looking for emailValidationErrorMessage before it fills in the inputs, doesn't find the message and fails:

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
  Failed InvalidEmail("test") [18 s]
LocatorAssertions.ToBeVisibleAsync with timeout 5000ms
waiting for Locator("text='Wrong email format.'")
waiting for Locator("text='Wrong email format.'")
  Stack Trace:
sBase.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 PlaywrightTests.Auth.InvalidEmail(String email) in C:\Users\podkurkova\Source\Repos\fedsegment\PlaywrightTests\Auth.cs:line 44
   at NUnit.Framework.Internal.TaskAwaitAdapter.GenericAdapter`1.BlockUntilCompleted()
   at NUnit.Framework.Internal.MessagePumpStrategy.NoMessagePumpStrategy.WaitForCompletion(AwaitAdapter awaiter)
   at NUnit.Framework.Internal.AsyncToSyncAdapter.Await(Func`1 invoke)
   at NUnit.Framework.Internal.Commands.TestMethodCommand.RunTestMethod(TestExecutionContext context)
   at NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(TestExecutionContext context)
   at NUnit.Framework.Internal.Commands.BeforeAndAfterTestCommand.<>c__DisplayClass1_0.<Execute>b__0()
   at NUnit.Framework.Internal.Commands.DelegatingTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action)

Failed!  - Failed:     1, Passed:     0, Skipped:     0, Total:     1, Duration: 18 s - PlaywrightTests.dll (net7.0)

I have been struggling with this for a couple of days and still have no clue of what I am doing wrong. Please would you help me out here?

I am using Microsoft.Playwright 1.31.1 + Chromium

ggorlen
  • 44,755
  • 7
  • 76
  • 106
nadya.wyn
  • 11
  • 1
  • can you share a link to the repo if possible to see full code. also can you make this a test using [Test] and make the email as a string instead of passing and give it a try @nadya.wyn – Gaurav Khurana Jul 13 '23 at 12:27

0 Answers0