Skip to content

Commit 86f929a

Browse files
authored
feat(soft-assertions): Implement soft assertions for playwright-java (#1361)
1 parent fa75e29 commit 86f929a

File tree

18 files changed

+986
-13
lines changed

18 files changed

+986
-13
lines changed

playwright/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,9 @@
7373
<groupId>com.microsoft.playwright</groupId>
7474
<artifactId>driver-bundle</artifactId>
7575
</dependency>
76+
<dependency>
77+
<groupId>org.mockito</groupId>
78+
<artifactId>mockito-junit-jupiter</artifactId>
79+
</dependency>
7680
</dependencies>
7781
</project>
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.microsoft.playwright.assertions;
18+
19+
import com.microsoft.playwright.impl.SoftAssertionsImpl;
20+
import com.microsoft.playwright.APIResponse;
21+
import com.microsoft.playwright.Locator;
22+
import com.microsoft.playwright.Page;
23+
24+
/**
25+
* The {@code SoftAssertions} class provides assertion methods that can be used to make multiple assertions without failing
26+
* the test immediately.
27+
* <pre>{@code
28+
* ...
29+
* import com.microsoft.playwright.assertions.SoftAssertions;
30+
*
31+
* public class TestPage {
32+
* ...
33+
* @Test
34+
* void hasUrlTextPass() {
35+
* SoftAssertions softly = SoftAssertions.create();
36+
* page.getByText("Sign in").click();
37+
* softly.assertThat(page).hasURL(Pattern.compile(".*\/login"));
38+
* softly.assertAll();
39+
* }
40+
* }
41+
* }</pre>
42+
*/
43+
public interface SoftAssertions {
44+
/**
45+
* Creates a {@code SoftAssertions} object.
46+
*
47+
* <p> **Usage**
48+
* <pre>{@code
49+
* SoftAssertions softly = SoftAssertions.create();
50+
* }</pre>
51+
*
52+
* @since v1.38
53+
*/
54+
static SoftAssertions create() {
55+
return new SoftAssertionsImpl();
56+
}
57+
/**
58+
* Creates a {@code LocatorAssertions} object for the given {@code Locator}.
59+
*
60+
* <p> **Usage**
61+
* <pre>{@code
62+
* SoftAssertions softly = SoftAssertions.create();
63+
* ...
64+
* softly.assertThat(locator).isVisible();
65+
* }</pre>
66+
*
67+
* @param locator {@code Locator} object to use for assertions.
68+
* @since v1.38
69+
*/
70+
LocatorAssertions assertThat(Locator locator);
71+
/**
72+
* Creates a {@code PageAssertions} object for the given {@code Page}.
73+
*
74+
* <p> **Usage**
75+
* <pre>{@code
76+
* SoftAssertions softly = SoftAssertions.create();
77+
* ...
78+
* softly.assertThat(page).hasTitle("News");
79+
* }</pre>
80+
*
81+
* @param page {@code Page} object to use for assertions.
82+
* @since v1.38
83+
*/
84+
PageAssertions assertThat(Page page);
85+
/**
86+
* Creates a {@code APIResponseAssertions} object for the given {@code APIResponse}.
87+
*
88+
* <p> **Usage**
89+
* <pre>{@code
90+
* SoftAssertions softly = SoftAssertions.create();
91+
* ...
92+
* softly.assertThat(response).isOK();
93+
* }</pre>
94+
*
95+
* @param response {@code APIResponse} object to use for assertions.
96+
* @since v1.38
97+
*/
98+
APIResponseAssertions assertThat(APIResponse response);
99+
/**
100+
* Runs all the assertions have been executed for this {@code SoftAssertions} object. If any assertions fail, this method
101+
* throws an AssertionFailedError with the details of all the failed assertions.
102+
*
103+
* <p> **Usage**
104+
* <pre>{@code
105+
* SoftAssertions softly = SoftAssertions.create();
106+
* ...
107+
* softly.assertAll();
108+
* }</pre>
109+
*
110+
* @since v1.38
111+
*/
112+
void assertAll();
113+
}
114+

playwright/src/main/java/com/microsoft/playwright/impl/APIResponseAssertionsImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public APIResponseAssertionsImpl(APIResponse response) {
3737
}
3838

3939
@Override
40-
public APIResponseAssertions not() {
40+
public APIResponseAssertionsImpl not() {
4141
return new APIResponseAssertionsImpl(actual, !isNot);
4242
}
4343

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.microsoft.playwright.impl;
18+
19+
import com.microsoft.playwright.APIResponse;
20+
import com.microsoft.playwright.assertions.APIResponseAssertions;
21+
22+
import java.util.List;
23+
24+
public class APIResponseAssertionsImplProxy extends SoftAssertionsBase implements APIResponseAssertions {
25+
private final APIResponseAssertionsImpl apiResponseAssertionsImpl;
26+
27+
APIResponseAssertionsImplProxy(APIResponse response, List<Throwable> results) {
28+
this(results, new APIResponseAssertionsImpl(response));
29+
}
30+
31+
private APIResponseAssertionsImplProxy(List<Throwable> results, APIResponseAssertionsImpl apiResponseAssertionsImpl) {
32+
super(results);
33+
this.apiResponseAssertionsImpl = apiResponseAssertionsImpl;
34+
}
35+
36+
@Override
37+
public APIResponseAssertions not() {
38+
return new APIResponseAssertionsImplProxy(super.results, apiResponseAssertionsImpl.not());
39+
}
40+
41+
@Override
42+
public void isOK() {
43+
assertAndCaptureResult(apiResponseAssertionsImpl::isOK);
44+
}
45+
}

playwright/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ private void expectTrue(String expression, String message, FrameExpectOptions op
353353
}
354354

355355
@Override
356-
public LocatorAssertions not() {
356+
public LocatorAssertionsImpl not() {
357357
return new LocatorAssertionsImpl(actualLocator, !isNot);
358358
}
359359

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
package com.microsoft.playwright.impl;
2+
3+
import com.microsoft.playwright.Locator;
4+
import com.microsoft.playwright.assertions.LocatorAssertions;
5+
6+
import java.util.List;
7+
import java.util.regex.Pattern;
8+
9+
public class LocatorAssertionsImplProxy extends SoftAssertionsBase implements LocatorAssertions {
10+
private final LocatorAssertionsImpl locatorAssertionsImpl;
11+
12+
LocatorAssertionsImplProxy(Locator locator, List<Throwable> results) {
13+
this(results, new LocatorAssertionsImpl(locator));
14+
}
15+
16+
private LocatorAssertionsImplProxy(List<Throwable> results, LocatorAssertionsImpl locatorAssertionsImpl) {
17+
super(results);
18+
this.locatorAssertionsImpl = locatorAssertionsImpl;
19+
}
20+
21+
@Override
22+
public LocatorAssertions not() {
23+
return new LocatorAssertionsImplProxy(super.results, locatorAssertionsImpl.not());
24+
}
25+
26+
@Override
27+
public void isAttached(IsAttachedOptions options) {
28+
assertAndCaptureResult(() -> locatorAssertionsImpl.isAttached(options));
29+
}
30+
31+
@Override
32+
public void isChecked(IsCheckedOptions options) {
33+
assertAndCaptureResult(() -> locatorAssertionsImpl.isChecked(options));
34+
}
35+
36+
@Override
37+
public void isDisabled(IsDisabledOptions options) {
38+
assertAndCaptureResult(() -> locatorAssertionsImpl.isDisabled(options));
39+
}
40+
41+
@Override
42+
public void isEditable(IsEditableOptions options) {
43+
assertAndCaptureResult(() -> locatorAssertionsImpl.isEditable(options));
44+
}
45+
46+
@Override
47+
public void isEmpty(IsEmptyOptions options) {
48+
assertAndCaptureResult(() -> locatorAssertionsImpl.isEmpty(options));
49+
}
50+
51+
@Override
52+
public void isEnabled(IsEnabledOptions options) {
53+
assertAndCaptureResult(() -> locatorAssertionsImpl.isEnabled(options));
54+
}
55+
56+
@Override
57+
public void isFocused(IsFocusedOptions options) {
58+
assertAndCaptureResult(() -> locatorAssertionsImpl.isFocused(options));
59+
}
60+
61+
@Override
62+
public void isHidden(IsHiddenOptions options) {
63+
assertAndCaptureResult(() -> locatorAssertionsImpl.isHidden(options));
64+
}
65+
66+
@Override
67+
public void isInViewport(IsInViewportOptions options) {
68+
assertAndCaptureResult(() -> locatorAssertionsImpl.isInViewport(options));
69+
}
70+
71+
@Override
72+
public void isVisible(IsVisibleOptions options) {
73+
assertAndCaptureResult(() -> locatorAssertionsImpl.isVisible(options));
74+
}
75+
76+
@Override
77+
public void containsText(String expected, ContainsTextOptions options) {
78+
assertAndCaptureResult(() -> locatorAssertionsImpl.containsText(expected, options));
79+
}
80+
81+
@Override
82+
public void containsText(Pattern expected, ContainsTextOptions options) {
83+
assertAndCaptureResult(() -> locatorAssertionsImpl.containsText(expected, options));
84+
}
85+
86+
@Override
87+
public void containsText(String[] expected, ContainsTextOptions options) {
88+
assertAndCaptureResult(() -> locatorAssertionsImpl.containsText(expected, options));
89+
}
90+
91+
@Override
92+
public void containsText(Pattern[] expected, ContainsTextOptions options) {
93+
assertAndCaptureResult(() -> locatorAssertionsImpl.containsText(expected, options));
94+
}
95+
96+
@Override
97+
public void hasAttribute(String name, String value, HasAttributeOptions options) {
98+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasAttribute(name, value, options));
99+
}
100+
101+
@Override
102+
public void hasAttribute(String name, Pattern value, HasAttributeOptions options) {
103+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasAttribute(name, value, options));
104+
}
105+
106+
@Override
107+
public void hasClass(String expected, HasClassOptions options) {
108+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasClass(expected, options));
109+
}
110+
111+
@Override
112+
public void hasClass(Pattern expected, HasClassOptions options) {
113+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasClass(expected, options));
114+
}
115+
116+
@Override
117+
public void hasClass(String[] expected, HasClassOptions options) {
118+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasClass(expected, options));
119+
}
120+
121+
@Override
122+
public void hasClass(Pattern[] expected, HasClassOptions options) {
123+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasClass(expected, options));
124+
}
125+
126+
@Override
127+
public void hasCount(int count, HasCountOptions options) {
128+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasCount(count, options));
129+
}
130+
131+
@Override
132+
public void hasCSS(String name, String value, HasCSSOptions options) {
133+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasCSS(name, value, options));
134+
}
135+
136+
@Override
137+
public void hasCSS(String name, Pattern value, HasCSSOptions options) {
138+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasCSS(name, value, options));
139+
}
140+
141+
@Override
142+
public void hasId(String id, HasIdOptions options) {
143+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasId(id, options));
144+
}
145+
146+
@Override
147+
public void hasId(Pattern id, HasIdOptions options) {
148+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasId(id, options));
149+
}
150+
151+
@Override
152+
public void hasJSProperty(String name, Object value, HasJSPropertyOptions options) {
153+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasJSProperty(name, value, options));
154+
}
155+
156+
@Override
157+
public void hasText(String expected, HasTextOptions options) {
158+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasText(expected, options));
159+
}
160+
161+
@Override
162+
public void hasText(Pattern expected, HasTextOptions options) {
163+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasText(expected, options));
164+
}
165+
166+
@Override
167+
public void hasText(String[] expected, HasTextOptions options) {
168+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasText(expected, options));
169+
}
170+
171+
@Override
172+
public void hasText(Pattern[] expected, HasTextOptions options) {
173+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasText(expected, options));
174+
}
175+
176+
@Override
177+
public void hasValue(String value, HasValueOptions options) {
178+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasValue(value, options));
179+
}
180+
181+
@Override
182+
public void hasValue(Pattern value, HasValueOptions options) {
183+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasValue(value, options));
184+
}
185+
186+
@Override
187+
public void hasValues(String[] values, HasValuesOptions options) {
188+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasValues(values, options));
189+
}
190+
191+
@Override
192+
public void hasValues(Pattern[] values, HasValuesOptions options) {
193+
assertAndCaptureResult(() -> locatorAssertionsImpl.hasValues(values, options));
194+
}
195+
}

playwright/src/main/java/com/microsoft/playwright/impl/PageAssertionsImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void hasURL(Pattern pattern, HasURLOptions options) {
6767
}
6868

6969
@Override
70-
public PageAssertions not() {
70+
public PageAssertionsImpl not() {
7171
return new PageAssertionsImpl(actualPage, !isNot);
7272
}
7373
}

0 commit comments

Comments
 (0)