15 Jun, 2023
Some notes ...
Install NodeJS
Setup a Playwright project
Create and install:
mkdir play
cd play
npm init -y
npm init playwright@latest -y
npx playwright install
Delete the examples:
rm tests/*
rm -r tests-examples
Record a new test
npx playwright codegen
Copy code and simplify to tests/login.spec.js
:
test('test', async ({ page }) => {
await page.goto('https://example.com/users/login');
await page.getByLabel('Username*').fill('admin');
await page.getByLabel('Password*').fill(process.env['ADMIN_PASSWORD']);
await page.locator('form div').filter({ hasText: 'Log In' }).click();
});
Run
ADMIN_PASSWORD=xxx npx playwright test --project=chromium --headed
(Optionally) save your work using Git
git init
git add * .gitignore
git commit -m 'First test'
git branch -M main
Note how to manage downloaded files:
// Start waiting for download before clicking. Note no await.
const downloadPromise = page.waitForEvent('download');
await page.getByText('Download file').click();
const download = await downloadPromise;
// Wait for the download process to complete
console.log(await download.path());
// Save downloaded file somewhere
await download.saveAs('/path/to/save/download/at.txt');
If you need to process XLS sheets, might be worth looking at SheetJS.
Be the first to comment.
Copyright James Gardner 1996-2020 All Rights Reserved. Admin.