├── .nvmrc ├── .travis.yml ├── .gitignore ├── babel.config.json ├── report.js ├── features ├── manage-todo-list.feature └── support │ ├── steps.js │ └── world.js ├── package.json ├── README.md └── LICENSE /.nvmrc: -------------------------------------------------------------------------------- 1 | lts/* 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: bionic 2 | language: node_js 3 | node_js: lts/* 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | cucumber_report.html 3 | cucumber_report.json 4 | yarn.lock 5 | package-lock.json 6 | -------------------------------------------------------------------------------- /babel.config.json: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | [ 4 | "@babel/preset-env", 5 | { 6 | "targets": { 7 | "esmodules": true 8 | } 9 | } 10 | ] 11 | ] 12 | } 13 | -------------------------------------------------------------------------------- /report.js: -------------------------------------------------------------------------------- 1 | const reporter = require('cucumber-html-reporter'); 2 | 3 | const options = { 4 | theme: 'bootstrap', 5 | jsonFile: 'cucumber_report.json', 6 | output: 'cucumber_report.html', 7 | reportSuiteAsScenarios: true, 8 | launchReport: true, 9 | }; 10 | 11 | reporter.generate(options); 12 | -------------------------------------------------------------------------------- /features/manage-todo-list.feature: -------------------------------------------------------------------------------- 1 | Feature: Manage todo list 2 | 3 | Scenario: add todo 4 | Given I have the todo list 5 | | Label | 6 | | Clean the kitchen | 7 | When I add the todo item "Prepare dinner" to the list 8 | Then I expect the todo list to have 2 items 9 | And I expect the todo item 2 to be "Prepare dinner" 10 | 11 | Scenario: delete todo 12 | Given I have the todo list 13 | | Label | 14 | | Clean the kitchen | 15 | | Prepare dinner | 16 | When I press the delete button of the todo item 1 17 | Then I expect the todo list to have 1 item 18 | And I expect the todo item 1 to be "Prepare dinner" -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cucumber-puppeteer-example", 3 | "description": "Example project on how to use cucumber with puppeteer", 4 | "version": "1.0.0", 5 | "main": "index.js", 6 | "repository": "git@github.com:spirosikmd/cucumber-puppeteer-example.git", 7 | "author": "Spyros Ioakeimidis ", 8 | "license": "MIT", 9 | "devDependencies": { 10 | "@babel/core": "^7.8.4", 11 | "@babel/preset-env": "^7.8.4", 12 | "@babel/register": "^7.8.3", 13 | "chai": "^4.2.0", 14 | "cucumber": "^6.0.5", 15 | "cucumber-html-reporter": "^5.1.0", 16 | "puppeteer": "^2.1.1" 17 | }, 18 | "scripts": { 19 | "test": "cucumber-js --require-module @babel/register --format json:cucumber_report.json", 20 | "test:report": "node report.js" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cucumber-puppeteer-example 2 | 3 | Write UI tests using Gherkin, Cucumber, and Puppeteer. This project is an example project on how to use [cucumber](https://github.com/cucumber/cucumber-js) with [puppeteer](https://github.com/GoogleChrome/puppeteer). It uses the [React TodoMVC](http://todomvc.com/examples/react/#/) project as a test UI. 4 | 5 | Run `yarn`/`npm install` to get the dependencies and then run `yarn test`/`npm test` to execute the UI tests. 6 | 7 | The test command will generate a JSON report file. You can use the `yarn test:report`/`npm run test:report` command to check the HTML report. 8 | 9 | ## Headless 10 | 11 | The tests run by default in headless mode. To launch a full version of Chromium: 12 | 13 | ``` 14 | $ HEADLESS=false yarn test 15 | ``` 16 | 17 | ## Timeouts 18 | 19 | The `http://todomvc.com/examples/react/#/` might be slow. To avoid any kind of timeout erros, in [world.js](./features/support/world.js) there is an example of how to [change the default timeout](https://github.com/cucumber/cucumber-js/blob/master/docs/support_files/timeouts.md#timeouts). 20 | -------------------------------------------------------------------------------- /features/support/steps.js: -------------------------------------------------------------------------------- 1 | import { After, Before, Given, Then, When } from "cucumber"; 2 | 3 | Before(async function(testCase) { 4 | return await this.openTodoPage(); 5 | }); 6 | 7 | After(async function() { 8 | return await this.closeTodoPage(); 9 | }); 10 | 11 | Given("I have the todo list", async function(dataTable) { 12 | const rows = dataTable.rows(); 13 | for (let index = 0; index < rows.length; index++) { 14 | await this.writeTodo(rows[index][0]); 15 | } 16 | }); 17 | 18 | When(/^I add the todo item "(.*)" to the list$/, async function(todo) { 19 | return await this.writeTodo(todo); 20 | }); 21 | 22 | When(/^I press the delete button of the todo item (\d+)$/, async function( 23 | todoIndex 24 | ) { 25 | return await this.deleteTodo(todoIndex); 26 | }); 27 | 28 | Then(/^I expect the todo list to have (\d+) items?$/, async function(number) { 29 | return await this.checkNumberOfTodos(number); 30 | }); 31 | 32 | Then(/^I expect the todo item (\d+) to be "(.*)"$/, async function( 33 | todoIndex, 34 | todo 35 | ) { 36 | return await this.checkTodoIsInList(todoIndex, todo); 37 | }); 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Spyros Ioakeimidis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /features/support/world.js: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | import { setWorldConstructor, setDefaultTimeout } from "cucumber"; 3 | import puppeteer from "puppeteer"; 4 | 5 | const PAGE = "http://todomvc.com/examples/react/#/"; 6 | const ENTER_EVENT = "Enter"; 7 | const INPUT_SELECTOR = "section input"; 8 | const TODO_ITEMS_SELECTOR = "ul.todo-list li"; 9 | const todoItemSelector = index => `ul.todo-list li:nth-child(${index})`; 10 | const todoItemLabelSelector = index => `${todoItemSelector(index)} label`; 11 | const deleteButtonSelector = index => `${todoItemSelector(index)} button`; 12 | 13 | const HEADLESS = process.env.HEADLESS !== "false"; 14 | 15 | setDefaultTimeout(30 * 1000); 16 | 17 | class TodoWorld { 18 | async openTodoPage() { 19 | this.browser = await puppeteer.launch({ headless: HEADLESS }); 20 | this.page = await this.browser.newPage(); 21 | await this.page.goto(PAGE); 22 | } 23 | 24 | async writeTodo(todo) { 25 | await this.page.waitForSelector(INPUT_SELECTOR); 26 | this.inputElement = await this.page.$(INPUT_SELECTOR); 27 | await this.inputElement.type(todo); 28 | await this.inputElement.press(ENTER_EVENT); 29 | } 30 | 31 | async checkNumberOfTodos(number) { 32 | const todoItemCount = await this.page.$$eval( 33 | TODO_ITEMS_SELECTOR, 34 | items => items.length 35 | ); 36 | expect(todoItemCount).to.eql(parseInt(number)); 37 | } 38 | 39 | async checkTodoIsInList(todoIndex, todo) { 40 | const foundTodo = await this.page.$eval( 41 | todoItemLabelSelector(todoIndex), 42 | el => el.textContent.trim() 43 | ); 44 | expect(foundTodo).to.eql(todo); 45 | } 46 | 47 | async deleteTodo(todoIndex) { 48 | await this.page.hover(todoItemSelector(todoIndex)); 49 | await this.page.click(deleteButtonSelector(todoIndex)); 50 | } 51 | 52 | async closeTodoPage() { 53 | await this.browser.close(); 54 | } 55 | } 56 | 57 | setWorldConstructor(TodoWorld); 58 | --------------------------------------------------------------------------------