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 14, 2016
1 parent 563263e commit f6df520
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
/dist
/test-failed-*.png
/coverage
/coverage-selenium.json

*.cjsx.js
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 --verbose --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
15 changes: 12 additions & 3 deletions test-integration/helpers/describe.coffee
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
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}
User = require './user'
Timeout = require './timeout'

utils = require './utils'
{resetIndentationLevel} = require './utils/verbose'
Verbose = require './utils/verbose'

screenshot = require './utils/screenshot'
SERVER_URL = process.env['SERVER_URL'] or 'http://localhost:3001/'
Expand All @@ -26,6 +28,8 @@ logger.addHandler (record) ->





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

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

@before ->
resetIndentationLevel()
Verbose.reset()

# Wait 20sec for the browser to start up
@timeout(20 * 1000, true)
Expand Down Expand Up @@ -102,7 +106,12 @@ describe = (name, cb) ->
# logs = @driver.manage().logs().get('browser').then (lines) ->
# console.log line.level.name, line.message for line in lines

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
26 changes: 26 additions & 0 deletions test-integration/helpers/user.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
_ = require 'underscore'
{TestHelper} = require './test-element'

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

COMMON_ELEMENTS =
loginLink:
linkText: 'Login'
Expand Down Expand Up @@ -31,6 +35,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 @@ -78,6 +97,10 @@ class User extends TestHelper
@el.userMenu.isPresent()

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

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

Expand All @@ -92,6 +115,9 @@ class User extends TestHelper
@el.homeLink.click()


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 f6df520

Please sign in to comment.