Skip to content

Commit 2a5118b

Browse files
committed
[java] Implementing commands to get timeouts
1 parent edf9637 commit 2a5118b

File tree

9 files changed

+112
-0
lines changed

9 files changed

+112
-0
lines changed

java/client/src/org/openqa/selenium/WebDriver.java

+28
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,15 @@ default Timeouts implicitlyWait(Duration duration) {
344344
return implicitlyWait(duration.toMillis(), TimeUnit.MILLISECONDS);
345345
}
346346

347+
/**
348+
* Gets the amount of time the driver should wait when searching for an element if it is
349+
* not immediately present.
350+
*
351+
* @return The amount of time the driver should wait when searching for an element.
352+
* @see <a href="https://www.w3.org/TR/webdriver/#get-timeouts">W3C WebDriver</a>
353+
*/
354+
Duration getImplicitWaitTimeout();
355+
347356
/**
348357
* @deprecated Use {@link #setScriptTimeout(Duration)}
349358
*
@@ -372,6 +381,16 @@ default Timeouts setScriptTimeout(Duration duration) {
372381
return setScriptTimeout(duration.toMillis(), TimeUnit.MILLISECONDS);
373382
}
374383

384+
/**
385+
* Gets the amount of time to wait for an asynchronous script to finish execution before
386+
* throwing an error. If the timeout is negative, then the script will be allowed to run
387+
* indefinitely.
388+
*
389+
* @return The amount of time to wait for an asynchronous script to finish execution.
390+
* @see <a href="https://www.w3.org/TR/webdriver/#get-timeouts">W3C WebDriver</a>
391+
*/
392+
Duration getScriptTimeout();
393+
375394
/**
376395
* @deprecated Use {@link #pageLoadTimeout(Duration)}
377396
*
@@ -395,6 +414,15 @@ default Timeouts setScriptTimeout(Duration duration) {
395414
default Timeouts pageLoadTimeout(Duration duration) {
396415
return pageLoadTimeout(duration.toMillis(), TimeUnit.MILLISECONDS);
397416
}
417+
418+
/**
419+
* Gets the amount of time to wait for a page load to complete before throwing an error.
420+
* If the timeout is negative, page loads can be indefinite.
421+
*
422+
* @return The amount of time to wait for a page load to complete.
423+
* @see <a href="https://www.w3.org/TR/webdriver/#get-timeouts">W3C WebDriver</a>
424+
*/
425+
Duration getPageLoadTimeout();
398426
}
399427

400428
/**

java/client/src/org/openqa/selenium/remote/DriverCommand.java

+1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ static CommandPayload SET_ALERT_VALUE(String keysToSend) {
226226
}
227227
String SET_ALERT_CREDENTIALS = "setAlertCredentials";
228228

229+
String GET_TIMEOUTS = "getTimeouts";
229230
String SET_TIMEOUT = "setTimeout";
230231

231232
String PRINT_PAGE = "printPage";

java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java

+24
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,14 @@ public Timeouts implicitlyWait(Duration duration) {
866866
return this;
867867
}
868868

869+
@Override
870+
public Duration getImplicitWaitTimeout() {
871+
Response response = execute(DriverCommand.GET_TIMEOUTS);
872+
Map<String, Object> rawSize = (Map<String, Object>) response.getValue();
873+
long timeout = ((Number) rawSize.get("implicit")).longValue();
874+
return Duration.ofMillis(timeout);
875+
}
876+
869877
@Deprecated
870878
@Override
871879
public Timeouts setScriptTimeout(long time, TimeUnit unit) {
@@ -878,6 +886,14 @@ public Timeouts setScriptTimeout(Duration duration) {
878886
return this;
879887
}
880888

889+
@Override
890+
public Duration getScriptTimeout() {
891+
Response response = execute(DriverCommand.GET_TIMEOUTS);
892+
Map<String, Object> rawSize = (Map<String, Object>) response.getValue();
893+
long timeout = ((Number) rawSize.get("script")).longValue();
894+
return Duration.ofMillis(timeout);
895+
}
896+
881897
@Deprecated
882898
@Override
883899
public Timeouts pageLoadTimeout(long time, TimeUnit unit) {
@@ -889,6 +905,14 @@ public Timeouts pageLoadTimeout(Duration duration) {
889905
execute(DriverCommand.SET_PAGE_LOAD_TIMEOUT(duration));
890906
return this;
891907
}
908+
909+
@Override
910+
public Duration getPageLoadTimeout() {
911+
Response response = execute(DriverCommand.GET_TIMEOUTS);
912+
Map<String, Object> rawSize = (Map<String, Object>) response.getValue();
913+
long timeout = ((Number) rawSize.get("pageLoad")).longValue();
914+
return Duration.ofMillis(timeout);
915+
}
892916
} // timeouts class.
893917

894918
@Beta

java/client/src/org/openqa/selenium/remote/codec/AbstractHttpCommandCodec.java

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import static org.openqa.selenium.remote.DriverCommand.GET_SCREEN_ORIENTATION;
6262
import static org.openqa.selenium.remote.DriverCommand.GET_SCREEN_ROTATION;
6363
import static org.openqa.selenium.remote.DriverCommand.GET_SESSION_LOGS;
64+
import static org.openqa.selenium.remote.DriverCommand.GET_TIMEOUTS;
6465
import static org.openqa.selenium.remote.DriverCommand.GET_TITLE;
6566
import static org.openqa.selenium.remote.DriverCommand.GO_BACK;
6667
import static org.openqa.selenium.remote.DriverCommand.GO_FORWARD;
@@ -193,6 +194,7 @@ public AbstractHttpCommandCodec() {
193194
defineCommand(DELETE_COOKIE, delete(cookie + "/:name"));
194195

195196
String timeouts = sessionId + "/timeouts";
197+
defineCommand(GET_TIMEOUTS, get(timeouts));
196198
defineCommand(SET_TIMEOUT, post(timeouts));
197199
defineCommand(SET_SCRIPT_TIMEOUT, post(timeouts + "/async_script"));
198200
defineCommand(IMPLICITLY_WAIT, post(timeouts + "/implicit_wait"));

java/client/src/org/openqa/selenium/support/events/EventFiringWebDriver.java

+15
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,11 @@ public Timeouts implicitlyWait(Duration duration) {
685685
return this;
686686
}
687687

688+
@Override
689+
public Duration getImplicitWaitTimeout() {
690+
return timeouts.getImplicitWaitTimeout();
691+
}
692+
688693
@Deprecated
689694
@Override
690695
public Timeouts setScriptTimeout(long time, TimeUnit unit) {
@@ -697,6 +702,11 @@ public Timeouts setScriptTimeout(Duration duration) {
697702
return this;
698703
}
699704

705+
@Override
706+
public Duration getScriptTimeout() {
707+
return timeouts.getScriptTimeout();
708+
}
709+
700710
@Deprecated
701711
@Override
702712
public Timeouts pageLoadTimeout(long time, TimeUnit unit) {
@@ -708,6 +718,11 @@ public Timeouts pageLoadTimeout(Duration duration) {
708718
timeouts.pageLoadTimeout(duration);
709719
return this;
710720
}
721+
722+
@Override
723+
public Duration getPageLoadTimeout() {
724+
return timeouts.getPageLoadTimeout();
725+
}
711726
}
712727

713728
private class EventFiringTargetLocator implements TargetLocator {

java/client/test/org/openqa/selenium/ExecutingAsyncJavascriptTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ public void setUp() {
5252
driver.manage().timeouts().setScriptTimeout(Duration.ofMillis(5000));
5353
}
5454

55+
@Test
56+
@NotYetImplemented(value = CHROME, reason = "Default to 5s")
57+
@NotYetImplemented(value = FIREFOX, reason = "Default to 5s")
58+
@NotYetImplemented(value = SAFARI, reason = "Default to 5s")
59+
public void shouldSetAndGetScriptTimeout() {
60+
Duration timeout = driver.manage().timeouts().getScriptTimeout();
61+
assertThat(timeout).hasMillis(30000);
62+
driver.manage().timeouts().setScriptTimeout(Duration.ofMillis(3000));
63+
Duration timeout2 = driver.manage().timeouts().getScriptTimeout();
64+
assertThat(timeout2).hasMillis(3000);
65+
}
66+
5567
@Test
5668
public void shouldNotTimeoutIfCallbackInvokedImmediately() {
5769
driver.get(pages.ajaxyPage);

java/client/test/org/openqa/selenium/ImplicitWaitTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ public void tearDown() {
5151
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(0));
5252
}
5353

54+
@Test
55+
public void shouldSetAndGetImplicitWaitTimeout() {
56+
Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
57+
assertThat(timeout).hasMillis(0);
58+
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(3000));
59+
Duration timeout2 = driver.manage().timeouts().getImplicitWaitTimeout();
60+
assertThat(timeout2).hasMillis(3000);
61+
}
62+
5463
@Test
5564
public void testShouldImplicitlyWaitForASingleElement() {
5665
driver.get(pages.dynamicPage);

java/client/test/org/openqa/selenium/PageLoadingTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ private void initDriverWithLoadStrategy(String strategy) {
5858
createNewDriver(new ImmutableCapabilities(CapabilityType.PAGE_LOAD_STRATEGY, strategy));
5959
}
6060

61+
@Test
62+
public void shouldSetAndGetPageLoadTimeout() {
63+
Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
64+
assertThat(timeout).hasMillis(300000);
65+
driver.manage().timeouts().pageLoadTimeout(Duration.ofMillis(3000));
66+
Duration timeout2 = driver.manage().timeouts().getPageLoadTimeout();
67+
assertThat(timeout2).hasMillis(3000);
68+
}
69+
6170
@Test
6271
@NeedsLocalEnvironment
6372
@NoDriverBeforeTest

java/client/test/org/openqa/selenium/remote/RemoteWebDriverUnitTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,18 @@ public void canHandleSetImplicitWaitCommand() {
630630
new CommandPayload(DriverCommand.SET_TIMEOUT, ImmutableMap.of("implicit", 10000L)));
631631
}
632632

633+
@Test
634+
public void canHandleGetTimeoutsCommand() {
635+
WebDriverFixture fixture = new WebDriverFixture(
636+
echoCapabilities,
637+
valueResponder(ImmutableMap.of("implicit", 100, "script", 200, "pageLoad", 300)));
638+
639+
fixture.driver.manage().timeouts().getImplicitWaitTimeout();
640+
641+
fixture.verifyCommands(
642+
new CommandPayload(DriverCommand.GET_TIMEOUTS, emptyMap()));
643+
}
644+
633645
@Test
634646
public void canHandleSetScriptTimeoutCommand() {
635647
WebDriverFixture fixture = new WebDriverFixture(echoCapabilities, nullValueResponder);

0 commit comments

Comments
 (0)