PlaywrightJS Tutorial

PlaywrightJS Tutorial

  • Docs

›Cookbook

Setup

  • PlaywrightJS Installation
  • Install Mocha and Chai
  • Installation Test

First Steps

  • Create a PlaywrightJS Test
  • Using Mocha and Chai

The Playwright Library

  • The Browser Object
  • The Context Object
  • The Page Object
  • Selectors
  • Navigation
  • Interactions
  • Auditing

Using PlayWright on web pages

  • A real world case

Code Generation

  • Code Generation

Cookbook

  • Timers
  • Using SQL
  • Database Connection
  • Email Setup

Using SQL

SQL

Since the test file is a NodeJS script, it can use any NodeJS library to do any task required. In this case, a SQLite database is used to persist data.

First, SQLite must be installed:

> npm i sqlite3

Then, it can be required on test:

An empty database file has to be created. In this example, a ./db/test.db is created.

const pw = require('playwright');
const assert = require('assert');
const sqlite3 = require('sqlite3').verbose();


(async () => {
    const db = new sqlite3.Database('./db/test.db', (err) => {
      if (err) {
        return console.error(err.message);
      }
      console.log('Connected to TEST database.');
    });
    try {
        db.serialize(() => {
            db.each(`CREATE TABLE IF NOT EXISTS benchmark (id INTEGER PRIMARY KEY AUTOINCREMENT, time1 VARCHAR(255), time2 VARCHAR(255))`);
        })
    }
    catch (e) {
        console.log(e)
    }
    const browser = await pw.chromium.launch();
    const context = await browser.newContext();
    const page = await context.newPage();
    for (let index = 0; index < 10; index++) {
        let start = process.hrtime();
        await page.goto('http://leafground.com/');

        let title = await page.title();
        try {
            await assert.strictEqual(title, 'TestLeaf - Selenium Playground', 'Title match!');
            let end = process.hrtime(start);
            let start2 = process.hrtime();
            let end2 = process.hrtime(start2);
            db.serialize(() => {
                db.each(`INSERT INTO benchmark(time1, time2) VALUES('${end}', '${end2}')`);
            })
        }
        catch (e) {
            console.log(e);
        }


    }
    db.all(`SELECT * from benchmark`, [], (err, rows) => {
      if (err) {
        throw err;
      }
      rows.forEach((row) => {
        console.log('Time 1: ' + row.time1 + ' Time 2: ' + row.time2);
      });
    });
    await page.close();
    await context.close();
    await browser.close();
    db.close();

})();

Once started, a database connection is made:

    const db = new sqlite3.Database('./db/test.db', (err) => {
      if (err) {
        return console.error(err.message);
      }
      console.log('Connected to TEST database.');
    });

And a table is created if it doesn't exist yet:

    db.serialize(() => {
        db.each(`CREATE TABLE IF NOT EXISTS benchmark (id INTEGER PRIMARY KEY AUTOINCREMENT, time1 VARCHAR(255), time2 VARCHAR(255))`);
    })

The db.serialize function ensures every operation in it is done sequentially. The SQL are standard SQL-92 commands.

Data is inserted:

    db.each(`INSERT INTO benchmark(time1, time2) VALUES('${end}', '${end2}')`);

And finally, all data is shown:

    db.all(`SELECT * from benchmark`, [], (err, rows) => {
      if (err) {
        throw err;
      }
      rows.forEach((row) => {
        console.log('Time 1: ' + row.time1 + ' Time 2: ' + row.time2);
      });
    });

You can also get the data and use it for some other test, since it is persisting on the database.

You can also use any database like MySQl, Oracle, or MS SQL with the proper NodeJS libraries

← TimersDatabase Connection →
  • SQL