1

I’m new to the entire concept of capturing JS coverage. The way I intend to use JS coverage tool is to check how many lines of my JS code is being hit by my tests running in a browser, thus using code coverage to improvise my test coverage. Can somebody guide me to an example/tool of how to capture JS coverage for the test running using selenium webdriver.

3 Answers3

1

A tool like nyc is what you need. Take your js file(s) and run something like

nyc instrument

on them and nyc will modify the js files to record coverage. Use these modified files in your website. Then run your selenium tests. At the end of the selenium test, you need to capture the data from the JS files and save it to a json file. I use this code for that.

IJavaScriptExecutor js = (IJavaScriptExecutor)driver; string coverageData = (string)js.ExecuteScript("return JSON.stringify(window.cov_h0rgge4zy);"); File.WriteAllText(@"C:\someDir\coverage\" + DateTime.Now.Ticks + ".json", "{\"someFile\":" + coverageData + "}");

then run something like

nyc report html someCoverage.json

and you'll get the coverage report, html format

user378380
  • 771
  • 8
  • 12
0

You can try JSCover in manual you can find how to integrate it with selenium.

mackowski
  • 366
  • 5
  • 15
  • I've tried JSCover and observed few hinge side: 1. JSCover proxy currently only supports HTTP, not HTTPS 2. With HTTP it does not collect the coverage data of my application 3. Not sure if the repository is maintained lately – Abhijit Bangde Jan 09 '17 at 10:04
  • Also with the following evaluation I'm more inclined towards using istanbul [link](http://stackoverflow.com/questions/32042195/blanket-js-vs-istanbul-js-vs-jscover) any help around this would be highly appreciated – Abhijit Bangde Jan 09 '17 at 10:11
  • > 2. With HTTP it does not collect the coverage data of my application There must be something wrong with your configuration. There are working examples at https://github.com/tntim96/JSCover-samples. > 3. Not sure if the repository is maintained lately It is. Last release was 13th Dec 2016 (28 days ago from today). – tntim96 Jan 09 '17 at 23:39
0

keep generated .json file in .nyc_output directory then run below command. nyc report --reporter=html

reference https://istanbul.js.org/docs/advanced/coverage-object-report/