playwright--抓取文本内容
https://playwright.dev/docs/running-tests
创建项目:
yarn create playwright运行:
yarn playwright test(运行端到端测试。)
yarn playwright test --project=chromium(仅在桌面Chrome上运行测试。)
yarn playwright test example(在特定文件中运行测试。)
yarn playwright test --debug(在调试模式下运行测试。)
yarn playwright codegen(使用Codegen自动生成测试。)
✔ Success! Created a Playwright Test project at /workspace/test_playwright
Inside that directory, you can run several commands:
yarn playwright test
Runs the end-to-end tests.
yarn playwright test --project=chromium
Runs the tests only on Desktop Chrome.
yarn playwright test example
Runs the tests in a specific file.
yarn playwright test --debug
Runs the tests in debug mode.
yarn playwright codegen
Auto generate tests with Codegen.
We suggest that you begin by typing: yarn playwright testAnd check out the following files:
- ./tests/example.spec.ts - Example end-to-end test
- ./tests-examples/demo-todo-app.spec.ts - Demo Todo App end-to-end tests
- ./playwright.config.ts - Playwright Test configuration Visit https://playwright.dev/docs/intro for more information. ✨ Happy hacking! 🎭
Done in 29.67s.
抓取内容(方法):https://playwright.bootcss.com/docs/assertions
const content = await page.textContent('nav:first-child');
expect(content).toBe('home');修改内容:(更换URL等)(tests/example.spec.ts)
import { test, expect } from '@playwright/test';
test('get page content', async ({ page }) => {
//await page.goto('https://playwright.dev/');
await page.goto('https://www.museumsusa.org/museums/?k=1271407%2cCategoryID%3a200050%3bState%3aCA%3bDirectoryID%3a200454#top');
nbsp; // Expect a title "to contain" a substring.
//await expect(page).toHaveTitle(/Playwright/);
nbsp; const content = await page.textContent('.itemGroup');
console.log(content)
// expect(content).toBe('home');
// create a locator
const getStarted = page.locator('text= Adobe Art Gallery');
nbsp; // Expect an attribute "to be strictly equal" to the value.
//await expect(getStarted).toHaveAttribute('href', '/docs/intro');
nbsp; // Click the get started link.
await getStarted.click();
// Expects the URL to contain intro.
//await expect(page).toHaveURL(/.*intro/);
});运行:yarn playwright test
会把div的class内容进行打印。
