Getting code-coverage of browser-Javascript files requires interaction with an external tool that does JS-code instrumentation. The most common approach requires that Javascript files be instrumented manually, then upload onto the web-server for the web-page-under-test (WUT). This latter install is a manual step and may be considered intrusive. There are other approaches and various tools for Javascript instrumentation.
JscovTestUtil came about due to the author’s requirement to test a MCU (micro-computer-unit) that supported WiFi and enough storage to run a small web-server (an ESP8266 device). Manual instrumenting and upload, to the MCU, approach would be time consuming and subject to coordination error, while placing wear-&-tear on the MCU development board.
JSCover was found to meet the requirements needed for automatic instrumentation and no re-upload to the device. So the design and development of JscovTestUtil was instigated.
Using JSCover's proxy-server of on-the-fly instrumentation (which works great), and interacting with Selenium and TestNG, the goal of a seamless flow-through for getting code-coverage of JS-files on a MCU device/web-server works.
Used in a like manner as a user, by
executing the tool using Java's ProcessBuilder
class.
webDriverForJSCCoverProxy(..)
) [the
WebDriver is configured to use JSCover's as a proxy]webDriverForBrowser(..)
, or
webDriverForTypeSameAs(..)
)JscovTestUtil employs JSCover's proxy-server capability to do on-the-fly instrumentation of JS-files.
For all intent-&-purposes, users of JscovTestUtil will find it removes 'manual instrumentation' for the user and subsequent upload to web-servers. The removal of any upload reduces the risk of leaving instrumented JS-files on the web-server by accident.
The proxy-server approach (simplified) allows processing in the following manner:
For JscovTestUtil to work setup/configuration of Selenium V3 and JSCover is needed.
How-to setup/configure is outside this documents scope, and it is assumed users will follow-up with Selenium and/or JSCover documentation.
IDEA: consider using WebDriverManager for Selenium setup/install/configuration management.
The following diagram and its ordered steps is a brief on how JscovTestUtil interacts with hi-level "components" to do Javascript code-coverage in a test Selenium (Java) and @Test framework environment.
Note: JscovTestUnit causes JSCover to generate report-files,
however, creation of files is NOT concurrent friendly for
parallel processing of @Test testcase-scripts.
JscovTestUnit is a part of the Test environment as a Selenium
add-in. It is a library that provides JSCover control interaction
methods to simplify/integrate run-time configuration.
JscovTestUtil has been designed to be integral with IDE's and/or using external locations for resources, the resources being Selenium V3 WebDriver executables and the JSCover program.
Selenium setup is involved, in that WebDriver executables are OS-platform and browser type dependent, JSCover setup has no dependencies as such.
Notes: An assumption is made that users are familiar with Selenium V3's WebDriver setup/configuration.
Also, the user is capable of configuring JSCover as per its setup/configuration arrangements.
It is recommended that JSCover and Selenium WebDrivers be installed on a machines local-file-system, thus supporting distributed development and no reliance on a network connection.
The integral support of JscovTestUtil is focused on IDE use, where all the resources are contained within an IDE's project structure. This is done by making JSCover part of the test-environment.
JSCover IDE integration"installing" JSCover within the IDE's "user.dir" structure at '<user.dir>/jscoverInst' directory. Notes
|
Selenium Web-driver installSelenium V3 WebDrivers are independently provided files for each browser type. There is a specific manner in which they are setup/install to interface with Selenium Java. The user should refer to Selenium V3 documentation on how this is done.
Also, consider using |
|||
|
||||
Structure allows cross-platform of project distribution
so shopping around is unnecessary.
CON: Large amount of storage on SCM system PRO: all users have the same JSCover programs PRO: single person program update into a repository PRO: SCM pull provides the driver distribution, vs individual download PRO: supports distributed SCM development when network is not available PRO: structure can be reduced in size by removing 'items' e.g. Examples |
Using the external resources capability of JscovTestUtil allows users to choose how-to and where-from resources are accessed. The following diagram outlines access arrangements. It is the users decision to use the external resources arrangement:
With Selenium and JSCover setup in the test environment, either as identified to a JscovTestUtil object, or with a user's setup, test-cases are written against the JscovTestUtil object.
The setOn...
or setOff...
should be called
before action methods.
JSCover has a number of options that may be added to influence the operation of the proxy-server. The key options are those that influence which JS-files are instrumented, or not (see below and/or the JSCover manual).
|
|
JSCover has proxy-server options that are "include/exclude settings" for JS-files that are to be instrumented, while others are excluded.
JscovTestUtil | JSCover opt | what |
---|---|---|
optionNoInstrument(...) |
--no-instrument=URL | exclude URL, all other included |
optionNoInstrumentReg(...) |
--no-instrument-reg=URL | exclude regex-URL, all other included |
optionOnlyInstrumentReg(...) |
--only-instrument-reg=URL | include regex-URL, all other excluded |
'Reg/-reg' is a regular expression and should be used when dealing with multiple file patterns to include or exclude from instrumenting.
Javascript files are HTTP GET requested due to the <script src... > tag:
<script type="text/javascript" src="./subdir/file1.js"></script> <script type="text/javascript" src="./subdir/fileA1.js"></script> <script type="text/javascript" src="./file2.js"></script> <script type="text/javascript" src="/file3.js"></script>
The parameter regexOfJsFile
is a regex-pattern
to match a URL of a JS-file (as it pertains to
the above paths). Regex can be involved and deal with complex
patterns. For JscovTestUtil very simple patterns are
recommended, for more involved patterns consult external Java
regex expertise.
Following are simple positive matches (note: '\' needs to be coded as '\\' in Java)
regex-pattern | ./subdir/file1.js | ./subdir/fileA1.js | ./file2.js | /file3.js | simple wildcard | coded as |
---|---|---|---|---|---|---|
^.*dir.file.*$ |
X | X | - | - | *dir*file* | |
^.*file.*$ |
X | X | X | X | *file* | |
^.*file.*1.*$ |
X | X | - | - | *file*1* | |
^.*file3.*$ |
- | - | - | X | *file3* | |
^.*file2.*$ |
- | - | X | - | *file2* | |
^.*2.*$ |
- | - | X | - | *2* | |
^.*file3\.js$ |
- | - | - | X | *file3.js | ^.*file3\\.js$ |
^.*subdir.*$ |
X | X | - | - | *subdir* | |
^.*file[32].*$ |
- | - | X | X | ||
^.*file[321].*$ |
X | - | X | X | ||
^.*file.*[321].*$ |
X | X | X | X | ||
^.*file.*[13].*$ |
X | X | - | X | ||
^.*file.*[3].*$|^.*file.*[1].*$ |
X | X | - | X | regex or condition | |
^.*file.*[A].*$|^.*file.*[3].*$ ^.*file.*[A].*$|^.*file[3].*$ ^.*file.*[A].*$|^.*file3.*$ |
- | X | - | X | regex or condition |
At the beginning and end of testing the following methods need to be used. The start and stop are best placed in @BeforeSuite and @AfterSuite respectively. The store-report may be done anytime (before stop).
If the setOffCodeCoverage is done before testing, most JscovTestUtil methods will be ignored and not do anything.
|
|
|
WebDriver driver = new FirefoxDriver(); // the normal manner of API actions driver.get("http://........); WebElement myTestElement = driver.findElement(........); ::
However, for code-coverage to work a driver needs to be acquired from a JscovTestUtil object (the object provides the JSCover proxy-server management and provides a WebDriver to use the proxy).
JscovTestUtil jscovObj = new JscovTestUtil(3129, "base"); jscovObj.setOnCodeCoverage(); jscovObj.startProxyServer(); WebDriver jscovDriver = jscovObj.webDriverForJSCoverProxy(JscovBrowserKind.FIREFOX); // the normal manner of API actions jscovDriver.get("http://........); WebElement myTestElement = jscovDriver.findElement(........); ::
NOTE: getting a new driver per test-case is not ideal. Consider defining the driver-for-proxy and start and stop in @BeforeSuite or @AfterSuite setup/configuration methods.
Additional methods provide the ability to have a report viewed at the end of the testing.
|
|
Hint:
>cd <JSCover directory>
>java -cp target/dist/JSCover-all.jar jscover.server.SimpleWebServer . 8080
Launch a browser with URL 'http://localhost:8080' and follow the sub-directories to the location of the 'reports' directory.
Copyright (c) 2019 dbradley.