Forum Discussion

allenjTC's avatar
allenjTC
New Contributor
5 months ago

Web test code coverage without AQTime

With AQTime off support and soon to be retired, does anyone have any solutions for collecting code coverage from a web page driven by TestComplete?

We can instrument our code and generate istanbul coverage data, but I have not been able to figure out how to retrieve the "window.__coverage__" results in the local browser session from within a TestComplete script (Javascript).

4 Replies

  • scot1967's avatar
    scot1967
    Icon for Champion Level 1 rankChampion Level 1

    I know NOTHING about this but I copied your question into Chat GPT.  This may be complete rubbish but worth a shot to help!  🤪🤪  

    Retrieving window.__coverage__ in TestComplete (JavaScript)

    TestComplete provides the ability to interact with browser content using Page and Eval methods. You can use this to fetch the coverage object.

    Solution: Use Eval() to Retrieve window.__coverage__

    Try the following JavaScript code in TestComplete:

    function getCoverageData() {
        var page = Sys.Browser("chrome").Page("*");
        
        // Execute JavaScript in the browser context
        var coverageData = page.Eval("JSON.stringify(window.__coverage__)");
        
        if (coverageData) {
            Log.Message("Coverage Data Retrieved");
            Log.Message(coverageData); // Display the coverage JSON in the TestComplete log
            
            // Save to a file
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            var file = fso.CreateTextFile("C:\\temp\\coverage.json", true);
            file.Write(coverageData);
            file.Close();
            
            Log.Message("Coverage data saved to C:\\temp\\coverage.json");
        } else {
            Log.Warning("No coverage data found. Ensure Istanbul is properly instrumented.");
        }
    }

    How This Works

    1. Access the Browser Session
      • Sys.Browser("chrome").Page("*") gets the active page.
    2. Extract window.__coverage__
      • Using Eval("JSON.stringify(window.__coverage__)") runs JavaScript in the browser.
    3. Log & Save Coverage Data
      • Displays the JSON data in the TestComplete log.
      • Saves it to C:\temp\coverage.json for further analysis.

    Next Steps

    • Verify that your app is properly instrumented and window.__coverage__ is available.
    • If using a different browser, replace "chrome" with "firefox" or "edge".
    • Analyze the saved coverage.json using Istanbul's reporting tools:
    npx nyc report --reporter=html

    Then open coverage/index.html in your browser.

    • allenjTC's avatar
      allenjTC
      New Contributor

      Thanks for the attempt, I already spent many hours interrogating ChatGPT with no useful results.

      At best, page.contentDocument.Script.eval("JSON.stringify(window__coverage__)") runs but returns "No coverage data found" message, despite the fact that I can clearly see the data with "window.__coverage__" command in the console. I also have Cypress scripts on the same build that give me coverage.

      page.Eval, page.eval and other variations mostly say "Unable to find the object". ChatGPT is particularly eager to tell me to use EvaluateJavaScript, a command that as far as I can tell only exists in one users unanswered question on StackOverflow.

      • scot1967's avatar
        scot1967
        Icon for Champion Level 1 rankChampion Level 1

        Worth a shot.  Post back if you figure it out.  Good luck!