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

Email Setup

Email Setup

NodeJS can also be configured for sending mails. Using the previous recipe, we can enhance it by sending mails.

First, we install NodeMailer

    > npm i nodemailer

.env File

Then, we will install dotenv, will allow us to use enviroment variables set in a .dev file

    > npm i dotenv

Create a .env file

    MAIL_USERNAME="mail"
    MAIL_PASSWORD="password"

Code

Then, create a test.

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

let browser, context, page, results, transporter;
//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",
  });
  transporter = nodemailer.createTransport({
    host: "smtp-mail.outlook.com",
    port: 587, // port for secure SMTP
    secureConnection: false,
    //ignoreTLS: true,
    tls: {
      ciphers: "SSLv3",
    },
    auth: {
      user: process.env.MAIL_USERNAME, 
      pass: process.env.MAIL_PASSWORD, 
    },
  });
  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) {
      let mailOptions = {
        //from: `BOT <${process.env.MAIL_USERNAME}>`, // sender address (who sends)
        //from: '"BOT " <msanroman@legado.gob.pe>', // sender address (who sends)
        from: `BOT ${process.env.MAIL_USERNAME}`,
        //to: 'mymail@mail.com, mymail2@mail.com', // list of receivers (who receives)
        to: "msroman@insum.ca", // list of receivers (who receives)
        subject: "Page failed", // Subject line
        text: "Page failed: " + result.PAGE, // plaintext body
      };
      console.log("Sending mail...");
      transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
          return console.log(error);
        }
        console.log("Message sent!");
      });
    }
  }
  await page.close();
  await context.close();
  await browser.close();
})();

Explanation

First, we create a email transporter, which setup a mail server

    transporter = nodemailer.createTransport({
      host: "smtp-mail.outlook.com",
      port: 587, // port for secure SMTP
      secureConnection: false,
      //ignoreTLS: true,
      tls: {
        ciphers: "SSLv3",
      },
      auth: {
        user: process.env.MAIL_USERNAME, 
        pass: process.env.MAIL_PASSWORD, 
      },
    });

When a test fails, we create an email body and send an email:

      let mailOptions = {
        //from: `BOT <${process.env.MAIL_USERNAME}>`, // sender address (who sends)
        //from: '"BOT " <msanroman@legado.gob.pe>', // sender address (who sends)
        from: `BOT ${process.env.MAIL_USERNAME}`,
        //to: 'mymail@mail.com, mymail2@mail.com', // list of receivers (who receives)
        to: "msroman@insum.ca", // list of receivers (who receives)
        subject: "Page failed", // Subject line
        text: "Page failed: " + result.PAGE, // plaintext body
      };
      console.log("Sending mail...");
      transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
          return console.log(error);
        }
        console.log("Message sent!");
      });
← Database Connection
  • Email Setup
    • .env File
    • Code
    • Explanation