Is there any tool available to determine the code coverage for my selenium tests?

My tests are written in Java having use of selenium WebDriver with BDD-Cucumber implementation in it.

  • 1
    If you mean with your system-under-test, then yes all the standard code coverage tools should work. Start the server with code coverage tool enabled before you run your tests. If you mean code coverage on your selenium code then yes, also run it from your IDE debugger, but ask WHY do that. It gives you no meaningful information about the SUT. Nov 28 '19 at 14:31
  • @MartinSpamer basically I want to know that how much dev code got hit with my selenium test scripts. Looking for a tool that has selenium integration which can give me info about how much dev code is covered with my selenium script. Nov 29 '19 at 7:08
  • Then the SUT needs to be run under some dev harness that includes code coverage. JaCoCo is pretty solid way of doing this stand alone alternatively running the SUT in debug mode from within your IDE. The details depend the SUT more than the WebDriver or Cucumber. eclemma.org/jacoco/trun isk/doc/agent.html Dec 5 '19 at 22:46

You can use Istanbul to instrument front-end code for code-coverage.

Process looks something like this:

  • Start application with code instrumented by coverage tool
  • Run the Cucumber/Selenium tests against web-app instance
  • Generate report of with coverage tool

This article describes how you can use Istanbul to generate code-coverage from UI tests.

This question describes how to use Istanbul with Selenium.

Similar questions other tools:

  • Selenium scripts are written in java but the application I am testing is in javaScript. so looking out a tool which has integration with java (selenium) wrt application under test is in JavaScript. Nov 29 '19 at 10:47
  • @UpdeshKumar The concept is the same. Added links for Istanbul the current most popular JavaScript code-coverage tool. Nov 29 '19 at 10:55
  • @UpdeshKumar It was not very clear from your question that you wanted to test the front-end javascript code. Please write clearer question with more context. Nov 29 '19 at 10:56

Finally, this solution works for me --

  1. Instrument the Istanbul.js.
  2. Use Command - istanbul instrument test.js >> test-instumented.js

  3. Now, use the below code in @AfterClass annotation. It will execute the below method after all tests.

AfterClass Implementation 4. Now, we have coverage.json.

  1. Crete a HTML using coverage,json using istanbul report.
  • What is the js.executeScript() line doing? Didn't see the use in your code. Also, can you please explain more about this? "Use Command - istanbul instrument test.js >> test-instumented.js" Once the dev frontend code is using instanbul, then can we use the production environment to run Selenium WebDriver automation?
    – Sun Shine
    Apr 29 '20 at 18:55
  • Your note in #3 seems to contradict the code sample you gave. You said @ AfterClass, but your coe is using @ AfterMethod. Which should it be, or is that even important? May 15 '20 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.