Skip to content
Permalink
Browse files
Add login acceptance testing
Added acceptance testing for the login page feature using cucumber and
puppeteer. Test script has been changed to run Jest tests followed by
Cucumber features.

.githooks was also modified to run the test script on pre-commit so that
no code should be committed with failing tests.
  • Loading branch information
soperd committed Nov 29, 2019
1 parent 7d99249 commit cdb90eb1886810f82a0b84a72b44be103ad01c00
Show file tree
Hide file tree
Showing 8 changed files with 390 additions and 255 deletions.
@@ -1,4 +1,9 @@
#!/bin/sh

echo "running the 'pre-commit' script"

echo "running eslint..."
./node_modules/.bin/eslint .

echo "running tests..."
npm test
@@ -15,5 +15,6 @@ module.exports = {
testPathIgnorePatterns: [
'/node_modules/',
'/__tests__/fixtures/',
'/tests/test.js'
]
}

Large diffs are not rendered by default.

@@ -7,7 +7,7 @@
"start": "node --use_strict index.js",
"watch": "npx nodemon --harmony --use_strict index.js",
"build-db": "node ./build/build_db.js",
"test": "./tests/test.sh"
"test": "node ./tests/test.js"
},
"author": "",
"license": "ISC",
@@ -5,14 +5,15 @@ Feature: Login Page
Scenario: User logs in successfully
Given username is 'admin' and password is 'hello'
When I try to log in
Then I should be redirected and logged in successfully
Then I should be logged in successfully
Then I should be redirected to '/'

Scenario: User provides incorrect password
Given username is 'admin' and password is not 'hello'
Given username is 'admin' and password is 'nothello'
When I try to log in
Then I should be asked to try again with an error telling me the password was incorrect

Scenario: User provides incorrect username
Given username is not 'admin' and password is 'hello'
Given username is 'notadmin' and password is 'hello'
When I try to log in
Then I should be asked to try again with an error telling me the username doesn't exist
@@ -51,39 +51,40 @@ Before(async function() {
currentPage = await browser.newPage()
})

const getLoginError = async() => currentPage.evaluate(() => {
const msgEl = document.getElementsByClassName('error-msg')[0]
return msgEl ? msgEl.textContent.trim() : ''
})

Given('username is {string} and password is {string}', async function(user, pass) {
await currentPage.goto(`http://${hostname}/login`)

await currentPage.type('input[name=username]', user)
await currentPage.type('input[name=password]', pass)
})

When('I try to log in', async() => {
When('I try to log in', async function() {
await currentPage.click('button[type=submit]')
})

Then('I should be redirected and logged in successfully', async function() {
const currentURL = currentPage.url()
console.log(await currentPage.content())
assert(currentURL === `http://${hostname}/`, `got ${currentURL}`)
Then('I should be logged in successfully', async function() {
const errorMsg = await getLoginError()
assert(!errorMsg, `got: ${errorMsg}`)
})

Given('username is {string} and password is not {string}', function(user, pass) {
// Write code here that turns the phrase above into concrete actions
return 'pending'
})
Then('I should be redirected to {string}', function(page) {
const currentURL = currentPage.url()
if (!page.startsWith('/')) page = `/${page}`

Then('I should be asked to try again with an error telling me the password was incorrect', function() {
// Write code here that turns the phrase above into concrete actions
return 'pending'
assert(currentURL === `http://${hostname}${page}`, `got: ${currentURL}`)
})

Given('username is not {string} and password is {string}', function(user, pass) {
// Write code here that turns the phrase above into concrete actions
return 'pending'
Then('I should be asked to try again with an error telling me the password was incorrect', async function() {
const errorMsg = await getLoginError()
assert(errorMsg === 'Password incorrect', `got: ${errorMsg}`)
})

Then('I should be asked to try again with an error telling me the username doesn\'t exist', function() {
// Write code here that turns the phrase above into concrete actions
return 'pending'
Then('I should be asked to try again with an error telling me the username doesn\'t exist', async function() {
const errorMsg = await getLoginError()
assert(errorMsg === 'User does not exist', `got: ${errorMsg}`)
})
@@ -0,0 +1,30 @@
#!/usr/bin/env node

'use strict'

/* This is the test script */

const { exec } = require('child_process')

let exitCode = 0

const handler = (error, stdout, stderr) => {
if (error) {
console.error(error)
exitCode = 1
}

if (stderr) console.error(stderr)
if (stdout) console.log(stdout)
}

console.log('Running Jest...')
exec(`${__dirname}/../node_modules/.bin/jest --runInBand --coverage`, handler)
.on('close', () => {
console.log('Running Cucumber...')
exec(`${__dirname}/../node_modules/.bin/cucumber-js ./tests/features`, handler)
.on('close', () => {
process.exit(exitCode)
})
})

This file was deleted.

0 comments on commit cdb90eb

Please sign in to comment.