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

Database Connection

Oracle Database

Since a test is a NodeJS application, it can use a database to get and save information. NodeJS has drivers for most database engines. In this example

First, we install an Oracle driver

    > npm i oracledb

This test will read info from a TEST table on an Oracle database that can be created with this script>

create table tests
(
    id NUMBER generated as identity,
    page VARCHAR2(255),
    expected VARCHAR2(255),
    time VARCHAR2(255)
);

create unique index tests_idx
    on tests (id);

and fill with a couple of web pages on the page field, and its expected title.

idpageexpectedtime
https://www.google.comGooglenullnull
https://insum.canullnullnull

Code

Then, create a test.

const pw = require("playwright");
const expect = require("chai").expect;
const oracledb = require("oracledb");

let browser, context, page, results;
//const db = sworm.db();

(async () => {
  browser = await pw.chromium.launch();
  context = await browser.newContext();
  page = await context.newPage();
  connection = await oracledb.getConnection({
    user: "mateo",
    password: "mateosanroman",
    connectString: "localhost/XE",
  });

  console.log("Connection was successful!");

  results = await connection.execute(
    `SELECT *
       FROM TESTS`,
    [],
    {
      outFormat: oracledb.OUT_FORMAT_OBJECT,
    }
  );

  for (const result of results.rows) {
    console.log("Checking " + result.PAGE);
    try {
      let start = process.hrtime();
      await page.goto(result.PAGE);
      let title = await page.title();
      let diff = process.hrtime(start);
      expect(title).to.equal(result.EXPECTED);
      diff = diff[0] + "." + diff[1];
      //await connection.execute("alter session set current_schema = MATEO");
      console.log(result);
      let r = await connection.execute(
        `UPDATE tests SET TIME = :diff WHERE ID = :id`,
        [diff, result.ID],
        { autoCommit: true }
      );
      console.log(r);
    } catch (e) {
      console.log(e);
    }
  }
  await page.close();
  await context.close();
  await browser.close();
})();

Explanation

First, the test creates a new Oracle connection

  connection = await oracledb.getConnection({
    user: "mateo",
    password: "mateosanroman",
    connectString: "localhost/XE",
  });

Then, we query it for the test cases

  results = await connection.execute(
    `SELECT *
       FROM TESTS`,
    [],
    {
      outFormat: oracledb.OUT_FORMAT_OBJECT,
    }
  );

And then we iterate for each case and test it:

  for (const result of results.rows) {
    console.log("Checking " + result.PAGE);
    try {
        ...
    }
  }

When a page is successfully tested, its latency is saved into the db field:

  let r = await connection.execute(
    `UPDATE tests SET TIME = :diff WHERE ID = :id`,
    [diff, result.ID],
    { autoCommit: true }
  );
← Using SQLEmail Setup →
  • Oracle Database
    • Code
    • Explanation