Skip to content

Commit aef9bad

Browse files
authored
feat: assertions default timeout (#1023)
1 parent 64f7a05 commit aef9bad

File tree

8 files changed

+94
-6
lines changed

8 files changed

+94
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ Playwright is a Java library to automate [Chromium](https://www.chromium.org/Hom
1111

1212
| | Linux | macOS | Windows |
1313
| :--- | :---: | :---: | :---: |
14-
| Chromium <!-- GEN:chromium-version -->104.0.5112.57<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
14+
| Chromium <!-- GEN:chromium-version -->104.0.5112.81<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
1515
| WebKit <!-- GEN:webkit-version -->16.0<!-- GEN:stop --> ||||
16-
| Firefox <!-- GEN:firefox-version -->102.0<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
16+
| Firefox <!-- GEN:firefox-version -->103.0<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
1717

1818
Headless execution is supported for all the browsers on all platforms. Check out [system requirements](https://playwright.dev/java/docs/next/intro/#system-requirements) for details.
1919

playwright/src/main/java/com/microsoft/playwright/BrowserType.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,15 +1028,19 @@ public LaunchPersistentContextOptions setViewportSize(ViewportSize viewportSize)
10281028
}
10291029
}
10301030
/**
1031-
* This method attaches Playwright to an existing browser instance.
1031+
* This method attaches Playwright to an existing browser instance. When connecting to another browser launched via
1032+
* {@code BrowserType.launchServer} in Node.js, the major and minor version needs to match the client version (1.2.3 → is
1033+
* compatible with 1.2.x).
10321034
*
10331035
* @param wsEndpoint A browser websocket endpoint to connect to.
10341036
*/
10351037
default Browser connect(String wsEndpoint) {
10361038
return connect(wsEndpoint, null);
10371039
}
10381040
/**
1039-
* This method attaches Playwright to an existing browser instance.
1041+
* This method attaches Playwright to an existing browser instance. When connecting to another browser launched via
1042+
* {@code BrowserType.launchServer} in Node.js, the major and minor version needs to match the client version (1.2.3 → is
1043+
* compatible with 1.2.x).
10401044
*
10411045
* @param wsEndpoint A browser websocket endpoint to connect to.
10421046
*/

playwright/src/main/java/com/microsoft/playwright/assertions/PlaywrightAssertions.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.microsoft.playwright.Locator;
2121
import com.microsoft.playwright.Page;
2222
import com.microsoft.playwright.impl.APIResponseAssertionsImpl;
23+
import com.microsoft.playwright.impl.AssertionsTimeout;
2324
import com.microsoft.playwright.impl.LocatorAssertionsImpl;
2425
import com.microsoft.playwright.impl.PageAssertionsImpl;
2526

@@ -86,5 +87,17 @@ static PageAssertions assertThat(Page page) {
8687
return new PageAssertionsImpl(page);
8788
}
8889

90+
/**
91+
* Changes default timeout for Playwright assertions from 5 seconds to the speicified value.
92+
* <pre>{@code
93+
* PlaywrightAssertions.setDefaultAssertionTimeout(30_000);
94+
* }</pre>
95+
*
96+
* @param timeout Timeout in milliseconds.
97+
*/
98+
static void setDefaultAssertionTimeout(double milliseconds) {
99+
AssertionsTimeout.setDefaultTimeout(milliseconds);
100+
}
101+
89102
}
90103

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void expectImpl(String expression, List<ExpectedTextValue> expectedText, Object
5252

5353
void expectImpl(String expression, FrameExpectOptions expectOptions, Object expected, String message) {
5454
if (expectOptions.timeout == null) {
55-
expectOptions.timeout = 5_000.0;
55+
expectOptions.timeout = AssertionsTimeout.defaultTimeout;
5656
}
5757
if (expectOptions.isNot) {
5858
message = message.replace("expected to", "expected not to");
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.PlaywrightException;
20+
21+
public class AssertionsTimeout {
22+
static double defaultTimeout = 5_000;
23+
24+
public static void setDefaultTimeout(double ms) {
25+
if (ms < 0) {
26+
throw new PlaywrightException("Timeout cannot be negative");
27+
}
28+
defaultTimeout = ms;
29+
}
30+
}

playwright/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.microsoft.playwright;
1818

1919
import com.microsoft.playwright.assertions.LocatorAssertions;
20+
import com.microsoft.playwright.assertions.PlaywrightAssertions;
2021
import org.junit.jupiter.api.Disabled;
2122
import org.junit.jupiter.api.Test;
2223
import org.opentest4j.AssertionFailedError;
@@ -980,4 +981,35 @@ void locatorCountShouldWorkWithDeletedMapInMainWorld() {
980981
page.locator("#searchResultTableDiv .x-grid3-row").count();
981982
assertThat(page.locator("#searchResultTableDiv .x-grid3-row")).hasCount(0);
982983
}
984+
985+
@Test
986+
void defaultTimeoutHasTextFail() {
987+
page.setContent("<div></div>");
988+
Locator locator = page.locator("div");
989+
PlaywrightAssertions.setDefaultAssertionTimeout(1000);
990+
AssertionFailedError exception = assertThrows(AssertionFailedError.class, () -> assertThat(locator).hasText("foo"));
991+
assertTrue(exception.getMessage().contains("Locator.expect with timeout 1000ms"), exception.getMessage());
992+
// Restore default.
993+
PlaywrightAssertions.setDefaultAssertionTimeout(5_000);
994+
}
995+
996+
@Test
997+
void defaultTimeoutHasTextPass() {
998+
page.setContent("<div>foo</div>");
999+
Locator locator = page.locator("div");
1000+
PlaywrightAssertions.setDefaultAssertionTimeout(1000);
1001+
assertThat(locator).hasText("foo");
1002+
// Restore default.
1003+
PlaywrightAssertions.setDefaultAssertionTimeout(5_000);
1004+
}
1005+
1006+
@Test
1007+
void defaultTimeoutZeroHasTextPass() {
1008+
page.setContent("<div>foo</div>");
1009+
Locator locator = page.locator("div");
1010+
PlaywrightAssertions.setDefaultAssertionTimeout(0);
1011+
assertThat(locator).hasText("foo");
1012+
// Restore default.
1013+
PlaywrightAssertions.setDefaultAssertionTimeout(5_000);
1014+
}
9831015
}

scripts/CLI_VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.25.0-alpha-jul-28-2022
1+
1.25.0-alpha-1659639526000

tools/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,14 @@ void writeTo(List<String> output, String offset) {
675675
}
676676
return;
677677
}
678+
if ("PlaywrightAssertions.setDefaultAssertionTimeout".equals(jsonPath)) {
679+
writeJavadoc(params, output, offset);
680+
output.add(offset + "static void setDefaultAssertionTimeout(double milliseconds) {");
681+
output.add(offset + " AssertionsTimeout.setDefaultTimeout(milliseconds);");
682+
output.add(offset + "}");
683+
output.add("");
684+
return;
685+
}
678686
int numOverloads = 1;
679687
for (int i = 0; i < params.size(); i++) {
680688
if (params.get(i).type.isTypeUnion()) {
@@ -952,6 +960,7 @@ void writeTo(List<String> output, String offset) {
952960
output.add("import com.microsoft.playwright.Locator;");
953961
output.add("import com.microsoft.playwright.Page;");
954962
output.add("import com.microsoft.playwright.impl.APIResponseAssertionsImpl;");
963+
output.add("import com.microsoft.playwright.impl.AssertionsTimeout;");
955964
output.add("import com.microsoft.playwright.impl.LocatorAssertionsImpl;");
956965
output.add("import com.microsoft.playwright.impl.PageAssertionsImpl;");
957966
}

0 commit comments

Comments
 (0)