Skip to content

Commit

Permalink
add coverage to selenium tests
Browse files Browse the repository at this point in the history
  • Loading branch information
philschatz committed Feb 17, 2016
1 parent d21b1be commit 9bbbb29
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/dist
/test-failed-*.png
/coverage
/coverage-selenium.json

*.cjsx.js

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
"build": "gulp build",
"build-archive": "gulp build-archive",
"test-integration:only": "mocha --bail -R spec ./test-integration/index",
"pretest-integration": "if [ -z ${SERVER_URL} ]; then npm run build; fi",
"pretest-integration": "if [ -z ${SERVER_URL} ]; then NODE_COVERAGE=true npm run build; fi",
"test-integration": "./scripts/run-test-integration.sh",
"posttest-integration": "istanbul report --include ./coverage-selenium.json json --dir ./coverage/ lcov",
"lint-cjsx": "./scripts/lint-cjsx-files.sh",
"start": "gulp dev"
},
Expand Down Expand Up @@ -89,6 +90,7 @@
"gulp-util": "3.0.6",
"gulp-watch": "4.3.5",
"http-server": "0.8.5",
"istanbul": "0.4.1",
"istanbul-instrumenter-loader": "0.1.3",
"json-loader": "0.5.3",
"karma": "0.13.19",
Expand Down
11 changes: 10 additions & 1 deletion test-integration/helpers/describe.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
fs = require 'fs'
selenium = require 'selenium-webdriver'
seleniumMocha = require('selenium-webdriver/testing')
_ = require 'underscore'

chai = require 'chai'
chai.use require 'chai-as-promised'
expect = {chai}
Expand All @@ -26,6 +28,8 @@ logger.addHandler (record) ->





describe = (name, cb) ->
seleniumMocha.describe name, ->

Expand All @@ -40,6 +44,7 @@ describe = (name, cb) ->
@__afterEach = afterEach

@before ->
Verbose.reset()

# Wait 20sec for the browser to start up
@timeout(20 * 1000, true)
Expand Down Expand Up @@ -116,7 +121,11 @@ describe = (name, cb) ->


Verbose.reset()
User.logout(@)
User.logout(@).then ->
# Save the coverage data to a file
# Keep replacing the file as tests finish (the object will continue to be appended to in memory)
coverageData = User.getCoverageData()
fs.writeFileSync('./coverage-selenium.json', JSON.stringify(coverageData), 'utf8')


@after ->
Expand Down
27 changes: 27 additions & 0 deletions test-integration/helpers/user.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ _ = require 'underscore'
{TestHelper} = require './test-element'
selenium = require 'selenium-webdriver'

# Record code coverage before logging out
istanbul = require 'istanbul'
{mergeFileCoverage} = istanbul.utils

COMMON_ELEMENTS =
loginLink:
linkText: 'Login'
Expand Down Expand Up @@ -46,6 +50,21 @@ COMMON_ELEMENTS =
COMMON_ELEMENTS.eitherSignInElement =
css: "#{COMMON_ELEMENTS.searchQuery.css}, #{COMMON_ELEMENTS.usernameInput.css}"


# Record code coverage before logging out.
# Merges the coverage results from multiple tests
__coverage__ = {}
mergeCoverage = (obj) ->
Object.keys(obj).forEach (filePath) ->
original = __coverage__[filePath]
added = obj[filePath]
if original
result = mergeFileCoverage(original, added)
else
result = added
__coverage__[filePath] = result


class User extends TestHelper
constructor: (test) ->
testElementLocator = 'body'
Expand Down Expand Up @@ -125,6 +144,11 @@ class User extends TestHelper

_logout: =>
@openHamburgerMenu()

# Pull out the coverage data
@test.driver.executeScript("var c = window.__coverage__; delete window.__coverage__; return c;").then (results) ->
mergeCoverage(results) if results

@el.logoutForm.get().submit()

logout: =>
Expand All @@ -149,6 +173,9 @@ class User extends TestHelper
unless isOpen
@toggleHamburgerMenu()

User.getCoverageData = ->
__coverage__

User.logout = (test) ->
user = new User(test).logout()

Expand Down
12 changes: 11 additions & 1 deletion webpack.config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ ExtractTextPlugin = require 'extract-text-webpack-plugin'
webpack = require 'webpack'

isStandaloneBuild = process.env.NODE_BUILD_TYPE is 'standalone'
isCoverage = process.env.NODE_COVERAGE
LOADERS = if isStandaloneBuild then [] else ["react-hot", "webpack-module-hot-accept"]
lessLoader = if isStandaloneBuild
{ test: /\.less$/, loader: ExtractTextPlugin.extract('css!less') }
else
{ test: /\.less$/, loaders: LOADERS.concat('style-loader', 'css-loader', 'less-loader') }

if isCoverage
POST_LOADERS = [
{ test: /\.coffee$/, loaders: ["istanbul-instrumenter"] }
{ test: /\.cjsx$/, loaders: ["istanbul-instrumenter"] }
]
else
POST_LOADERS = []

module.exports =
cache: true

Expand Down Expand Up @@ -41,7 +50,8 @@ module.exports =
{ test: /\.cjsx$/, loaders: LOADERS.concat("coffee-jsx-loader") }
{ test: /\.(png|jpg|svg)/, loader: 'file-loader?name=[name].[ext]'}
{ test: /\.(woff|woff2|eot|ttf)/, loader: "url-loader?limit=30000&name=[name]-[hash].[ext]" }
]
]
postLoaders: POST_LOADERS
resolve:
extensions: ['', '.js', '.json', '.coffee', '.cjsx']

Expand Down

0 comments on commit 9bbbb29

Please sign in to comment.