I have written a script to test a website (https://www.demoblaze.com/), I am trying to validate whether the logout button is visible to the user once the login process is complete.
I am using "expect().toBeVisible()" to check the presence of the logout button, At the run time I am getting an error
Error: Timed out 5000ms waiting for expect(received).toBeVisible()
Call log:
- expect.toBeVisible with timeout 5000ms
- waiting for locator('xpath=//a[normalize-space()='Log out']')
- locator resolved to <a href="#" id="logout2" class="nav-link" onclick="…>Log out</a>
- unexpected value "hidden"
- locator resolved to <a href="#" id="logout2" class="nav-link" onclick="…>Log out</a>
- unexpected value "hidden"
- locator resolved to <a href="#" id="logout2" class="nav-link" onclick="…>Log out</a>
- unexpected value "hidden"
- locator resolved to <a href="#" id="logout2" class="nav-link" onclick="…>Log out</a>
- unexpected value "hidden"
- locator resolved to <a href="#" id="logout2" class="nav-link" onclick="…>Log out</a>
- unexpected value "hidden"
- locator resolved to <a href="#" id="logout2" class="nav-link" onclick="…>Log out</a>
- unexpected value "hidden"
- locator resolved to <a href="#" id="logout2" class="nav-link" onclick="…>Log out</a>
- unexpected value "hidden"
- locator resolved to <a href="#" id="logout2" class="nav-link" onclick="…>Log out</a>
- unexpected value "hidden"
- locator resolved to <a href="#" id="logout2" class="nav-link" onclick="…>Log out</a>
- unexpected value "hidden"
Below is the code I am executing
let { test, expect } = require("@playwright/test");
test("Locator", async ({ page }) => {
await page.goto("https://www.demoblaze.com/");
//click on login button using Property element
await page.locator("#login2").click();
//username= CSS
await page.fill("#loginusername", "pavanol");
//provide password =CSS
await page.fill("input[id='loginpassword']", "test@123");
//Click On Login - Xpath
await page.click("//input[@id='loginpassword']");
//verify logout button presence xpath
const logoutLink = await page.locator("//a[normalize-space()='Log out']");
//assertion
await expect(logoutLink).toBeVisible();
await page.close;
});
2
Answers
You currently don’t specify a timeout when you call toBeVisible(), and the default in your code is using 5000ms, which is 5 seconds.
Perhaps it is possible that the page locater is taking longer than 5 seconds to find the logout button.
One solution you can try; from https://playwright.dev/docs/test-timeouts:
To increase the timeout to a large enough value that it might be able to pick it up.
Await can take a really long time especially looking for elements so I think this would be a good first step.
There is not element with
#logout2
locator