|
| 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; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.api.condition.DisabledOnOs; |
| 21 | +import org.junit.jupiter.api.condition.OS; |
| 22 | + |
| 23 | +import java.lang.reflect.Field; |
| 24 | + |
| 25 | +import static org.junit.jupiter.api.Assertions.*; |
| 26 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 27 | + |
| 28 | +public class TestUnresponsiveDriver { |
| 29 | + @Test |
| 30 | + @DisabledOnOs(OS.WINDOWS) |
| 31 | + void shouldThrowTimeoutErrorWhenDriverStopsResponding() throws Exception { |
| 32 | + try (Playwright playwright = Playwright.create()) { |
| 33 | + Browser browser = Utils.getBrowserTypeFromEnv(playwright).launch(TestBase.createLaunchOptions()); |
| 34 | + Page page = browser.newPage(); |
| 35 | + long pid = driverPid(playwright); |
| 36 | + signal("STOP", pid); |
| 37 | + try { |
| 38 | + long start = System.nanoTime(); |
| 39 | + TimeoutError e = assertThrows(TimeoutError.class, |
| 40 | + () -> page.waitForFunction("() => false", null, new Page.WaitForFunctionOptions().setTimeout(100))); |
| 41 | + long elapsedMs = (System.nanoTime() - start) / 1_000_000; |
| 42 | + assertTrue(e.getMessage().contains("Timeout 100ms exceeded while waiting for the driver to respond to waitForFunction"), e.getMessage()); |
| 43 | + assertTrue(elapsedMs < 20_000, "elapsed: " + elapsedMs); |
| 44 | + } finally { |
| 45 | + signal("CONT", pid); |
| 46 | + } |
| 47 | + // The driver's late reply to the abandoned call must not break the connection. |
| 48 | + assertEquals(2, page.evaluate("1 + 1")); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private static long driverPid(Playwright playwright) throws Exception { |
| 53 | + Field field = playwright.getClass().getDeclaredField("driverProcess"); |
| 54 | + field.setAccessible(true); |
| 55 | + Process process = (Process) field.get(playwright); |
| 56 | + long pid = -1; |
| 57 | + try { |
| 58 | + // Process.pid() is only available since Java 9. |
| 59 | + pid = (long) Process.class.getMethod("pid").invoke(process); |
| 60 | + } catch (NoSuchMethodException e) { |
| 61 | + assumeTrue(false, "Process.pid() is not available"); |
| 62 | + } |
| 63 | + return pid; |
| 64 | + } |
| 65 | + |
| 66 | + private static void signal(String signal, long pid) throws Exception { |
| 67 | + Process kill = new ProcessBuilder("kill", "-" + signal, Long.toString(pid)).inheritIO().start(); |
| 68 | + assertEquals(0, kill.waitFor()); |
| 69 | + } |
| 70 | +} |
0 commit comments