@Test public void shouldIncludeUnloadJSInSavedReport() throws Exception { File jsonFile = new File(getReportDir() + "/jscoverage.json"); if (jsonFile.exists()) jsonFile.delete(); HtmlPage page = webClient.getPage("http://localhost:9001/jscoverage.html?index.html"); page.getHtmlElementById("summaryTab").click(); webClient.waitForBackgroundJavaScript(2000); assertEquals("77%", page.getElementById("summaryTotal").getTextContent()); verifyCoverage(page, "/root.js", "80%", "50%", "100%"); verifyCoverage(page, "/level1/level1.js", "75%", "50%", "N/A"); page.getHtmlElementById("storeTab").click(); webClient.waitForBackgroundJavaScript(500); HtmlElement storeButton = page.getHtmlElementById("storeButton"); storeButton.click(); webClient.waitForBackgroundJavaScript(2000); String result = page.getElementById("storeDiv").getTextContent(); assertThat( result, containsString("Coverage data stored at " + new File(getReportDir()).getPath())); String json = ioUtils.toString(jsonFile); assertThat(json, containsString("/root.js")); assertThat(json, containsString("/level1/level2/level2.js")); String url = "file:///" + new File(getReportDir() + "/jscoverage.html").getAbsolutePath(); page = webClient.getPage(url); webClient.waitForBackgroundJavaScript(1000); assertEquals("53%", page.getElementById("summaryTotal").getTextContent()); assertEquals("33%", page.getElementById("branchSummaryTotal").getTextContent()); assertEquals("50%", page.getElementById("functionSummaryTotal").getTextContent()); verifyCoverage(page, "/root.js", "80%", "50%", "100%"); verifyCoverage(page, "/level1/level1.js", "75%", "50%", "N/A"); verifyCoverage(page, "/level1/level2/level2.js", "0%", "0%", "0%"); }
public void saveJSONData( File reportDir, String data, List<ScriptCoverageCount> unloadJSData, UriFileTranslator uriFileTranslator) { try { lockOnReportDir(reportDir); reportDir.mkdirs(); File jsonFile = new File(reportDir, "jscoverage.json"); SortedMap<String, FileData> dataMap = new TreeMap<String, FileData>(); if (jsonFile.exists()) { logger.info("Saving/merging JSON with existing JSON"); String existingJSON = ioUtils.toString(jsonFile); if (uriFileTranslator.mutates()) { translateUris(data, uriFileTranslator, dataMap); dataMap = jsonDataMerger.mergeJSONCoverageMaps(dataMap, jsonDataMerger.jsonToMap(existingJSON)); } else dataMap.putAll(jsonDataMerger.mergeJSONCoverageStrings(existingJSON, data)); ioUtils.copy(jsonDataMerger.toJSON(dataMap), jsonFile); } else if (unloadJSData != null) { logger.info("Saving/merging JSON with unloaded JavaScript JSON"); // Only scan for unloaded JS if JSON not saved before dataMap.putAll(jsonDataMerger.createEmptyJSON(unloadJSData)); if (uriFileTranslator.mutates()) translateUris(data, uriFileTranslator, dataMap); else dataMap.putAll(jsonDataMerger.jsonToMap(data)); ioUtils.copy(jsonDataMerger.toJSON(dataMap), jsonFile); } else { logger.info("Saving JSON"); if (uriFileTranslator.mutates()) { translateUris(data, uriFileTranslator, dataMap); ioUtils.copy(jsonDataMerger.toJSON(dataMap), jsonFile); } else ioUtils.copy(data, jsonFile); } } finally { unlockOnReportDir(reportDir); } }