Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions skills/playwright-cli/references/running-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ playwright-cli run-code "async page => {

# Wait for specific element
playwright-cli run-code "async page => {
await page.waitForSelector('.loading', { state: 'hidden' });
await page.locator('.loading').waitFor({ state: 'hidden' });
}"

# Wait for function to return true
Expand All @@ -97,7 +97,7 @@ playwright-cli run-code "async page => {

# Wait with timeout
playwright-cli run-code "async page => {
await page.waitForSelector('.result', { timeout: 10000 });
await page.locator('.result').waitFor({ timeout: 10000 });
}"
```

Expand All @@ -122,10 +122,9 @@ playwright-cli run-code "async page => {
```bash
# Handle file download
playwright-cli run-code "async page => {
const [download] = await Promise.all([
page.waitForEvent('download'),
page.click('a.download-link')
]);
const downloadPromise = page.waitForEvent('download');
await page.locator('a.download-link').click();
const download = await downloadPromise;
await download.saveAs('./downloaded-file.pdf');
return download.suggestedFilename();
}"
Expand Down Expand Up @@ -197,7 +196,7 @@ playwright-cli run-code "async page => {
# Try-catch in run-code
playwright-cli run-code "async page => {
try {
await page.click('.maybe-missing', { timeout: 1000 });
await page.locator('.maybe-missing').click({ timeout: 1000 });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be even better to instruct it to use role locators

return 'clicked';
} catch (e) {
return 'element not found';
Expand All @@ -211,9 +210,9 @@ playwright-cli run-code "async page => {
# Login and save state
playwright-cli run-code "async page => {
await page.goto('https://example.com/login');
await page.fill('input[name=email]', 'user@example.com');
await page.fill('input[name=password]', 'secret');
await page.click('button[type=submit]');
await page.locator('input[name=email]').fill('user@example.com');
await page.locator('input[name=password]').fill('secret');
await page.locator('button[type=submit]').click();
await page.waitForURL('**/dashboard');
await page.context().storageState({ path: 'auth.json' });
return 'Login successful';
Expand Down