Udemy Playwright: Web Automation Testing From Zero to Hero: Difference between revisions
Jump to navigation
Jump to search
Udemy Playwright: Web Automation Testing From Zero to Hero (view source)
Revision as of 21:16, 20 May 2024
, 20 May→Locator Syntax Rules
Line 114: | Line 114: | ||
==== Locator Syntax Rules ==== | ==== Locator Syntax Rules ==== | ||
<nowiki> | |||
page.locator('input') //finds all of them | |||
page.locator('#inputEmail1') // by id | |||
page.locator('.shape-rectangle') //by class value | |||
page.locator('[placeholder="Email"]') // by attribute | |||
page.locator('[class="input-full-width size-medium status-basic shape-rectangle nb-transition cdk-focused cdk-mouse-focused"]') // by class value (full) | |||
page.locator('input[placeholder="Email"][nbinput].shape-rectangle') // combine selectors | |||
page.locator('//*[@id="inputEmail1"]') // XPath (NOT Recommended because it's testing implementation rather than user visible aspects) | |||
page.locator(':text("Using")') // by partial text match | |||
page.locator(':text-is("Using the Grid")') // by exact text match | |||
</nowiki> | |||
Note: | |||
If you had previously run the test, and the associated browser window is still open, then when the cursor is on a code line with <code>page.locator(...)</code> it highlights the elements selected by the locator ''(very cool!)''<br> | |||
It's blue when a single element is selected, and orange when multiple elements match the locator. | |||
<code>page.locator(...)</code> will always return all matching elements, can use <code>.first()</code> to refine to first element to perform an action. | |||
use <code>npx playwright test --ui</code> and click the watch icon so that test auto-re-runs when you edit the code ''(I love this)'' |