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

Javascript Code Coverage for Application deployed on Tomcat #321

Closed ManjeetEX closed 1 year ago

ManjeetEX commented 1 year ago

Hi All,

Can anyone please confirm how to get Javascript code coverage for application deployed on tomcat.

Description : My application is build using dotnet, javascript and Java. To run our application we deploy it on Tomcat(For java code) and for dotnet we deploy using IIS server. Both IIS and Tomcat use same port 8080. Now to get my Javascript code coverage I need to start my JSCover server (https://tntim96.github.io/JSCover/manual/manual.xml#generalInformation) used fs - method to instrument my code.

Issue: As for dotnet and java i have already started my tomcat server that is using port 8080. So when i try to start JSCover server on port 8080 i am getting error i.e port is already in use(Java.bind exception). If i change port for JSCover and start using 8085 i am able to start my server and then i launch https://localhost:8085/jscoverage.html.

After launching JSCoverage.html page we enter my application url and click open in new window. I am not able to get code coverage. Probably reason might be that i am using 8085 port for JScover and on port 8080 my application is deployed.

Ques : 1) Can anyone suggest, How can i get Javascript code coverage for application deployed on Tomcat? 2) How can we start JSCover server on same 8080 port.

Thank you Manjeet Kumar

tntim96 commented 1 year ago

Take a look at https://github.com/tntim96/JSCover/tree/master/examples/localStorage-file-system

How can we start JSCover server on same 8080 port. As shown in the example, you don't "start" JSCover and have port issues...you instrument using the file-system mode and deploy that to Tomcat. See the linked example for more details.

ManjeetEX commented 1 year ago

I have followed this approach but "Step 3 : Start the non-instrumenting web-server web-server.bat/web-server.sh" here it will through exception i.e Port 8080 is already in use when we are going to start server.

tntim96 commented 1 year ago

This is an example, and you can either change the ports or temporarily shut-down whatever else is using that port while running the example. The point is to demonstrate how it can work, and then follow that example with your real-world scenario.

ManjeetEX commented 1 year ago

Hi @tntim96 ,

After I instrument my code and redeploy in Tomcat server I am getting multiple javascript error in my .js file when i launch my application. It is because instrumented code has added multiple new line of codes.

Can you please confirm?

Thank you

tntim96 commented 1 year ago

It is because instrumented code has added multiple new line of codes.

That's expected

I am getting multiple javascript error in my .js file when i launch my application

Can you isolate the issue enough for me to replicate so I can investigate further?

ManjeetEX commented 1 year ago

Basically, when i launch my application on my application home page i am getting error for javascript class quote " Unhandled JSException". And line number in the error is referring to the extra code that has been added to actual code. JSError2

Please find below attached screenshot.

tntim96 commented 1 year ago

Sorry, I can't help without an example that I can replicate.

ManjeetEX commented 1 year ago

Sorry, for security reason i cannot share code. Can you please help me with the steps that i need to follow : Step 1) Instrument the code. Step 2) Deploy instrumented code in tomcat.

Quest : 1) After deploying do i need to open my application in Jscoverage.html (after starting server) page by clicking open in window ? If no then how i will be able to see my coverage report.

tntim96 commented 1 year ago

Sorry, for security reason i cannot share code.

I'm not asking for your code, but an cut-down example I can use to replicate the issue. This would require some work on your side.

Quest : 1) After deploying do i need to open my application in Jscoverage.html

No.

If no then how i will be able to see my coverage report.

You will need to be able to access the JavaScript context in whatever tool your using to call jscoverage_serializeCoverageToJSON(). The relevant lines in the example are:

    coverageJSON = @driver.execute_script('return jscoverage_serializeCoverageToJSON();')
    File.open '../../target/example-fs-localStorage/jscoverage.json', 'w' do |f|
      f.write coverageJSON
    end
    jsCoverJSFile = '../../target/example-fs-localStorage/jscoverage.js'
    jsCoverJS = File.read(jsCoverJSFile)
    jsReportCode = "\njscoverage_isReport = true;"
    if jsCoverJS.index(jsReportCode) == nil
      File.open jsCoverJSFile, 'a' do |f|
        f.write jsReportCode
      end
    end
ManjeetEX commented 1 year ago

I am executing my test case manually in that case how can i see report. I am not using any tool for now. Steps followed till now : 1) Instrumented the code and deployed in tomcat. 2) I need to launch my application in chrome. 3) How to get coverage of workflow that i will execute manually ?

Note : After i have instrumented my code "\u00ef;" quoted text is getting added in many javascript file. Which is causing javascript error.

tntim96 commented 1 year ago

I am executing my test case manually...I need to launch my application in chrome...How to get coverage

You can open the dev tools, go to the console and type in jscoverage_serializeCoverageToJSON(). Then you can manually save the output to jscoverage.json, and then manually edit the jscoverage.js adding jscoverage_isReport = true; as per the example above.

BTW, if you are doing this manually in chrome, you can probably use it's coverage measurement tool: https://developer.chrome.com/docs/devtools/coverage/

After i have instrumented my code "\u00ef;" quoted text is getting added in many javascript file

Can you give me a cut-down example of the code that causes this?