1
0
mirror of synced 2026-08-31 19:55:37 +00:00

fix(api): throw TimeoutError on timeout (#323)

This commit is contained in:
Yury Semikhatsky
2021-03-05 13:18:24 -08:00
committed by GitHub
parent d57c449aef
commit 86c06c4fd3
10 changed files with 33 additions and 17 deletions
@@ -16,12 +16,17 @@
package com.microsoft.playwright;
import java.util.*;
/**
* TimeoutError is emitted whenever certain operations are terminated due to timeout, e.g. {@link Page#waitForSelector
* Page.waitForSelector()} or {@link BrowserType#launch BrowserType.launch()}.
*/
public interface TimeoutError {
public class TimeoutError extends PlaywrightException {
public TimeoutError(String message) {
super(message);
}
public TimeoutError(String message, Throwable exception) {
super(message, exception);
}
}
@@ -20,6 +20,7 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.microsoft.playwright.PlaywrightException;
import com.microsoft.playwright.TimeoutError;
import java.io.File;
import java.io.IOException;
@@ -200,10 +201,12 @@ public class Connection {
if (message.error == null) {
callback.complete(message.result);
} else {
if (message.error.error != null) {
callback.completeExceptionally(new DriverException(message.error.error));
} else {
if (message.error.error == null) {
callback.completeExceptionally(new PlaywrightException(message.error.toString()));
} else if ("TimeoutError".equals(message.error.error.name)) {
callback.completeExceptionally(new TimeoutError(message.error.error.toString()));
} else {
callback.completeExceptionally(new DriverException(message.error.error));
}
}
return;
@@ -17,6 +17,7 @@
package com.microsoft.playwright.impl;
import com.microsoft.playwright.PlaywrightException;
import com.microsoft.playwright.TimeoutError;
class WaitableResult<T> implements Waitable<T> {
private T result;
@@ -47,6 +48,9 @@ class WaitableResult<T> implements Waitable<T> {
@Override
public T get() {
if (exception != null) {
if (exception instanceof TimeoutError) {
throw new TimeoutError(exception.getMessage(), exception);
}
throw new PlaywrightException(exception.getMessage(), exception);
}
return result;
@@ -16,7 +16,7 @@
package com.microsoft.playwright.impl;
import com.microsoft.playwright.PlaywrightException;
import com.microsoft.playwright.TimeoutError;
class WaitableTimeout<T> implements Waitable<T> {
private final long deadline;
@@ -38,7 +38,7 @@ class WaitableTimeout<T> implements Waitable<T> {
if (timeoutStr.endsWith(".0")) {
timeoutStr = timeoutStr.substring(0, timeoutStr.length() - 2);
}
throw new PlaywrightException("Timeout " + timeoutStr + "ms exceeded");
throw new TimeoutError("Timeout " + timeoutStr + "ms exceeded");
}
@Override
@@ -54,7 +54,7 @@ public class TestElementHandleWaitForElementState extends TestBase {
try {
div.waitForElementState(VISIBLE, new ElementHandle.WaitForElementStateOptions().withTimeout(1000));
fail("did not throw");
} catch (PlaywrightException e) {
} catch (TimeoutError e) {
assertTrue(e.getMessage().contains("Timeout 1000ms exceeded"));
}
}
@@ -45,7 +45,7 @@ public class TestFrameNavigate extends TestBase {
String url = server.PREFIX + "/frames/child-redirect.html";
try {
page.navigate(url, new Page.NavigateOptions().withTimeout(5000).withWaitUntil(NETWORKIDLE));
} catch (PlaywrightException e) {
} catch (TimeoutError e) {
assertTrue(e.getMessage().contains("Timeout 5000ms exceeded."));
assertTrue(e.getMessage().contains("navigating to \"" + url +"\", waiting until \"networkidle\""));
}
@@ -129,7 +129,7 @@ public class TestPageSetInputFiles extends TestBase {
try {
page.waitForFileChooser(new Page.WaitForFileChooserOptions().withTimeout(1), () -> {});
fail("did not throw");
} catch (PlaywrightException e) {
} catch (TimeoutError e) {
assertTrue(e.getMessage().contains("Timeout 1ms exceeded"));
}
}
@@ -140,7 +140,7 @@ public class TestPageSetInputFiles extends TestBase {
try {
page.waitForFileChooser(() -> {});
fail("did not throw");
} catch (PlaywrightException e) {
} catch (TimeoutError e) {
assertTrue(e.getMessage().contains("Timeout 1ms exceeded"));
}
}
@@ -151,7 +151,7 @@ public class TestPageSetInputFiles extends TestBase {
try {
page.waitForFileChooser(new Page.WaitForFileChooserOptions().withTimeout(1), () -> {});
fail("did not throw");
} catch (PlaywrightException e) {
} catch (TimeoutError e) {
assertTrue(e.getMessage().contains("Timeout 1ms exceeded"));
}
}
@@ -45,7 +45,7 @@ public class TestPageWaitForNavigation extends TestBase {
new Page.WaitForNavigationOptions().withUrl("**/frame.html").withTimeout(5000),
() -> page.navigate(server.EMPTY_PAGE));
fail("did not throw");
} catch (PlaywrightException e) {
} catch (TimeoutError e) {
assertTrue(e.getMessage().contains("Timeout 5000ms exceeded"));
// assertTrue(e.getMessage().contains("waiting for navigation to '**/frame.html' until 'load'"));
// assertTrue(e.getMessage().contains("navigated to '${server.EMPTY_PAGE}'"));
@@ -80,7 +80,7 @@ public class TestWaitForFunction extends TestBase {
" console.log(window['counter']);\n" +
"}", null, new Page.WaitForFunctionOptions().withPollingInterval(1).withTimeout(1000));
fail("did not throw");
} catch (PlaywrightException e) {
} catch (TimeoutError e) {
assertTrue(e.getMessage().contains("Timeout 1000ms exceeded"));
}
@@ -180,7 +180,7 @@ public class TestWaitForFunction extends TestBase {
try {
page.waitForFunction("false", null, new Page.WaitForFunctionOptions().withTimeout(10));
fail("did not throw");
} catch (PlaywrightException e) {
} catch (TimeoutError e) {
assertTrue(e.getMessage().contains("Timeout 10ms exceeded"));
}
}
@@ -191,7 +191,7 @@ public class TestWaitForFunction extends TestBase {
try {
page.waitForFunction("false");
fail("did not throw");
} catch (PlaywrightException e) {
} catch (TimeoutError e) {
assertTrue(e.getMessage().contains("Timeout 1ms exceeded"));
}
}