GithubHelp home page GithubHelp logo

choznerol / puppeteer-jest-starter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from codementorio/puppeteer-jest-starter

0.0 1.0 0.0 318 KB

A starter-kit quipped with the minimal requirements for Puppeteer + Jest, making E2E testing a breeze.

Dockerfile 7.66% JavaScript 92.34%

puppeteer-jest-starter's Introduction

Puppeteer-Jest Starter

CircleCI

A starter-kit quipped with the minimal requirements for Puppeteer + Jest, making E2E testing a breeze.

Kicking off:

Install dependencies:

$ npm i

Run the test:

$ npm start

Run test test in headed Chrome:

$ npm start:headed

What's inside:

global.page

  • provide wrapped global.page instance so that you can start writting your test case immediately.

Screenshot for each failed test case

  • A screenshot will be taken for each failed test case, put in screenshots folder. (/tmp/screenshots if env.CI)
  • Each file will be named by the spec description

Failure details for each failed test case

  • A json file named by the spec description will be generated for each failed test case. Contents in the file include:
    • The current page url
    • Spec description
    • console messages

Multiple Browsers

Sometimes, we'd like to manipulate more than on browsers in a test case. For example, testing two sided chat. We can do that by:

const { getPageFromBrowser } = require('lib/browserStore')

describe('Demo', () => {
  it('manipulates multiple browsers', async ()=> {
    const page2 = await getPageFromBrowser('page2')

    // do something with `global.page` here
    // and do some other thing to `page2` here

    await page.goto('...')
    await page2.goto('...')
  })
})

where the getPageFromBrowser works as a browser store providing multiple browser references by browserName:

page1 = await getPageFromBrowser('the-name')
page2 = await getPageFromBrowser('the-name')

page1 === page2 // true

Wrapped global.page with convenient default options and behaviors which can be overwritten easily when needed

For example, global.page.goto is with default option waitUntil: networkidle0 and fails with readable erorr message if the returning status code is not 200. To overwrite it, just

global.page.goto({
  waitUntil: 'my-other-desired-options',
  myOtherKey: 'my-other-value'
})

The default options/overwritten methods can be modified in lib/pageWrapper.

Default options/behavior in global.page

  • page.goto: waitUntil: 'networkidle0', guard status 200
  • page.waitForSelector: visible: true
  • page.click: First waitForSelector with visible: true then click

Make it easy to decouple test case with UI detail

In e2e testing, it's easy to scatter css selectors all around the code base. This makes maintainance when UI changes a nightmare. Here we use window driver layer to mitigate this issue and make the error message more readable at the same time:

before

it('is nasty', async () => {
  // do payment
  await page.click('.my-first-css-selector')
  await page.click('.my-another-css-selector')
  await page.click('.my-yet-another-css-selector')
  await page.click('.this-drove-me-crazy')
})

after

const { getPageFromBrowser } = require('lib/browserStore')

function createPageDriver (page) {
  return wrapErrorHandler({
    async doPayment() {
      // when UI implementation changed, change here
      await page.click('.my-first-css-selector')
      await page.click('.my-another-css-selector')
      await page.click('.my-yet-another-css-selector')
      await page.click('.this-drove-me-crazy')
    },
  }, 'MyDemoPage')
}

it('is much better', async () => {
  // when UI implementation changed, this part won't change if the business logic remains the same
  const driver = createPageDriver(page)
  await page.doPayment()
})

By adding a layer between business logic and UI details, we can abstract out the UI implementation detail there.

CI setup

CircleCI config is included!

puppeteer-jest-starter's People

Contributors

mz026 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.