├── index.js
├── .gitignore
├── config.js
├── jest.config.js
├── google.test.js
├── setup
├── teardown.js
├── setup.js
├── print.js
└── puppeteer_environment.js
├── package.json
├── __tests__
└── test1.js
├── README.md
└── LICENSE.md
/index.js:
--------------------------------------------------------------------------------
1 | console.log('run "yarn test"')
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | .history
3 | *.lock
4 | .DS_Store
5 | package-lock.json
--------------------------------------------------------------------------------
/config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | isEnvironmentMessages:false,
3 | globalTimeout:5000
4 | };
--------------------------------------------------------------------------------
/jest.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | globalSetup: './setup/setup.js',
3 | globalTeardown: './setup/teardown.js',
4 | testEnvironment: './setup/puppeteer_environment.js',
5 | };
--------------------------------------------------------------------------------
/google.test.js:
--------------------------------------------------------------------------------
1 | /* global describe, beforeAll, page, it, expect */
2 |
3 | describe('Google', () => {
4 | beforeAll(async () => {
5 | page = await global.__BROWSER__.newPage()
6 | await page.goto('https://google.com')
7 | })
8 |
9 | it('should be titled "Google"', async () => {
10 | await expect(page.title()).resolves.toMatch('Google')
11 | })
12 | })
13 |
--------------------------------------------------------------------------------
/setup/teardown.js:
--------------------------------------------------------------------------------
1 | // teardown.js
2 | const print = require('./print');
3 | const os = require('os');
4 | const rimraf = require('rimraf');
5 | const path = require('path');
6 |
7 | const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
8 | module.exports = async function() {
9 | // close the browser instance
10 | print('Teardown Puppeteer','green');
11 | await global.__BROWSER_GLOBAL__.close();
12 |
13 | // clean-up the wsEndpoint file
14 | rimraf.sync(DIR);
15 | };
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "jest-puppeteer-test-automation",
3 | "version": "0.0.1",
4 | "description": "Sample repo for ui testing",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "jest"
8 | },
9 | "keywords": [
10 | "jest",
11 | "puppeteer",
12 | "test"
13 | ],
14 | "author": "mustafa ilhan",
15 | "license": "MIT",
16 | "devDependencies": {
17 | "chalk": "^2.4.2",
18 | "jest": "24.9.0",
19 | "jest-puppeteer": "4.3.0",
20 | "puppeteer": "1.20.0",
21 | "rimraf": "^3.0.0"
22 | },
23 | "jest": {
24 | "preset": "jest-puppeteer"
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/__tests__/test1.js:
--------------------------------------------------------------------------------
1 | const { globalTimeout } = require('../config');
2 | const timeout = globalTimeout // 5000
3 |
4 | describe(
5 | '/ (Home Page)',
6 | () => {
7 | let page
8 | beforeAll(async () => {
9 | page = await global.__BROWSER__.newPage()
10 | await page.goto('https://google.com')
11 | }, timeout)
12 |
13 | afterAll(async () => {
14 | await page.close()
15 | })
16 |
17 | it('should load without error', async () => {
18 | let text = await page.evaluate(() => document.body.textContent)
19 | expect(text).toContain('google')
20 | })
21 | },
22 | timeout
23 | )
24 |
--------------------------------------------------------------------------------
/setup/setup.js:
--------------------------------------------------------------------------------
1 | // setup.js
2 | const print = require('./print')
3 | const puppeteer = require('puppeteer');
4 | const mkdirp = require('mkdirp');
5 | const path = require('path');
6 | const fs = require('fs');
7 | const os = require('os');
8 | const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
9 |
10 | module.exports = async function() {
11 | print('Setup Puppeteer',"green")
12 | const browser = await puppeteer.launch();
13 | // store the browser instance so we can teardown it later
14 | // this global is only available in the teardown but not in TestEnvironments
15 | global.__BROWSER_GLOBAL__ = browser;
16 |
17 | // use the file system to expose the wsEndpoint for TestEnvironments
18 | mkdirp.sync(DIR);
19 | fs.writeFileSync(path.join(DIR, 'wsEndpoint'), browser.wsEndpoint());
20 | };
--------------------------------------------------------------------------------
/setup/print.js:
--------------------------------------------------------------------------------
1 | const chalk = require('chalk');
2 | const { isEnvironmentMessages } = require('../config');
3 |
4 | const error = chalk.bold.red;
5 | const warning = chalk.keyword('orange');
6 | const gray = chalk.keyword('gray');
7 | const green = chalk.keyword('green');
8 |
9 | module.exports = function(message, color) {
10 | if( isEnvironmentMessages ){
11 | let m;
12 | switch (color) {
13 | case "warning":
14 | m = warning(message);
15 | break;
16 |
17 | case "error":
18 | m = error(message);
19 | break;
20 |
21 | case "green":
22 | m = green(message);
23 | break;
24 |
25 | default:
26 | m = gray(message);
27 | break;
28 | }
29 | console.log(m);
30 | }
31 | };
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # UI test automation
2 | This repo provides sample code for ui test automation with jest and puppeteer.
3 |
4 | Please look at [this documentation](https://github.com/smooth-code/jest-puppeteer).
5 |
6 | # How to start?
7 | If you don't have `node` check [this](https://nodejs.org/en/).
8 | If you don't have `yarn` check [this](https://yarnpkg.com/en/docs/install).
9 |
10 | - `$ yarn install`
11 | - `$ yarn test`
12 |
13 | ## ✨ Contributors
14 |
15 | | [
Mustafa İlhan](https://github.com/ilhan-mstf)
💻 🎨 | [
Murat Mayadağ](https://github.com/mmayadag)
:pencil: 💻 🎨|
16 | | :---: | :---: |
17 |
18 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 |
2 | The MIT License (MIT)
3 |
4 | Copyright (c) 2019
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 |
--------------------------------------------------------------------------------
/setup/puppeteer_environment.js:
--------------------------------------------------------------------------------
1 | // puppeteer_environment.js
2 | const print = require('./print');
3 | const NodeEnvironment = require('jest-environment-node');
4 | const fs = require('fs');
5 | const path = require('path');
6 | const puppeteer = require('puppeteer');
7 | const os = require('os');
8 | const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
9 |
10 | class PuppeteerEnvironment extends NodeEnvironment {
11 | constructor(config) {
12 | super(config);
13 | }
14 |
15 | async setup() {
16 | print('Setup Test Environment.',"warning")
17 | await super.setup();
18 | // get the wsEndpoint
19 | const wsEndpoint = fs.readFileSync(path.join(DIR, 'wsEndpoint'), 'utf8');
20 | if (!wsEndpoint) {
21 | throw new Error('wsEndpoint not found');
22 | }
23 |
24 | // connect to puppeteer
25 | this.global.__BROWSER__ = await puppeteer.connect({
26 | browserWSEndpoint: wsEndpoint,
27 | });
28 | }
29 |
30 | async teardown() {
31 | print('Teardown Test Environment.',"warning")
32 | await super.teardown();
33 | }
34 |
35 | runScript(script) {
36 | return super.runScript(script);
37 | }
38 | }
39 |
40 | module.exports = PuppeteerEnvironment;
--------------------------------------------------------------------------------