Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(soft-assertions): Implement soft assertions for playwright-java #1361

Merged
merged 9 commits into from
Aug 28, 2023
4 changes: 4 additions & 0 deletions playwright/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,9 @@
<groupId>com.microsoft.playwright</groupId>
<artifactId>driver-bundle</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
uchagani marked this conversation as resolved.
Show resolved Hide resolved
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ public interface BrowserContext extends AutoCloseable {
*/
void offPage(Consumer<Page> handler);

/**
* Emitted when unhandled exceptions occur on any pages created through this context. To only listen for {@code pageError}
* events from a particular page, use {@link Page#onPageError Page.onPageError()}.
*/
void onPageError(Consumer<PageError> handler);
uchagani marked this conversation as resolved.
Show resolved Hide resolved
/**
* Removes handler that was previously added with {@link #onPageError onPageError(handler)}.
*/
void offPageError(Consumer<PageError> handler);

/**
* Emitted when a request is issued from any pages created through this context. The [request] object is read-only. To only
* listen for requests from a particular page, use {@link Page#onRequest Page.onRequest()}.
Expand Down
17 changes: 12 additions & 5 deletions playwright/src/main/java/com/microsoft/playwright/Download.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@
*
* <p> All the downloaded files belonging to the browser context are deleted when the browser context is closed.
*
* <p> Download event is emitted once the download starts. Download path becomes available once download completes:
* <p> Download event is emitted once the download starts. Download path becomes available once download completes.
* <pre>{@code
* // wait for download to start
* // Wait for the download to start
* Download download = page.waitForDownload(() -> {
* page.getByText("Download file").click();
* // Perform the action that initiates download
* page.getByText("Download file").click();
* });
* // wait for download to complete
* Path path = download.path();
*
* // Wait for the download process to complete and save the downloaded file somewhere
* download.saveAs(Paths.get("/path/to/save/at/", download.suggestedFilename()));
* }</pre>
*/
public interface Download {
Expand Down Expand Up @@ -80,6 +82,11 @@ public interface Download {
* Copy the download to a user-specified path. It is safe to call this method while the download is still in progress. Will
* wait for the download to finish if necessary.
*
* <p> **Usage**
* <pre>{@code
* download.saveAs(Paths.get("/path/to/save/at/", download.suggestedFilename()));
* }</pre>
*
* @param path Path where the download should be copied.
* @since v1.8
*/
Expand Down
47 changes: 47 additions & 0 deletions playwright/src/main/java/com/microsoft/playwright/PageError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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;


/**
* {@code PageError} class represents objects created by context when there are unhandled execeptions thrown on the pages
* and dispatched via the {@link BrowserContext#onPageError BrowserContext.onPageError()} event.
* <pre>{@code
* // Log all uncaught errors to the terminal
* context.onPageError(pagerror -> {
* System.out.println("Uncaught exception: " + pagerror.error());
* });
*
* // Navigate to a page with an exception.
* page.navigate("data:text/html,<script>throw new Error('Test')</script>");
* }</pre>
*/
public interface PageError {
/**
* The page that produced this unhandled exception, if any.
*
* @since v1.38
*/
Page page();
/**
* Unhandled error that was thrown.
*
* @since v1.38
*/
Error error();
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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.assertions;

import com.microsoft.playwright.impl.SoftAssertionsImpl;
import com.microsoft.playwright.APIResponse;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;

/**
* The {@code SoftAssertions} class provides assertion methods that can be used to make multiple assertions without failing
* the test immediately.
* <pre>{@code
* ...
* import com.microsoft.playwright.assertions.SoftAssertions;
*
* public class TestPage {
* ...
* @Test
* void hasUrlTextPass() {
* SoftAssertions softly = SoftAssertions.create();
* page.getByText("Sign in").click();
* softly.assertThat(page).hasURL(Pattern.compile(".*\/login"));
* softly.assertAll();
* }
* }
* }</pre>
*/
public interface SoftAssertions {
/**
* Creates a {@code SoftAssertions} object.
*
* <p> **Usage**
* <pre>{@code
* SoftAssertions softly = SoftAssertions.create();
* }</pre>
*
* @since v1.38
*/
static SoftAssertions create() {
return new SoftAssertionsImpl();
}
/**
* Creates a {@code LocatorAssertions} object for the given {@code Locator}.
*
* <p> **Usage**
* <pre>{@code
* SoftAssertions softly = SoftAssertions.create();
* ...
* softly.assertThat(locator).isVisible();
* }</pre>
*
* @param locator {@code Locator} object to use for assertions.
* @since v1.38
*/
LocatorAssertions assertThat(Locator locator);
/**
* Creates a {@code PageAssertions} object for the given {@code Page}.
*
* <p> **Usage**
* <pre>{@code
* SoftAssertions softly = SoftAssertions.create();
* ...
* softly.assertThat(page).hasTitle("News");
* }</pre>
*
* @param page {@code Page} object to use for assertions.
* @since v1.38
*/
PageAssertions assertThat(Page page);
/**
* Creates a {@code APIResponseAssertions} object for the given {@code APIResponse}.
*
* <p> **Usage**
* <pre>{@code
* SoftAssertions softly = SoftAssertions.create();
* ...
* softly.assertThat(response).isOK();
* }</pre>
*
* @param response {@code APIResponse} object to use for assertions.
* @since v1.38
*/
APIResponseAssertions assertThat(APIResponse response);
/**
* Runs all the assertions have been executed for this {@code SoftAssertions} object. If any assertions fail, this method
* throws an AssertionFailedError with the details of all the failed assertions.
*
* <p> **Usage**
* <pre>{@code
* SoftAssertions softly = SoftAssertions.create();
* ...
* softly.assertAll();
* }</pre>
*
* @since v1.38
*/
void assertAll();
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public APIResponseAssertionsImpl(APIResponse response) {
}

@Override
public APIResponseAssertions not() {
public APIResponseAssertionsImpl not() {
yury-s marked this conversation as resolved.
Show resolved Hide resolved
return new APIResponseAssertionsImpl(actual, !isNot);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.impl;

import com.microsoft.playwright.APIResponse;
import com.microsoft.playwright.assertions.APIResponseAssertions;

import java.util.List;

public class APIResponseAssertionsImplProxy extends SoftAssertionsBase implements APIResponseAssertions {
private final APIResponseAssertionsImpl apiResponseAssertionsImpl;

APIResponseAssertionsImplProxy(APIResponse response, List<Throwable> results) {
this(results, new APIResponseAssertionsImpl(response));
}

public APIResponseAssertionsImplProxy(List<Throwable> results, APIResponseAssertionsImpl apiResponseAssertionsImpl) {
super(results);
this.apiResponseAssertionsImpl = apiResponseAssertionsImpl;
}

@Override
public APIResponseAssertions not() {
return new APIResponseAssertionsImplProxy(super.results, apiResponseAssertionsImpl.not());
}

@Override
public void isOK() {
assertAndCaptureResult(apiResponseAssertionsImpl::isOK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ private void expectTrue(String expression, String message, FrameExpectOptions op
}

@Override
public LocatorAssertions not() {
public LocatorAssertionsImpl not() {
return new LocatorAssertionsImpl(actualLocator, !isNot);
}

Expand Down
Loading