Home Blog CV Projects Patterns Notes Book Colophon Search

Playwright Codgen

15 Jun, 2023

Some notes ...

  1. Install NodeJS

  2. 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
  3. Record a new test

    npx playwright codegen
  4. 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();
  5. Run

    ADMIN_PASSWORD=xxx npx playwright test --project=chromium --headed
  6. (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.

Add Comment

Copyright James Gardner 1996-2020 All Rights Reserved. Admin.