Learn basic automation with Playwright - with code
If you are yet to set up a playwright here is the post to know about the advantages of the playwright, and how to set up and run the first playwright script. (Click here to read). After setting up the playwright the first thing you must know is how to locate the elements? how to click on those elements, how to type in textbox, how to select from the dropdown, how to select the radio button or check box? If you know these, you can write scripts using the playwright.
Locators and Actions in Playwright
if you are interested in the video tutorial on the same, please go through the below video and subscribe to our youtube channel. Here I have explained writing the first script with live code.
How to find locators (Xpath) in the playwright?
For Example -
- page.getByRole() - to locate by explicit and implicit accessibility attributes.
- page.getByText() to locate by-text content.
- page.getByLabel() to locate a form control by the associated label's text.
- page.getByPlaceholder() to locate an input by the placeholder.
- page.getByAltText() to locate an element, usually an image, by its text alternative.
- page.getByTitle() to locate an element by its title attribute.
- page.getByTestId() to locate an element based on its data-tested attribute (other attributes can be configured).
From the above methods, the user can directly get the elements. For example for getByRole(), you need to provide a role like a button, textbox, div etc, and some unique parameter
await page.getByRole('button', { name: 'Sign in' }).click();
Others are straightforward, As per name you just need to pass text, label, title, or testID.
How to click on the button in the playwright?
How to Type in the TextBox using Playwright?
If you need to type like a real keyboard, you can use locator.type() it will enter character by character.
How to Select from the Dropdown in the Playwright?
Similarly for focus we can use locator.focus() method.
No comments