Skip to content

Commit 2494e69

Browse files
Tom-Trumperbarancev
authored andcommitted
Improved TimeoutException for wrapped WebDrivers
Modified WebDriverWait TimeoutException to include session ID and capabilities for WebDrivers that are wrapped e.g. in an EventFiringWebDriver instance. Fixes #4408. Signed-off-by: Alexei Barantsev <[email protected]>
1 parent 2d4541f commit 2494e69

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

java/client/src/org/openqa/selenium/support/ui/WebDriverWait.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.openqa.selenium.TimeoutException;
2222
import org.openqa.selenium.WebDriver;
2323
import org.openqa.selenium.WebDriverException;
24+
import org.openqa.selenium.internal.WrapsDriver;
2425
import org.openqa.selenium.remote.RemoteWebDriver;
2526

2627
import java.util.concurrent.TimeUnit;
@@ -77,10 +78,14 @@ public WebDriverWait(WebDriver driver, Clock clock, Sleeper sleeper, long timeOu
7778

7879
@Override
7980
protected RuntimeException timeoutException(String message, Throwable lastException) {
81+
WebDriver exceptionDriver = driver;
8082
TimeoutException ex = new TimeoutException(message, lastException);
81-
ex.addInfo(WebDriverException.DRIVER_INFO, driver.getClass().getName());
82-
if (driver instanceof RemoteWebDriver) {
83-
RemoteWebDriver remote = (RemoteWebDriver) driver;
83+
ex.addInfo(WebDriverException.DRIVER_INFO, exceptionDriver.getClass().getName());
84+
while (exceptionDriver instanceof WrapsDriver) {
85+
exceptionDriver = ((WrapsDriver) exceptionDriver).getWrappedDriver();
86+
}
87+
if (exceptionDriver instanceof RemoteWebDriver) {
88+
RemoteWebDriver remote = (RemoteWebDriver) exceptionDriver;
8489
if (remote.getSessionId() != null) {
8590
ex.addInfo(WebDriverException.SESSION_ID, remote.getSessionId().toString());
8691
}

java/client/test/org/openqa/selenium/support/ui/WebDriverWaitTest.java

+39-1
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,40 @@
1717

1818
package org.openqa.selenium.support.ui;
1919

20+
import static org.hamcrest.CoreMatchers.containsString;
21+
import static org.hamcrest.CoreMatchers.instanceOf;
22+
import static org.junit.Assert.assertNotNull;
2023
import static org.junit.Assert.assertSame;
24+
import static org.junit.Assert.assertThat;
2125
import static org.junit.Assert.fail;
26+
import static org.mockito.Matchers.any;
2227
import static org.mockito.Mockito.mock;
2328
import static org.mockito.Mockito.when;
29+
import static org.mockito.Mockito.withSettings;
30+
import static org.openqa.selenium.testing.TestUtilities.catchThrowable;
2431

2532
import org.junit.Before;
2633
import org.junit.Test;
2734
import org.junit.runner.RunWith;
2835
import org.junit.runners.JUnit4;
2936
import org.mockito.Mock;
3037
import org.mockito.MockitoAnnotations;
38+
import org.openqa.selenium.Capabilities;
39+
import org.openqa.selenium.MutableCapabilities;
3140
import org.openqa.selenium.NoSuchElementException;
3241
import org.openqa.selenium.NoSuchFrameException;
3342
import org.openqa.selenium.NoSuchWindowException;
3443
import org.openqa.selenium.TimeoutException;
3544
import org.openqa.selenium.WebDriver;
3645
import org.openqa.selenium.WebElement;
46+
import org.openqa.selenium.internal.WrapsDriver;
47+
import org.openqa.selenium.remote.Command;
48+
import org.openqa.selenium.remote.CommandExecutor;
49+
import org.openqa.selenium.remote.RemoteWebDriver;
50+
import org.openqa.selenium.remote.Response;
51+
import org.openqa.selenium.remote.SessionId;
52+
53+
import java.io.IOException;
3754

3855
@RunWith(JUnit4.class)
3956
public class WebDriverWaitTest {
@@ -46,6 +63,28 @@ public void createMocks() {
4663
MockitoAnnotations.initMocks(this);
4764
}
4865

66+
@Test
67+
public void shouldIncludeRemoteInfoForWrappedDriverTimeout() throws IOException {
68+
Capabilities caps = new MutableCapabilities();
69+
Response response = new Response(new SessionId("foo"));
70+
response.setValue(caps.asMap());
71+
CommandExecutor executor = mock(CommandExecutor.class);
72+
when(executor.execute(any(Command.class))).thenReturn(response);
73+
74+
RemoteWebDriver driver = new RemoteWebDriver(executor, caps);
75+
WebDriver testDriver = mock(WebDriver.class, withSettings().extraInterfaces(WrapsDriver.class));
76+
when(((WrapsDriver) testDriver).getWrappedDriver()).thenReturn(driver);
77+
78+
TickingClock clock = new TickingClock(200);
79+
WebDriverWait wait = new WebDriverWait(testDriver, clock, clock, 1, 200);
80+
81+
Throwable ex = catchThrowable(() -> wait.until((d) -> false));
82+
assertNotNull(ex);
83+
assertThat(ex, instanceOf(TimeoutException.class));
84+
assertThat(ex.getMessage(), containsString("Capabilities [{javascriptEnabled=true, platformName=ANY, platform=ANY}]"));
85+
assertThat(ex.getMessage(), containsString("Session ID: foo"));
86+
}
87+
4988
@Test
5089
public void shouldThrowAnExceptionIfTheTimerRunsOut() {
5190
TickingClock clock = new TickingClock(200);
@@ -105,4 +144,3 @@ public Boolean apply(WebDriver driver) {
105144
}
106145
}
107146
}
108-

0 commit comments

Comments
 (0)