5

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.

3
  • 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. Commented Nov 28, 2019 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. Commented Nov 29, 2019 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 Commented Dec 5, 2019 at 22:46

2 Answers 2

0

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:

3
  • 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. Commented Nov 29, 2019 at 10:47
  • @UpdeshKumar The concept is the same. Added links for Istanbul the current most popular JavaScript code-coverage tool. Commented Nov 29, 2019 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. Commented Nov 29, 2019 at 10:56
3

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.
2
  • 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
    Commented Apr 29, 2020 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? Commented May 15, 2020 at 14:24

Your Answer

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

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