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 ();}}