Skip to content

Commit

Permalink
feat(soft-assertions): Implement soft assertions for playwright-java (m…
Browse files Browse the repository at this point in the history
  • Loading branch information
uchagani committed Aug 28, 2023
1 parent fa75e29 commit 86f929a
Show file tree
Hide file tree
Showing 18 changed files with 986 additions and 13 deletions.
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>
</dependency>
</dependencies>
</project>
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() {
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));
}

private 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package com.microsoft.playwright.impl;

import com.microsoft.playwright.Locator;
import com.microsoft.playwright.assertions.LocatorAssertions;

import java.util.List;
import java.util.regex.Pattern;

public class LocatorAssertionsImplProxy extends SoftAssertionsBase implements LocatorAssertions {
private final LocatorAssertionsImpl locatorAssertionsImpl;

LocatorAssertionsImplProxy(Locator locator, List<Throwable> results) {
this(results, new LocatorAssertionsImpl(locator));
}

private LocatorAssertionsImplProxy(List<Throwable> results, LocatorAssertionsImpl locatorAssertionsImpl) {
super(results);
this.locatorAssertionsImpl = locatorAssertionsImpl;
}

@Override
public LocatorAssertions not() {
return new LocatorAssertionsImplProxy(super.results, locatorAssertionsImpl.not());
}

@Override
public void isAttached(IsAttachedOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.isAttached(options));
}

@Override
public void isChecked(IsCheckedOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.isChecked(options));
}

@Override
public void isDisabled(IsDisabledOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.isDisabled(options));
}

@Override
public void isEditable(IsEditableOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.isEditable(options));
}

@Override
public void isEmpty(IsEmptyOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.isEmpty(options));
}

@Override
public void isEnabled(IsEnabledOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.isEnabled(options));
}

@Override
public void isFocused(IsFocusedOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.isFocused(options));
}

@Override
public void isHidden(IsHiddenOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.isHidden(options));
}

@Override
public void isInViewport(IsInViewportOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.isInViewport(options));
}

@Override
public void isVisible(IsVisibleOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.isVisible(options));
}

@Override
public void containsText(String expected, ContainsTextOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.containsText(expected, options));
}

@Override
public void containsText(Pattern expected, ContainsTextOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.containsText(expected, options));
}

@Override
public void containsText(String[] expected, ContainsTextOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.containsText(expected, options));
}

@Override
public void containsText(Pattern[] expected, ContainsTextOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.containsText(expected, options));
}

@Override
public void hasAttribute(String name, String value, HasAttributeOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasAttribute(name, value, options));
}

@Override
public void hasAttribute(String name, Pattern value, HasAttributeOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasAttribute(name, value, options));
}

@Override
public void hasClass(String expected, HasClassOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasClass(expected, options));
}

@Override
public void hasClass(Pattern expected, HasClassOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasClass(expected, options));
}

@Override
public void hasClass(String[] expected, HasClassOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasClass(expected, options));
}

@Override
public void hasClass(Pattern[] expected, HasClassOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasClass(expected, options));
}

@Override
public void hasCount(int count, HasCountOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasCount(count, options));
}

@Override
public void hasCSS(String name, String value, HasCSSOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasCSS(name, value, options));
}

@Override
public void hasCSS(String name, Pattern value, HasCSSOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasCSS(name, value, options));
}

@Override
public void hasId(String id, HasIdOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasId(id, options));
}

@Override
public void hasId(Pattern id, HasIdOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasId(id, options));
}

@Override
public void hasJSProperty(String name, Object value, HasJSPropertyOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasJSProperty(name, value, options));
}

@Override
public void hasText(String expected, HasTextOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasText(expected, options));
}

@Override
public void hasText(Pattern expected, HasTextOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasText(expected, options));
}

@Override
public void hasText(String[] expected, HasTextOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasText(expected, options));
}

@Override
public void hasText(Pattern[] expected, HasTextOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasText(expected, options));
}

@Override
public void hasValue(String value, HasValueOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasValue(value, options));
}

@Override
public void hasValue(Pattern value, HasValueOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasValue(value, options));
}

@Override
public void hasValues(String[] values, HasValuesOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasValues(values, options));
}

@Override
public void hasValues(Pattern[] values, HasValuesOptions options) {
assertAndCaptureResult(() -> locatorAssertionsImpl.hasValues(values, options));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void hasURL(Pattern pattern, HasURLOptions options) {
}

@Override
public PageAssertions not() {
public PageAssertionsImpl not() {
return new PageAssertionsImpl(actualPage, !isNot);
}
}
Expand Down
Loading

0 comments on commit 86f929a

Please sign in to comment.