npm init playwright
Default options below:
✔ Do you want to use TypeScript or JavaScript? · JavaScript
✔ Where to put your end-to-end tests? · tests
✔ Add a GitHub Actions workflow? (Y/n) · true
By default tests run in Headless mode
Inside project root directory, you can run several commands:
npx playwright test
Runs the end-to-end tests.
npx playwright test --project=chromium
Runs the tests only on Desktop Chrome.
npx playwright test tests/example.spec.js
Runs the tests of a specific file.
npx playwright test --debug
Runs the tests in debug mode.
We suggest that you begin by typing:
npx playwright test
By default tests run in Headless mode, To run them in headed mode use below command
npx playwright test --headed
In config you can find propery headless under use
headless: true
const {test, expect} = require('@playwright/test'); //import test variable, expect for doing assertions from playwright config
test('Page playwright test', async ({page}) => {
await page.goto("https://www.linkedin.com");
console.log(await page.title());
await expect(page).toHaveTitle("LinkedIn: Log In or Sign Up");
});
await page.waitForLoadState('networkidle');
await Promise.all(
[
page.waitForNavigation(),
signIn.click();
]
)
Two ways to acheive this
- using type method (or)
- using fill method
await page.locator("#username").type("rahulshettyacademy");
await page.locator("#password").fill("learning");
await page.locator("#signInBtn").click();
Here Locator ".card-body a" matches to give 4 elements.
To fetch 1st element from the matching elements
console.log(await page.locator(".card-body a").first().textContent());
To fetch last element from the matching elements
console.log(await page.locator(".card-body a").last().textContent());
To fetch 1st element using nth method
console.log(await page.locator(".card-body a").nth(0).textContent());
To fetch all text contents of matching elements
console.log(await page.locator(".card-body a").allTextContents());
For example i want to select a option in dropdown and then pause
await dropdown.selectOption("consult");
await page.pause();
Inspector looks like this
await dropdown.selectOption("consult");
await expect(page.locator(".radiotextsty").last()).toBeChecked(); //assertion to check if a radio btn is checked
console.log(await page.locator(".radiotextsty").last().isChecked()); //just true/false if checked/unchecked
await page.locator("input[name='terms']").uncheck(); //uncheck checkbox
expect(await page.locator("input[name='terms']").isChecked()).toBeFalsy(); //assert isChecked is giving false
await expect(page.locator("a[class='blinkingText']")).toHaveAttribute("class", "blinkingText");
we need to do a prmoise on waitforEvent of page and then click here pages are returned in array
const context = await browser.newContext();
const page = await context.newPage();
await page.goto("https://www.rahulshettyacademy.com/loginpagePractise/");
const [page2] = await Promise.all([
context.waitForEvent('page'),
await page.locator("a[class='blinkingText']").click()
])
console.log( "========> "+ await page2.locator("p[class='im-para red']").textContent());
// Now we can perform actions in tab1 too using page.locator()..
Using this debugger we can execute test, step by step using Step over icon
npx playwright codegen https://www.google.co.in