14

Selenium: Is there any JS (JavaScript) code coverage tool which we can integrate with Selenium Server/RC

Hi,

I want to JS code coverage done by my Selenium Test Suite

Is there any code coverage tool which can be integrated with my existing selenium rc framework??

Abhinav Garg
  • 1,452
  • 3
  • 20
  • 40

6 Answers6

5

In theory, you can use any coverage tool that instruments JavaScript code to collect coverage data.

For example, you can use JSCoverage either in proxy mode (for real-time instrumentation) or for static instrumentation to run your Selenium tests off the instrumented code.

One technical hurdle could be to devise a way to accumulate coverage data across multiple pages. The coverage tool of your choice could already have support for accumulation of data from multiple page runs. If not, you might have to handle that bit yourself, but this is probably not something trivial.

Ates Goral
  • 126,894
  • 24
  • 129
  • 188
3

I have found the solution for my need, there are multiple tools available but I want a tool which can be integrated with my existing automation easily

Tool is JSCover http://tntim96.github.io/JSCover/

Just run the Server

  1. Run Server java -cp JSCover-all.jar jscover.server.SimpleWebServer . 8080
  2. Run Proxy Server java -jar target/dist/JSCover-all.jar -ws --proxy --port=3128 --report-dir=target

  3. Add Proxy in your Webdriver for port specified, 3128 in my case

  4. After execution of test Generate Reports using

    ((JavascriptExecutor) driver).executeScript("jscoverage_report();");
    

You can add this line where you exit WebDriver or Test Case

Abhinav Garg
  • 1,452
  • 3
  • 20
  • 40
2

Not aware of a tool for Selenium, but JsTestDriver has a design very similar to Selenium RC (can launch tests from the command line and they are run on a server that drives browsers headlessly) and provides code coverage information.

IntelliJ integrates with JsTestDriver and provides a visual display of coverage information.

Charles Kendrick
  • 2,059
  • 13
  • 14
2

Take a look at https://github.com/yui/yuitest/wiki/Code-Coverage

I've integrated this tool in my Selenium tests a time ago. You need a bit of work to gather coverage info before page changes (in any case js trigger a page reload, link etc...)

Once you set up everything, it will fully coverage any js executed while Selenium load and test your website pages.

PS : Even if it was specially adapted for YUI test, you can use it with selenium.

Fabien
  • 873
  • 2
  • 10
  • 22
  • Can you give some more detail about how you recorded js coverage while selenium is running? – karambir Jul 11 '14 at 13:22
  • Before each end of test, ask selenium to run JS on the current page: YUITest.TestRunner.getCoverage(YUITest.CoverageFormat.JSON). This will return coverage data that selenium can store somewhere. Then using the yui report jar, you can generate a coverage report for all the data of all tests. For complete and exact coverage, you have to take care of page change (eg. triggered by events on the page). Do some sort of code instrumentation on your js to call yui coverage function before each page change. – Fabien Jul 25 '14 at 09:25
1

I don't know what you are trying to achieve, but:

  • Selenium is testing the final output, as seen on the page itself.

So it really does not matter if its PHP, HTML, JSP, ASP or .NET - the Selenium is designed to mimick the end user and click the final application - the final HTML code generated by whatever is under the hood.

Selenium is also not that good for code coverage tests - one piece code can be on many pages - so better approach with selenium is to do the "user" coverage - try to cover all the possible actions which living human could possibly do with your page

Pavel Janicek
  • 12,577
  • 12
  • 47
  • 76
  • 1
    my mistake i was talking about JS (Javascript) coverage. I want to know how much JS my selenium test cases cover. It helps me in verifying that i covered most of the cases also it helps me to report if there is unused JS file. There may be a case that one or two JS files are never used so using code coverage with selenium i can report that these JS files are not being used and could be garbage. – Abhinav Garg Feb 29 '12 at 09:11
  • I am not aware of this possibility - of course, Selenium clicking and user interactions fire some javascript actions. You can monitor these, but there is probably no tool to tell you e.g. "the accounts.js was never used" – Pavel Janicek Feb 29 '12 at 09:46
  • ya there may be no tool which can tell me which JS was never used but i think there are tools like sonar which can tell which JS are used and there use percentage so by this i can also identify which JS are never used. I just want is there any tool for code coverage which can be easily integrated with selenium – Abhinav Garg Feb 29 '12 at 09:57
0

There is no particular tool that can integrate with Selenium to do JS coverage. However there are lots of tools which test JS on every page which can tell if the JS that executing on your web page had any errors. This may not ideal solution but on each page you will have the measure of uptil which point JS executed properly on your webpage under test. There are two solutions for that:

1.) JSErrorCollector API: It will integrate directly with Selenium and let you know if there were any error on the page. Can be found at: http://mguillem.wordpress.com/2011/10/11/webdriver-capture-js-errors-while-running-tests/

2.) Full fledged JS coverage tools: There is an excellent list of tools here which will essentially help you in covering JS on on your web pages. Can be found at: JavaScript unit test tools for TDD

Community
  • 1
  • 1
Manpreet Singh
  • 3,440
  • 2
  • 15
  • 14