playwright-community / playwright-go

Playwright for Go a browser automation library to control Chromium, Firefox and WebKit with a single API.
https://playwright-community.github.io/playwright-go/
MIT License
2.11k stars 156 forks source link

Does this ship with assertions? #393

Closed on3iro closed 10 months ago

on3iro commented 10 months ago

Hey folks,

Thanks for this great library. I have a few questions I wasn't able to answer by reading the docs. Help would be very appreciated :)

  1. Does this library ship with assertions? I saw that you seem to use expect in conjunction with typical playwright assertions toBeInDocument etc. internally and you are also exporting a LocatorAssertions-interface, though I don't understand if and how exactly these are supposed to be used. The recipes from the docs do not seem to use them.
  2. I have a weird issue where tests only ever succeed on their second run. So let's say I have a page with a simple crud 'add' operation, which creates some entity. The test does not seem to wait long enough for this entity to be rendered and fails. However when I run my test suite a second time afterwards, it does always seem to work (maybe because the entity does still exist in the database? I am not quite sure why that is happening...). Any idea what I could be doing wrong? Below is an example test:
func TestUserCanAddHousehold(t *testing.T) {
    test.WithPlaywright(func(pw test.PlaywrightCtx) {
        // Arrange
        test.CreateUser("popel")

        // Act
        test.LoginUserE2E("user", "test", pw.Page)
        test.CreateHouseholdE2E("Testhousehold", pw.Page)

        // Assert
        count, err := pw.Page.GetByText("Testhousehold").Count()
        test.AssertExistsOnce("Could not find Testhousehold", count, err)

        count, err = pw.Page.GetByText("Owner: user").Count()
        test.AssertExistsOnce("Could not find 'Owner: user'", count, err)
    })
}

// LoginUserE2E logs the user in via browser
func LoginUserE2E(name, password string, page playwright.Page) {
    page.Goto("localhost:3008/login")

    page.GetByPlaceholder("username").Fill(name)
    page.GetByPlaceholder("password").Fill(password)
    page.GetByRole("button").Click()

    page.WaitForURL("*/households")
}

// Requires to be logged in!
func CreateHouseholdE2E(name string, page playwright.Page) {
    page.Goto("/households")

    page.GetByText("New Household").Click()
    page.GetByPlaceholder("Name").Fill(name)
    page.GetByText("Save").Click()
}

Thanks in advance :)

canstand commented 10 months ago
  1. See #278 , you can do this:

    expect := playwright.NewPlaywrightAssertions(5000) 
    
    require.NoError(t, expect.Page(page1).ToHaveTitle("unkown"))
    require.NoError(t, expect.Locator(checkbox1).ToBeChecked())
  2. Locator doesn't wait, you need Web-first assertions,

    
    expect := playwright.NewPlaywrightAssertions(5000)  // default timeout 5 senconds

err := expect.Locator(pw.Page.GetByText("Testhousehold")).ToHaveCount(1)

on3iro commented 10 months ago

Awesome, thanks! Will definitely check these out.