Skip to content

A bare bones project to demonstrate a basic automated end to end test case using Puppeteer, Jest, and jest-html-reporter.

Notifications You must be signed in to change notification settings

dnewingh/jest-puppeteer-starter-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

This is a bare bones project to demonstrate a basic automated end to end test case using Puppeteer, Jest, and jest-html-reporter.

Installation

Clone the repository and install dependencies.

git clone https://github.com/dnewingh/jest-puppeteer-starter-project.git
npm install

Skip ahead to the Usage section to give it a try.

Alternative Installation via Manual Setup

As with every JavaScript project you'll need an NPM environment (make sure to have Node installed on your system). Create a new folder and initialize the project with:

mkdir jest-puppeteer-starter-project && cd $_
npm init -y

Next, install Jest, jest-html-reporter, and Puppeteer with:

npm i jest jest-html-reporter puppeteer --save-dev

Let's also configure an NPM script for running our tests from the command line. Open up package.json and configure a script named test for running Jest:

  "scripts": {
    "test": "jest"
  },

Finally configure Jest to format test results as HTML with the following file.

jest.config.json

{
	"testResultsProcessor": "./node_modules/jest-html-reporter"
}

Writing tests using Puppeteer

Jest will automatically run any files with *.test.js in the filename or that are placed in a __tests__ directory. The test below launches an incognito instance of the browser, navigates to a known endpoint, and checks for the presence of an expected text value.

newsPage.test.js

const puppeteer = require('puppeteer');

describe("News page end to end test", () => {
  test('Verify element text = Hacker News', async () => {
    const browser = await puppeteer.launch({
        headless: false,
        slowMo: 40,
        args: ['--window-size=1920,1080']
    });
    try {
        const context = await browser.createIncognitoBrowserContext();
        const page = await context.newPage();          
        await page.goto('https://news.ycombinator.com/news');
        const elementText = await page.$eval('.hnname > a', el => el.innerText);
        expect(elementText).toBe('Hacker News');        
    } 
    catch (error) {
        console.log(error);
    } 
    finally {
        await browser.close();
    }
  }, 3000);
});

Usage

After installation, give it a shot with:

npm test

A new file test-report.html will be generated in the project root directory. Open this file in your browser to view the test results summary!

Troubleshooting

Note: Puppeteer requires additional dependencies to be installed on amazon-linux. Please refer to the following: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#running-puppeteer-on-aws-ec2-instance-running-amazon-linux

Inspiration

Thanks to all these great examples.

License

MIT

About

A bare bones project to demonstrate a basic automated end to end test case using Puppeteer, Jest, and jest-html-reporter.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published