HOV QA Engineers
  • ๐Ÿ”HOV QA Engineers
  • AGILE TESTING
    • ๐Ÿ’ปCSS elements for QA
    • ๐Ÿ›ซOn-boarding QA
    • ๐ŸคTesters Communications, Task and Responsibilities
    • โ™ป๏ธSoftware Testing Life cycle
    • ๐Ÿ““Test Plan
    • ๐ŸŽŸ๏ธKanban board and ticket flows
    • ๐Ÿ““User Stories and Acceptance Criteria
    • ๐Ÿ‘ฃGherkin syntax
    • ๐Ÿค–Test Design
    • ๐Ÿ“…Test Strategy
    • ๐Ÿ‘ฌTypes of Testing strategy
    • ๐ŸงชTesting types
    • ๐Ÿ›Bug and Bug life Cycle
    • โœ๏ธManual Testing
    • Automation testing
    • ๐Ÿ‘ทโ€โ™‚๏ธE2E Testing
    • E2E testing best practices
    • Accessibility testing
    • Performance testing
    • Mobile Testing
  • Tools and Guidelines
    • Software QA Engineer Roadmap
    • ๐Ÿ’ปSetup Cypress v10 E2E testing
    • ๐ŸงชSetup End to end test with Cypress test suite
    • Setup Performance test with k6
    • API testing with postman
    • Building GitHub Action part 1
    • โœ๏ธPlaywright + Cucumber
  • Training Videos
    • SQA Trainings
  • Research
    • ๐Ÿ“ฑMobile Automation with Appium
      • Setup Project and Configuration (Android)
    • ๐ŸŽญPlaywright - Web Apps E2E Testing Tool
      • Setup Project and Configuration (Android)
    • Testing Library
  • PROJECTS
    • Page 2
    • Opexa
    • ๐Ÿ“ฃIdentifi
      • โš–๏ธSQA Metrics and Testing progress
      • ๐ŸงชManual testing
        • WEB
          • Sanity Testing
          • Regression Test cases
          • Credentials and URLs
        • Android
          • Sanity Testing
          • Regression Testing
          • End to end Testing
          • Credentials
        • IOS
          • Sanity Testing
          • Regression Testing
      • ๐Ÿ“”API Testing
      • ๐Ÿค–Web Automation
        • E2E Automation Test Plan
        • Web Automation Setup
          • ๐ŸšงSetup WSL2 Environment for Windows
          • ๐Ÿ—๏ธSetup local Environment (Linux/Ubuntu)
        • Regression Testing Coverage
        • Sanity Testing Coverage
      • ๐ŸŽญPerformance Testing
        • K6 Test Runs
      • ๐Ÿ“ดMobile Automation
        • Mobile Automation Test Plan
        • Identifi Mobile Automation Setup
      • Page 1
    • ๐Ÿ–ผ๏ธsubsit
      • Test plan
      • Smoke Test cases
      • ๐ŸงชTest Scenarios
      • ๐Ÿ•ธ๏ธWeb Automation
    • ๐ŸงตThreadSync
      • Test Plan
    • ๐Ÿ‘๏ธUpWatch
      • Product Requirement
      • Test plan
      • Monitoring & Bug Reporting
      • E2E Test
        • E2E UpWatch Test
      • E2E QA Automation
    • ๐ŸŽฒWallet
      • ๐ŸงฌTest Plan
      • ๐Ÿ’ปE2E Wallet Automation Test Plan
      • ๐Ÿ“–E2E Test Automation Docs
      • ๐Ÿ“‘Credentials| Urls
      • ๐Ÿ“šWallet Feature List
    • ๐Ÿ‘จโ€๐Ÿ’ปDevLuvs
      • ๐ŸงชTest Cases
      • ๐Ÿ”‘Credentials For Automation
        • ๐Ÿค–Automation Test Cases
      • ๐Ÿ“ŠAutomation Board
    • โš™๏ธMehira
      • ๐Ÿ•ธ๏ธWeb Automation
      • Sanity Testing Document
      • How to start running Mehira application from your local using Docker engine via Ubuntu platform
Powered by GitBook
On this page
  • How to Setup:
  • Features:
  • World
  • Steps:
  • Steps execution:
  • Reporting:
  • Attach screenshot for failure:
  1. Tools and Guidelines

Playwright + Cucumber

PreviousBuilding GitHub Action part 1NextSQA Trainings

Last updated 2 years ago

โ€‹Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API. Playwright is built to enable cross-browser web automation that is ever-green, capable, reliable and fast.

Cucumber is a tool that supports (BDD), If youโ€™re new to Behaviour-Driven Development read first.โ€‹

How to Setup:

Run the command in the terminal:

npm init

Install playwright:

npm i playwright

This command will install the playwright library and browser dependencies required for testing.

npm i @playwright/test

This command will install the modules required for validations with built-in automatic waits.

Install cucumber:

npm install @cucumber/cucumber

This command will install the Cucumber, a tool for running automated tests written in plain language.

Install typescript:

npm i typescript
npm i ts-node

This command will install the typescript dependencies to support with node.

After installing, go to package.json and your package.json must look similar to the below picture:

Features:

Features files are the test case files written in Gherkin language, which explains the motive of the tests in plain English, making it easier for non-technical people to understand the context of the test.

Letโ€™s create a sample feature using Gherkin keywords and derive the logic using Playwright.

  • Create a Feature folder

  • A feature file should always end with a .feature extension.

  • Add feature and scenario details as per the below image, based on the demo siteโ€™s functionality.

World

The word โ€˜Worldโ€™ describes the steps where we can declare and invoke the objects/variables that can be used globally. The Java/C# version of Cucumber is said to be Hooks.

Hooks are primarily used to specify pre and post-conditions globally across the features. Here, we will create two global variables, one for the browser and another for the page.

The โ€˜Beforeโ€™ and โ€˜Afterโ€™ functions launch the app before each scenario and then kill it. The goal is to make the scenarios independent. The Before function captures the browser and page variables from the initializer and exports.

  • Create a file with the name โ€˜world.tsโ€™ under the steps folder.

  • Initialize the browser and page variables.

  • Set the default timeout at 60 seconds (default waits till promise gets resolved).

  • Create a โ€˜Beforeโ€™ function to launch the chromium browser and assign the session id to the browser object.

  • Create a browser context and assign it to the page variable.

  • Navigating the URL is specified here as part of the precondition.

Add this code to world.ts

import { After, Before, setDefaultTimeout, Status } from "@cucumber/cucumber";
import { Browser, chromium, Page } from "playwright";

const baseURL =
  "https://4h12dnftgp7hp.a9ni7d1ff3m0e.ap-southeast-1.cs.amazonlightsail.com";

let page: Page;
let browser: Browser;
setDefaultTimeout(60000);

Before(async () => {
  try {
    browser = await chromium.launch({ headless: true });
    const context = await browser.newContext();
    page = await context.newPage();
    await page.goto(
      "https://4h12dnftgp7hp.a9ni7d1ff3m0e.ap-southeast-1.cs.amazonlightsail.com/"
    );
    console.log(`${await page.title()}`);
  } catch (error) {
    throw new Error(`chrome navigation throws an error ${error}`);
  }
  return page;
});

After(async function (Scenario) {
  if (Scenario.result!.status === Status.FAILED) {
    const randomNo = Math.floor(Math.random() * 100);
    this.attach(
      await page.screenshot({
        path: `./Screenshots/${Scenario.pickle.name}${randomNo}.png`,
        fullPage: true,
      }),
      "image/png"
    );
    await browser.close();
  }
  await browser.close();
});

export { page, browser, baseURL };

Steps:

Steps from the feature file have to be implemented as definitions to specify your business logic.

Gherkinโ€™s steps from the features are initially considered undefined by Cucumber, and when running the script command defined in package.json, Cucumber generates the undefined snippets which could be used in the steps file instead of writing them, which saves time for us.

Firstly, Cucumber needs to know where to look for the steps to notify us with the snippets of unimplemented ones (In case already implemented, it will directly run the matching step).

The below procedure helps you to run the script and get the status from Cucumber.

  • Create a file named cucumber.js to define the Cucumber options where the path to the feature and step are defined.

Declare the options and export them as a module with the name โ€˜test_runner.โ€™

Add the code below to cucumber.js

let options = [
  '--require-module ts-node/register',
  '--require ./steps/*.step.ts',
  '--format progress',
  '--format json:Reports/cucumberReport.json',
].join(' ');

let run_features = ['./features/', options].join(' ');

module.exports = {
  test_runner: run_features,
};

Now navigate to package.json and add this to script > test:

"scripts": {
    "test": "npx cucumber-js -p test_runner"
  },

Now run the โ€œnpm testโ€

Notice that you will see this result:

It shows undefined because thereโ€™s no steps yet for the signup.feature.

Add this to the signup.step.ts under step folder:

import { Given, When, Then } from "@cucumber/cucumber";
import { page, baseURL } from "../steps/world";
import { expect } from "@playwright/test";

Given("user is on the signup page", async () => {
  await page.goto(baseURL + `/signup/`);
  await expect(page.locator("h2")).toContainText("Create an Account");
});

// Scenario: User signup already registered account
When("user inputs all the given field", async () => {
  const randomNo = Math.floor(Math.random() * 4); // pick random number from 0 to 4
  const addRole = page.locator("ul[role='listbox'] li");

  await page.locator("input[name='emailAddress']").type("sqa.hov@gmail.com");
  await page.locator("input[name='company']").type("Testing");
  await page.locator(".chakra-input__right-element").click();
  await addRole.nth(randomNo).click();

  await page.locator("button[type='submit']").click();
});

Then("user should see {string} status", async (status: string) => {
  await expect(page.locator("div[role='status'] div span")).toContainText(
    status
  );
});

Steps execution:

We are now done installing the packages and creating some scripts.

To run all tests:

npm test

To run specific test:

npm test <nameOfTheFeature>.feature

npm test signup.feature

Reporting:

Though we have results displayed in the terminal, a report implementation is required to share the results as an html file.

Cucumber provides an html reporter plugin to generate the reports based on Gherkinโ€™s steps, which can be customized using metadata.

Install the following:

npm i cucumber-html-reporter

npm i @types/cucumber-html-reporter

To confirm your installation, go to package.json.

Add a file โ€˜htmlReportGenerator.jsโ€™ under the root folder to define the report options.

const reporter = require('cucumber-html-reporter');

var date = new Date();
var currentDate =
  date.getDate() +
  '_' +
  (date.getMonth() + 1) +
  '_' +
  date.getFullYear() +
  '_' +
  date.getHours() +
  '_' +
  date.getMinutes() +
  '_' +
  date.getSeconds();

var options = {
  brandTitle: 'e2e testing: Subsit',
  theme: 'bootstrap',
  jsonFile: 'Reports/cucumberReport.json',
  output: 'Reports/cucumber_report_' + currentDate + '.html',
  screenshotsDirectory: './Screenshots/',
  storeScreenshots: true,
  reportSuiteAsScenarios: true,
  launchReport: true,
  metadata: {
    'App Version': '1.1.1',
    'Test Environment': 'QA',
    Platform: 'Web',
  },
};

reporter.generate(options);

To store the json data, create a folder name Reports. Under reports folder create cucumber_report.json

Navigate to cucumber.js and add the below option to format json dataโ€™โ€“ format json:./ Reports/cucumber_report.json.โ€™

To run the report:

node htmlReportGenerator.js

Sample image for report:

Attach screenshot for failure:

Navigate to steps> world.ts. Add this to the After function

After(async function (Scenario) {
  if (Scenario.result!.status === Status.FAILED) {
    const randomNo = Math.floor(Math.random() * 100);
    this.attach(
      await page.screenshot({
        path: `./Screenshots/${Scenario.pickle.name}${randomNo}.png`,
        fullPage: true,
      }),
      'image/png',
    );
    await browser.close();
  }
  await browser.close();
});ty
โœ๏ธ
Behaviour-Driven Development
BDD introduction