Search code examples
typescriptplaywright

Error when the test has passed in the console


Running this test in Playwright on mac:

 import {chromium, test} from "@playwright/test"

 test("login test demo", async() => {

    const browser = await  chromium.launch({
       headless: false
    });
    
   const context = await browser.newContext();
   const page = await context.newPage(); 
   
   (await page).goto ("https://ecommerce-playground.lambdatest.io");
  (await page).hover("//a[@data-toggle='dropdown']//span[contains(.,'My account')]");
   (await page).click("text=Login")

   


 })

causes this error in my console:

tests/login.test.ts:3:6 › login test demo

   at tests/login.test.ts:11

   9 |    const page = await context.newPage(); 
  10 |    
> 11 |    (await page).goto ("https://ecommerce-playground.lambdatest.io");
     |                 ^
  12 |   (await page).hover("//a[@data-toggle='dropdown']//span[contains(.,'My account')]");
  13 |    (await page).click("text=Login")
  14 |

    at /Users/dwayne/Documents/Development/Learn-Playwright /tests/login.test.ts:11:17

  1 passed (2.4s)
  1 error was not a part of any test, see above for details
Dwaynes-MacBook-Pro:Learn-Playwright  dwayne$ 

Solution

  • There's no need to create a browser in a test. Use the pre-built page. If you want to run headfully, use npx playwright test --headed or set that in the config.

    Don't use parentheses on your await calls, and avoid XPath.

    import {expect, test} from "@playwright/test"
    
    test("login test demo", async ({page}) => {
      await page.goto("https://ecommerce-playground.lambdatest.io");
      await page.getByRole("link", {name: "My account"})
        .evaluate(el => el.click());
      await expect(page).toHaveURL(/\/index\.php\?route=account\/login$/);
      await page.getByLabel("E-Mail Address").fill("user");
      await page.getByLabel("Password").fill("1234");
      await page.keyboard.press("Tab");
      await page.keyboard.press("Tab");
      await page.keyboard.press("Enter");
      await expect(page.getByText("Warning:")).toBeVisible();
    });
    

    In an actual test, you'd want to avoid the untrusted click, but a trusted click doesn't work here due to visibility, so we have to fall back.