Jscover+webdriver/selenium get JS Code Coverage

Source: Internet
Author: User

When we look at the introduction and use of Jscover (Http://tntim96.github.io/JSCover/manual/manual.xml), we are often attracted by the graphical interface. This method is more suitable for manual operation, click and input can be.


But this often interface and the real interface is quite different, because the real page in the jscover inside the browser loading box. This makes the original selenium code possible with many problems. So how can we try to change the original selenium code as little as possible, and get to JS's coded coverage? This can be used in file mode.

1. Instrument JS Code

Java-jar Jscover-all.jar-fs source-directory destination-directory
Source-directory is the JS code folder, containing the JS code that needs to be instrument. Destination-directory the folder for files that are output after the instrument. The command creates destination-directory and copies the source-directory files. For JS files, it will be instrument. Source-directory will remain in the end. Finally jscover also generates the code jscoverage.html uses to execute instrument.


2. Start the server

Because we finally want to see the report result, we need to set the variable Jscoverage_isreport in Jscoverage.js to true. Jscoverage.js will be produced along with jscoverage.html.

Jscoverage_isreport = true;
Then we can start the Instrument Web server and test it.

3. Writing Selenium/webdriver Code

The sample code can be consulted: https://github.com/tntim96/JSCover-samples

We just need to follow the general Selnium/webdriver code to write it. But in the end there is a step to generate code coverage that requires the following code:

Webdriver

String json = (string) ((Javascriptexecutor) webClient). Executescript ("Return Jscoverage_serializecoveragetojson ();");

Selenium

String json = Selenium.geteval ("Selenium.browserbot.getUserWindow (). Jscoverage_serializecoveragetojson ();");
Here we can refer to a netizen wrote: http://www.350351.com/qianyanjishu/ceshijishu/69429.html

Import Org.junit.test;import Org.openqa.selenium.by;import Org.openqa.selenium.javascriptexecutor;import Org.openqa.selenium.webdriver;import Org.openqa.selenium.firefox.firefoxdriver;public class SeleniumTester{@ testpublic void Testjscover () {Webdriver driver = new Firefoxdriver ();d river.get ("http://localhost/example-fs/ Index.html ");d river.findelement (by.id (" Radio1 "). Click (); String json = (string) ((Javascriptexecutor) driver). Executescript ("Return Jscoverage_serializecoveragetojson ();"); Driver.quit ();}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.