Frequent calls, JS first, Elixir below. Snippets alias Playwright.{Page, Locator, ...};
From playwright.dev to Elixir explains the rules behind them.
Setup
Launch
const browser = await chromium.launch();
const context = await browser.newContext({ baseURL: 'http://localhost:4000' });
const page = await context.newPage();browser = Playwright.instance() |> Playwright.chromium() |> BrowserType.launch!()
context = Browser.new_context!(browser, base_url: "http://localhost:4000")
page = BrowserContext.new_page!(context)Close
await context.close();
await browser.close();BrowserContext.close!(context)
Browser.close!(browser)Navigation
Go to a URL
await page.goto('/login');
await page.goBack();
await page.goForward();
await page.reload();Page.goto!(page, "/login")
Page.go_back!(page)
Page.go_forward!(page)
Page.reload!(page)Where am I
page.url();
await page.title();
await page.waitForURL('**/dashboard');Page.url(page)
Page.title!(page)
Page.wait_for_url!(page, "**/dashboard")Locators
Find
page.getByRole('button', { name: 'Sign in' });
page.getByLabel('Email');
page.getByPlaceholder('name@example.com');
page.getByText('Welcome', { exact: true });
page.getByTestId('submit');
page.locator('.flash');Page.get_by_role(page, :button, name: "Sign in")
Page.get_by_label(page, "Email")
Page.get_by_placeholder(page, "name@example.com")
Page.get_by_text(page, "Welcome", exact: true)
Page.get_by_test_id(page, "submit")
Page.locator(page, ".flash")Narrow down
page.getByRole('listitem').filter({ hasText: 'Apple' });
page.locator('nav').getByRole('link');
page.getByRole('listitem').nth(1);
page.getByRole('listitem').first();Page.get_by_role(page, :listitem) |> Locator.filter(has_text: "Apple")
Page.locator(page, "nav") |> Locator.get_by_role(:link)
Page.get_by_role(page, :listitem) |> Locator.nth(1)
Page.get_by_role(page, :listitem) |> Locator.first()Many elements
await page.getByRole('listitem').count();
for (const item of await page.getByRole('listitem').all())
console.log(await item.textContent());Page.get_by_role(page, :listitem) |> Locator.count!()
for item <- Page.get_by_role(page, :listitem) |> Locator.all!() do
IO.puts(Locator.text_content!(item))
endFrames
page.frameLocator('#editor').getByRole('textbox');Page.frame_locator(page, "#editor") |> FrameLocator.get_by_role(:textbox)Actions
Click and type
await locator.click();
await locator.dblclick();
await locator.fill('ada@example.com');
await locator.press('Enter');
await locator.pressSequentially('hello');
await locator.clear();Locator.click!(locator)
Locator.dblclick!(locator)
Locator.fill!(locator, "ada@example.com")
Locator.press!(locator, "Enter")
Locator.press_sequentially!(locator, "hello")
Locator.clear!(locator)Forms
await locator.check();
await locator.uncheck();
await locator.selectOption('blue');
await locator.setInputFiles('avatar.png');Locator.check!(locator)
Locator.uncheck!(locator)
Locator.select_option!(locator, "blue")
Locator.set_input_files!(locator, "avatar.png")Mouse and keyboard
await locator.hover();
await locator.dragTo(target);
await page.keyboard.press('Control+A');
await page.mouse.click(10, 10);Locator.hover!(locator)
Locator.drag_to!(locator, target)
Page.keyboard(page) |> Keyboard.press!("Control+A")
Page.mouse(page) |> Mouse.click!(10, 10)Read
await locator.textContent();
await locator.innerText();
await locator.inputValue();
await locator.getAttribute('href');
await locator.isVisible();Locator.text_content!(locator)
Locator.inner_text!(locator)
Locator.input_value!(locator)
Locator.get_attribute!(locator, "href")
Locator.is_visible!(locator)Assertions
Locator
await expect(locator).toBeVisible();
await expect(locator).toHaveText('Welcome');
await expect(locator).toContainText('Wel');
await expect(locator).toHaveValue('ada@example.com');
await expect(locator).toHaveCount(3);
await expect(locator).toBeChecked();
await expect(locator).toHaveAttribute('href', '/home');Expect.to_be_visible!(locator)
Expect.to_have_text!(locator, "Welcome")
Expect.to_contain_text!(locator, "Wel")
Expect.to_have_value!(locator, "ada@example.com")
Expect.to_have_count!(locator, 3)
Expect.to_be_checked!(locator)
Expect.to_have_attribute!(locator, "href", "/home")Page
await expect(page).toHaveTitle(/Dashboard/);
await expect(page).toHaveURL('/dashboard');Expect.to_have_title!(page, ~r/Dashboard/)
Expect.to_have_url!(page, "/dashboard")Negate, time out
await expect(locator).not.toBeVisible();
await expect(locator).toBeVisible({ timeout: 10_000 });Expect.not_to_be_visible!(locator)
Expect.to_be_visible!(locator, timeout: 10_000)For no timeout, pass timeout: :infinity; 0 also means no timeout, unlike Elixir's convention (details).
Waiting
Popup
const popupPromise = page.waitForEvent('popup');
await page.getByText('Open').click();
const popup = await popupPromise;popup =
Page.expect_popup!(page, fn ->
Page.get_by_text(page, "Open") |> Locator.click!()
end)Download
const downloadPromise = page.waitForEvent('download');
await page.getByText('Export').click();
const download = await downloadPromise;
await download.saveAs('report.csv');download =
Page.expect_download!(page, fn ->
Page.get_by_text(page, "Export") |> Locator.click!()
end)
Download.save_as!(download, "report.csv")States
await locator.waitFor({ state: 'hidden' });
await page.waitForLoadState('networkidle');
await page.waitForFunction(() => window.ready);Locator.wait_for!(locator, state: :hidden)
Page.wait_for_load_state!(page, :networkidle)
Page.wait_for_function!(page, "() => window.ready")Listeners
page.on('console', msg => console.log(msg.text()));
page.on('dialog', dialog => dialog.accept());require Logger
Page.on(page, :console, &Logger.info(ConsoleMessage.text(&1)))
Page.on(page, :dialog, &Dialog.accept!/1)Network
Mock
await page.route('**/api/user', route =>
route.fulfill({ json: { name: 'Ada' } }));
await page.route('**/*.png', route => route.abort());Page.route!(page, "**/api/user", &Route.fulfill!(&1, json: %{name: "Ada"}))
Page.route!(page, "**/*.png", &Route.abort!/1)Modify a request
await page.route('**/api/**', route => {
const headers = { ...route.request().headers(), 'x-test': '1' };
return route.continue({ headers });
});Page.route!(page, "**/api/**", fn route, request ->
headers = Map.put(Request.headers(request), "x-test", "1")
Route.continue!(route, headers: headers)
end)Wait for a response
const responsePromise = page.waitForResponse('**/api/user');
await page.getByText('Load').click();
const response = await responsePromise;
await response.json();response =
Page.expect_response!(page, "**/api/user", fn ->
Page.get_by_text(page, "Load") |> Locator.click!()
end)
Response.json!(response)API requests
const response = await page.request.get('/api/user');
await expect(response).toBeOK();
await response.json();response = Page.request(page) |> APIRequestContext.get!("/api/user")
Expect.to_be_ok!(response)
APIResponse.json!(response)Screenshots and scripts
Screenshots
await page.screenshot({ path: 'page.png', fullPage: true });
await locator.screenshot({ path: 'button.png' });
const buffer = await page.screenshot();Page.screenshot!(page, path: "page.png", full_page: true)
Locator.screenshot!(locator, path: "button.png")
png = Page.screenshot!(page)Evaluate
await page.evaluate(() => document.title);
await page.evaluate(([a, b]) => a + b, [1, 2]);
await locator.evaluate(el => el.textContent);Page.evaluate!(page, "() => document.title")
Page.evaluate!(page, "([a, b]) => a + b", [1, 2])
Locator.evaluate!(locator, "el => el.textContent")