Powrót do: Playwright Elements – Kluczowe koncepcje automatyzacji testów
Nagrywanie testów, a lokatory
Prezentacja
Dodatkowe materiały
TIP: Cały kod testów z poszczególnych lekcji znajdziesz w specjalnie przygotowanym repozytorium:
👉jaktestowac/playwright-elements-locators
👉jaktestowac/playwright-elements-locators
TIP: Ta lekcja jest częścią rozwijanego Programu Testy Automatyczne z Playwright 🎭
Poniżej wykonaliśmy te same kroki na stronie /practice/simple-elements.html oraz /practice/simple-elements-no-ids.html.
W wersji 1.20:
import { test, expect } from "@playwright/test";
test.describe("Finding different elements using Playwright 1.20.0 codegen", () => {
test("Test page with IDs generated by codegen", async ({ page }) => {
// steps:
await page.goto("http://localhost:3000/practice/simple-elements.html");
await page.locator('[data-testid="dti-label-element"]').click();
await page.locator('[data-testid="dti-button-element"]').click();
await page.locator('[data-testid="dti-checkbox"]').check();
await page.locator('[data-testid="dti-input"]').click();
await page.locator('[data-testid="dti-input"]').fill("test input");
await page.locator('[data-testid="dti-input"]').press("Enter");
await page.locator('[data-testid="dti-textarea"]').click();
await page.locator('[data-testid="dti-textarea"]').fill("test area");
await page.locator('[data-testid="dti-dropdown"]').selectOption("option2");
await page.locator('[data-testid="dti-radio1"]').check();
await page.locator('[data-testid="dti-radio2"]').check();
await page.locator('[data-testid="dti-radio3"]').check();
await page.locator('[data-testid="dti-range"]').click();
});
test("Test page with no IDs generated by codegen", async ({ page }) => {
// steps:
await page.goto(
"http://localhost:3000/practice/simple-elements-no-ids.html"
);
await page.locator("text=Some text for label").click();
await page.locator("text=Click me!").click();
await page.locator('input[type="checkbox"]').check();
await page.locator('input[type="text"]').click();
await page.locator('input[type="text"]').fill("test input");
await page.locator('input[type="text"]').press("Enter");
await page.locator("textarea").click();
await page.locator("textarea").fill("test area");
await page.locator('select[name="name-dropdown"]').selectOption("option2");
await page.locator('input[name="name-radio"]').first().check();
await page.locator('input[name="name-radio"]').nth(1).check();
await page.locator('input[name="name-radio"]').nth(2).check();
await page.locator('input[type="range"]').click();
});
});
W wersji 1.27:
import { test, expect } from "@playwright/test";
test.describe("Finding different elements using Playwright 1.27.0 codegen", () => {
test("Test page with IDs generated by codegen", async ({ page }) => {
// steps:
await page.goto("http://localhost:3000/practice/simple-elements.html");
await page.getByTestId("dti-label-element").click();
await page.getByTestId("dti-button-element").click();
await page.getByTestId("dti-checkbox").check();
await page.getByTestId("dti-input").click();
await page.getByTestId("dti-input").fill("test input");
await page.getByTestId("dti-input").press("Enter");
await page.getByTestId("dti-textarea").click();
await page.getByTestId("dti-textarea").fill("test area");
await page.getByTestId("dti-dropdown").selectOption("option2");
await page.getByTestId("dti-radio1").check();
await page.getByTestId("dti-radio2").check();
await page.getByTestId("dti-radio3").check();
await page.getByTestId("dti-range").click();
});
test("Test page with no IDs generated by codegen", async ({ page }) => {
// steps:
await page.goto(
"http://localhost:3000/practice/simple-elements-no-ids.html"
);
await page.getByText("Some text for label").click();
await page.getByRole("button", { name: "Click me!" }).click();
await page.locator('input[type="checkbox"]').check();
await page.locator('input[type="text"]').click();
await page.locator('input[type="text"]').fill("test input");
await page.locator('input[type="text"]').press("Enter");
await page.locator("textarea").click();
await page.locator("textarea").fill("test area");
await page.locator('select[name="name-dropdown"]').selectOption("option2");
await page.locator('input[name="name-radio"]').first().check();
await page.locator('input[name="name-radio"]').nth(1).check();
await page.locator('input[name="name-radio"]').nth(2).check();
await page.locator('input[type="range"]').click();
});
});
W wersji 1.32:
import { test, expect } from "@playwright/test";
test.describe("Finding different elements using Playwright 1.32.0 codegen", () => {
test("Test page with IDs generated by codegen", async ({ page }) => {
// steps:
await page.goto("http://localhost:3000/practice/simple-elements.html");
await page.getByTestId("dti-label-element").click();
await page.getByTestId("dti-button-element").click();
await page.getByTestId("dti-checkbox").check();
await page.getByTestId("dti-input").click();
await page.getByTestId("dti-input").fill("test input");
await page.getByTestId("dti-input").press("Enter");
await page.getByTestId("dti-textarea").click();
await page.getByTestId("dti-textarea").fill("test area");
await page.getByTestId("dti-dropdown").selectOption("option2");
await page.getByTestId("dti-radio1").check();
await page.getByTestId("dti-radio2").check();
await page.getByTestId("dti-radio3").check();
await page.getByTestId("dti-range").click();
});
test("Test page with no IDs generated by codegen", async ({ page }) => {
// steps:
await page.goto(
"http://localhost:3000/practice/simple-elements-no-ids.html"
);
await page.getByText("Some text for label").click();
await page.getByRole("button", { name: "Click me!" }).click();
await page.getByRole("checkbox").check();
await page.locator('input[type="text"]').click();
await page.locator('input[type="text"]').fill("test input");
await page.locator('input[type="text"]').press("Enter");
await page.locator("textarea").click();
await page.locator("textarea").fill("test area");
await page.getByRole("combobox").selectOption("option2");
await page.getByRole("radio").first().check();
await page.getByRole("radio").nth(1).check();
await page.getByRole("radio").nth(2).check();
await page.getByRole('slider').click();
});
});
W wersji 1.45:
import { test, expect } from "@playwright/test";
test.describe("Finding different elements using Playwright 1.45.1 codegen", () => {
test("Test page with IDs generated by codegen", async ({ page }) => {
// steps:
await page.goto("http://localhost:3000/practice/simple-elements.html");
await page.getByTestId("dti-label-element").click();
await page.getByTestId("dti-button-element").click();
await page.getByTestId("dti-checkbox").check();
await page.getByTestId("dti-input").click();
await page.getByTestId("dti-input").fill("test input");
await page.getByTestId("dti-input").press("Enter");
await page.getByTestId("dti-textarea").click();
await page.getByTestId("dti-textarea").fill("test text area");
await page.getByTestId("dti-dropdown").selectOption("option2");
await page.getByTestId("dti-radio1").check();
await page.getByTestId("dti-radio2").check();
await page.getByTestId("dti-radio3").check();
await page.getByTestId("dti-range").fill("59");
});
test("Test page with no IDs generated by codegen", async ({ page }) => {
// steps:
await page.goto(
"http://localhost:3000/practice/simple-elements-no-ids.html"
);
await page.getByText("Some text for label").click();
await page.getByRole("button", { name: "Click me!" }).click();
await page.getByRole("checkbox").check();
await page.locator('input[type="text"]').click();
await page.locator('input[type="text"]').fill("test input");
await page.locator('input[type="text"]').press("Enter");
await page.locator("textarea").click();
await page.locator("textarea").fill("test area");
await page.getByRole("combobox").selectOption("option2");
await page.getByRole("radio").first().check();
await page.getByRole("radio").nth(1).check();
await page.getByRole("radio").nth(2).check();
await page.getByRole("slider").fill("66");
});
});
Zasoby i dokumentacja
- Aplikacja do testów: lekcja o pobraniu, instalacji i uruchomieniu
- Oficjalna dokumentacja o lokatorach – locators
- Oficjalna dokumentacja o codegen (narzędziu do nagrywania i generowania testów w Playwright) – Test generator






