4

Can anybody help me on how to get JavaScript code coverage using Istanbul while running Selenium test cases?

I have gone through this link but could not get it. How do I use it in my case? My tests are running in a local browser calling the remote server. Selenium test cases are written in Java.

1
  • Welcome to StackOverflow. Please read the help section on how to ask a good question, as this will help the community better understand your issue and provide you with a good answer: stackoverflow.com/help/how-to-ask We need to see what you have tried so far and what errors you've encountered otherwise it's difficult to give you any specific guidance. Commented Oct 11, 2018 at 0:03

1 Answer 1

4

https://github.com/alex028502/istanbulseleniumexample

I had trouble understanding that too, so I made the above example with webpack.

module.exports = {
  devtool: 'source-map',
  mode: 'none',
  module: {
    rules: [
      // { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
      {
        resolve: {
          extensions: ['.js'],
        },
        use: {
          loader: 'istanbul-instrumenter-loader',
          options: {esModules: true},
        },
        enforce: 'post',
        exclude: /node_modules/,
      },
      {
        test: /\.coffee$/,
        use: [
          {loader: 'coffee-loader'},
        ],
      },
    ],
  },
  entry: './src/index.js',
  output: {
    path: __dirname + '/public/',
    filename: 'index.js',
  },
};

and then if you are running instrumented code in the browser, you can download it like this

coverage_info = _driver.execute_script('return JSON.stringify(window.__coverage__);')
# each report needs a unique name
# but we don't care for this example which report corresponds
# to which test
timestamp = datetime.datetime.timestamp(datetime.datetime.now())
file = open("nyc_output/coverage%s.json" % timestamp, 'w')
file.write(coverage_info)
file.close()

and then generate a report like this

node_modules/.bin/nyc report -t nyc_output

If you are not using webpack, you just instrument your code using the command line as in the example you pasted in, and it creates a new folder with the instrumented code.

# from https://medium.com/@the1mills/front-end-javascript-test-coverage-with-istanbul-selenium-4b2be44e3e98
mkdir public-coverage
cp -a public/. public-coverage/   # copy all files over
istanbul instrument public \ 
    --output public-coverage \
    --embed-source true

The part that I was able to do without from the link you mentioned is the istanbul middleware

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.