Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions playwright/src/main/java/com/microsoft/playwright/Coverage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.microsoft.playwright;


import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

/**
* {@code Coverage} gathers information about parts of JavaScript and CSS that were used by the page.
*
* <p> An example of using {@code Coverage} class to generate v8 report:
* <pre>{@code
* import com.microsoft.playwright.*;
*
* public class Example {
* public static void main(String[] args) {
* try (Playwright playwright = Playwright.create()) {
* BrowserType chromium = playwright.chromium();
* Browser browser = chromium.launch();
* Page page = browser.newPage();
* Coverage coverage = page.coverage().startJSCoverage();
* ... some tests and assertions...
* CoverageReport v8Report = coverage.stopJSCoverage();
* browser.close();
* }
* }
* }
* }</pre>
*
* <p> <strong>NOTE:</strong> Coverage APIs are only supported on Chromium-based browsers.
*/
public interface Coverage {
class CoverageReport {
/**
* Entries containing coverage reports
*/
public JsonArray entries;

public CoverageReport addEntry(JsonObject entry) {
if (entries == null) {
entries = new JsonArray();
}
entries.add(entry);
return this;
}
}

class CoverageCSSOptions {
/**
* Whether to reset coverage on every navigation. Defaults to `true`.
*/
public Boolean resetOnNavigation;

public CoverageCSSOptions setResetOnNavigation(Boolean restOnNavigation) {
this.resetOnNavigation = restOnNavigation;
return this;
}
}

class CoverageJSOptions {
/**
* Whether to reset coverage on every navigation. Defaults to `true`.
*/
public Boolean resetOnNavigation;
/**
* Whether anonymous scripts generated by the page should be reported. Defaults to `false`.
*/
public Boolean reportAnonymousScripts;

public CoverageJSOptions setResetOnNavigation(Boolean resetOnNavigation) {
this.resetOnNavigation = resetOnNavigation;
return this;
}

public CoverageJSOptions setReportAnonymousScripts(Boolean reportAnonymousScripts) {
this.reportAnonymousScripts = reportAnonymousScripts;
return this;
}
}

/**
* Returns coverage is started
*/
void startCSSCoverage();

/**
* Returns coverage is started
*/
void startCSSCoverage(CoverageCSSOptions options);

/**
* Returns the array of coverage reports for all stylesheets
*
* <p> <strong>NOTE:</strong>CSS Coverage doesn't include dynamically injected style tags without sourceURLs.
*
*/
CoverageReport stopCSSCoverage();

/**
* Returns coverage is started
*
* <p> <strong>NOTE:</strong> Anonymous scripts are ones that don't have an associated url. These are scripts that are dynamically
* created on the page using `eval` or `new Function`. If {@link com.microsoft.playwright.Coverage.CoverageJSOptions#reportAnonymousScripts}
* is set to `true`, anonymous scripts will have `__playwright_evaluation_script__` as their URL.
*/
void startJSCoverage();

/**
* Returns coverage is started
*
* <p> <strong>NOTE:</strong> Anonymous scripts are ones that don't have an associated url. These are scripts that are dynamically
* created on the page using `eval` or `new Function`. If {@link com.microsoft.playwright.Coverage.CoverageJSOptions#reportAnonymousScripts}
* is set to `true`, anonymous scripts will have `__playwright_evaluation_script__` as their URL.
*/
void startJSCoverage(CoverageJSOptions options);

/**
* Returns the array of coverage reports for all scripts
*
* <p> <strong>NOTE:</strong> JavaScript Coverage doesn't include anonymous scripts by default. However, scripts with sourceURLs are
* reported.
*
*/
CoverageReport stopJSCoverage();
}

4 changes: 4 additions & 0 deletions playwright/src/main/java/com/microsoft/playwright/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -4097,6 +4097,10 @@ default void close() {
* @since v1.8
*/
BrowserContext context();
/**
* Get the coverage report associated to the page.
*/
Coverage coverage();
/**
* This method double clicks an element matching {@code selector} by performing the following steps:
* <ol>
Expand Down
Loading