Skip to content

Instantly share code, notes, and snippets.

@BananaAcid
Last active January 21, 2025 14:45
Show Gist options
  • Save BananaAcid/f798fc50907a3abc7ec48dcfb39197c3 to your computer and use it in GitHub Desktop.
Save BananaAcid/f798fc50907a3abc7ec48dcfb39197c3 to your computer and use it in GitHub Desktop.
Setup for Web Testing (Puppeteer)
  1. download nodeJS
  2. create empty folder, open it with vscode
  3. vscode -> terminal
  4. npm init
  5. npm i puppeteer tsx

create a index.ts:

import test from 'node:test';
import puppeteer from 'puppeteer';


// Launch the browser and open a new blank page
const browser = await puppeteer.launch();
const page = await browser.newPage();

suite('do something', () => {
  test('set the url', async (t) => {


    // Navigate the page to a URL.
    await page.goto('https://developer.chrome.com/');

    // Set screen size.
    await page.setViewport({width: 1080, height: 1024});
  });

  test('do the stuff', async (t) => {

    // Type into search box.
    await page.locator('.devsite-search-field').fill('automate beyond recorder');

    // Wait and click on first result.
    await page.locator('.devsite-result-item-link').click();

    // Locate the full title with a unique string.
    const textSelector = await page
      .locator('text/Customize and automate')
      .waitHandle();
    const fullTitle = await textSelector?.evaluate(el => el.textContent);

    // Print the full title.
    console.log('The title of this blog post is "%s".', fullTitle);

    assert.strictEqual(1, 1);
  });

});


await browser.close();

run:

tsx --test-reporter=spec index.ts

or

node --test-reporter=spec index.mjs

More:

alternatives: selenium-webdriver and playwright

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment