tntim96 / JSCover

JSCover is a JavaScript Code Coverage Tool that measures line, branch and function coverage
GNU General Public License v2.0
399 stars 84 forks source link

Error getting while trying to execute JavaScript for report generation #278

Closed neo81-83 closed 4 years ago

neo81-83 commented 5 years ago

Hello there, I followed the tutorials and some blogs to setup JSCover-2.0.8 for measuring code coverage using Selenium WebDriver and Java on one of our app. JSCover is up and running see the attached screenshot, I am using the proxy setup using the port 3128. Command used:

java -jar target/dist/JSCover-all.jar -ws --proxy --port=3128 --report-dir=target/jscover-proxy

Just before my test ends I execute the following JavaScripts from Selenium code: ((JavascriptExecutor) driver).executeScript("window.jscoverFinished = false;"); ((JavascriptExecutor) driver).executeScript("jscoverage_report('no-frames', function(){window.jscoverFinished=true;});");

And get the following error message on the 2nd JavaScript call: Exception in thread "main" org.openqa.selenium.JavascriptException: ReferenceError: jscoverage_report is not defined

Please let me know what I am doing wrong. Quick reply would be appreciated.

image

tntim96 commented 5 years ago

The screen-shot above uses iFrames which expects UI interaction. It's not documented (because it's not the normal programmatic way of doing things) but you could call the jscoverage_storeButton_click JavaScript function.

To do this programmatically, don't use iFrames. That is, instead of loading your site in the iFrame of jscoverage.html, load the page directly. The jscoverage_report JavaScript function will then be available in the instrumented JavaScript output.

neo81-83 commented 5 years ago

Thanks for your reply, really appreciate it. I tried to do it programmatically but ended up with the same result, please have a look at my code and tell what's wrong:

`public static void main(String args[]) throws Exception{ // TODO Auto-generated method stub System.setProperty("webdriver.gecko.driver", "src/test/resources/drivers/mac/geckodriver"); Proxy proxy = new Proxy(); // set the proxy location proxy.setHttpProxy("localhost:3128"); proxy.setSslProxy("localhost:3128"); // explicitly use Firefox for this, since it’s easy to configure the proxy DesiredCapabilities profile = new DesiredCapabilities(); // set Firefox’s proxy to “Manual” profile.setCapability("network.proxy.type", 1); profile.setCapability("network.http.proxy.version", "1.1"); // set proxy domain host profile.setCapability("network.proxy.http", "localhost"); // set proxy’s port number profile.setCapability("network.proxy.http_port", 3128); // increase the javascript time out since code coverage is really // intensive and slow profile.setCapability("dom.max_script_run_time", 90000); profile.setCapability(FirefoxProfile.ALLOWED_HOSTS_PREFERENCE, "localhost,localhost.localdomain,loghost"); WebDriver driver = new FirefoxDriver(profile); driver.get("https://my-test-site.org/auth/login"); Thread.sleep(2000); driver.findElement(By.xpath("//input[@id='loginEmailFormEmail']")).sendKeys("02@gmail.com"); driver.findElement(By.xpath("//button[@id='loginEmailFormSubmitBtn' or @id='loginPasswordFormSubmitBtn' or @class='loginbutton' or @id='CommonPasswordSubmitButton']")).click(); Thread.sleep(2000); driver.findElement(By.xpath("//input[@id='loginPasswordFormPassword']")).sendKeys("@11"); Thread.sleep(2000); driver.findElement(By.xpath("//button[@id='loginEmailFormSubmitBtn' or @id='loginPasswordFormSubmitBtn' or @class='loginbutton' or @id='CommonPasswordSubmitButton']")).click(); Thread.sleep(8000); //String jscoverageJson = (String)((JavascriptExecutor) driver).executeScript("return jscoverage_serializeCoverageToJSON();"); String json = (String) ((JavascriptExecutor) driver).executeScript("jscoverage_report();"); driver.close();

}`
tntim96 commented 5 years ago

There's a working example here: https://github.com/tntim96/JSCover-samples/blob/master/src/test/java/jscover/webdriver/proxy/WebDriverGeneralProxyTest.java

...and to use FireFox, try something like

    private WebDriver getWebClient() {
        Proxy proxy = new Proxy().setHttpProxy("localhost:3129");
        FirefoxOptions fireFoxOptions = new FirefoxOptions();
        fireFoxOptions.setHeadless(true);
        fireFoxOptions.setProxy(proxy);
        return new FirefoxDriver(fireFoxOptions);
    }
neo81-83 commented 5 years ago

How to make it work for a webapp inside a docker container?

tntim96 commented 4 years ago

Should be the same, as long as you're exposing the ports you need.