Skip to content

Commit

Permalink
[java] implement the timeout setters separately
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Sep 17, 2021
1 parent 76f9b55 commit eeaee77
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 64 deletions.
50 changes: 24 additions & 26 deletions java/src/org/openqa/selenium/remote/AbstractDriverOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,32 @@

package org.openqa.selenium.remote;

import static org.openqa.selenium.remote.CapabilityType.ACCEPT_INSECURE_CERTS;
import static org.openqa.selenium.remote.CapabilityType.BROWSER_VERSION;
import static org.openqa.selenium.remote.CapabilityType.IMPLICIT_TIMEOUT;
import static org.openqa.selenium.remote.CapabilityType.PAGE_LOAD_STRATEGY;
import static org.openqa.selenium.remote.CapabilityType.PAGE_LOAD_TIMEOUT;
import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME;
import static org.openqa.selenium.remote.CapabilityType.PROXY;
import static org.openqa.selenium.remote.CapabilityType.SCRIPT_TIMEOUT;
import static org.openqa.selenium.remote.CapabilityType.STRICT_FILE_INTERACTABILITY;
import static org.openqa.selenium.remote.CapabilityType.TIMEOUTS;
import static org.openqa.selenium.remote.CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR;

import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.internal.Require;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import static org.openqa.selenium.remote.CapabilityType.ACCEPT_INSECURE_CERTS;
import static org.openqa.selenium.remote.CapabilityType.BROWSER_VERSION;
import static org.openqa.selenium.remote.CapabilityType.PAGE_LOAD_STRATEGY;
import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME;
import static org.openqa.selenium.remote.CapabilityType.PROXY;
import static org.openqa.selenium.remote.CapabilityType.STRICT_FILE_INTERACTABILITY;
import static org.openqa.selenium.remote.CapabilityType.TIMEOUTS;
import static org.openqa.selenium.remote.CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR;

public abstract class AbstractDriverOptions<DO extends AbstractDriverOptions> extends MutableCapabilities {
private Map<String, Long> timeouts = new HashMap<>();

public DO setBrowserVersion(String browserVersion) {
setCapability(
BROWSER_VERSION,
Expand All @@ -61,19 +57,21 @@ public DO setPlatformName(String platformName) {
return (DO) this;
}

public DO setTimeouts(Map<String, Duration> timeouts) {
HashMap<String, Long> convertedTimeouts = new HashMap<>();
List<String> validTimeouts = new ArrayList<>(Arrays.asList(IMPLICIT_TIMEOUT, PAGE_LOAD_TIMEOUT, SCRIPT_TIMEOUT));
public DO setImplicitWaitTimeout(Duration timeout) {
this.timeouts.put("implicit", timeout.toMillis());
setCapability(TIMEOUTS, this.timeouts);
return (DO) this;
}

Require.nonNull("Timeouts", timeouts).forEach((k, v) -> {
if (validTimeouts.contains(k)) {
convertedTimeouts.put(k, v.toMillis());
} else {
throw new IllegalArgumentException(k + " is not one of the valid timeout values: " + validTimeouts);
}
});
public DO setPageLoadTimeout(Duration timeout) {
this.timeouts.put("pageLoad", timeout.toMillis());
setCapability(TIMEOUTS, this.timeouts);
return (DO) this;
}

setCapability(TIMEOUTS, convertedTimeouts);
public DO setScriptTimeout(Duration timeout) {
this.timeouts.put("script", timeout.toMillis());
setCapability(TIMEOUTS, this.timeouts);
return (DO) this;
}

Expand Down
3 changes: 0 additions & 3 deletions java/src/org/openqa/selenium/remote/CapabilityType.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ public interface CapabilityType {
String OVERLAPPING_CHECK_DISABLED = "overlappingCheckDisabled";
String STRICT_FILE_INTERACTABILITY = "strictFileInteractability";
String TIMEOUTS = "timeouts";
String IMPLICIT_TIMEOUT = "implicit";
String PAGE_LOAD_TIMEOUT = "pageLoad";
String SCRIPT_TIMEOUT = "script";

String LOGGING_PREFS = "loggingPrefs";

Expand Down
58 changes: 23 additions & 35 deletions java/test/org/openqa/selenium/chrome/ChromeOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
import static org.assertj.core.api.InstanceOfAssertFactories.MAP;
import static org.openqa.selenium.chrome.ChromeDriverLogLevel.OFF;
import static org.openqa.selenium.chrome.ChromeDriverLogLevel.SEVERE;
import static org.openqa.selenium.remote.CapabilityType.IMPLICIT_TIMEOUT;
import static org.openqa.selenium.remote.CapabilityType.PAGE_LOAD_TIMEOUT;
import static org.openqa.selenium.remote.CapabilityType.SCRIPT_TIMEOUT;

@Category(UnitTests.class)
public class ChromeOptionsTest {
Expand Down Expand Up @@ -76,18 +73,15 @@ public void canBuildLogLevelFromStringRepresentation() {
@Test
public void canAddW3CCompliantOptions() {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBrowserVersion("99");
chromeOptions.setPlatformName("9 3/4");
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.IGNORE);
chromeOptions.setAcceptInsecureCerts(true);
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
chromeOptions.setStrictFileInteractability(true);

Map<String, Duration> timeouts = new HashMap<>();
timeouts.put(IMPLICIT_TIMEOUT, Duration.ofSeconds(1));
timeouts.put(PAGE_LOAD_TIMEOUT, Duration.ofSeconds(2));
timeouts.put(SCRIPT_TIMEOUT, Duration.ofSeconds(3));
chromeOptions.setTimeouts(timeouts);
chromeOptions.setBrowserVersion("99")
.setPlatformName("9 3/4")
.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.IGNORE)
.setAcceptInsecureCerts(true)
.setPageLoadStrategy(PageLoadStrategy.EAGER)
.setStrictFileInteractability(true)
.setImplicitWaitTimeout(Duration.ofSeconds(1))
.setPageLoadTimeout(Duration.ofSeconds(2))
.setScriptTimeout(Duration.ofSeconds(3));

Map<String, Object> mappedOptions = chromeOptions.asMap();
assertThat(mappedOptions.get("browserName")).isEqualTo("chrome");
Expand All @@ -98,35 +92,29 @@ public void canAddW3CCompliantOptions() {
assertThat(mappedOptions.get("pageLoadStrategy").toString()).isEqualTo("eager");
assertThat(mappedOptions.get("strictFileInteractability")).isEqualTo(true);

Map<String, Long> convertedTimeouts = new HashMap<>();
convertedTimeouts.put("implicit", 1000L);
convertedTimeouts.put("pageLoad", 2000L);
convertedTimeouts.put("script", 3000L);
Map<String, Long> expectedTimeouts = new HashMap<>();
expectedTimeouts.put("implicit", 1000L);
expectedTimeouts.put("pageLoad", 2000L);
expectedTimeouts.put("script", 3000L);

assertThat(mappedOptions.get("timeouts")).isEqualTo(convertedTimeouts);
assertThat(expectedTimeouts).isEqualTo(mappedOptions.get("timeouts"));
}

@Test
public void canAddOneTimeout() {
public void canAddSequentialTimeouts() {
ChromeOptions chromeOptions = new ChromeOptions();
Map<String, Duration> timeouts = new HashMap<>();
timeouts.put("implicit", Duration.ofSeconds(1));
chromeOptions.setImplicitWaitTimeout(Duration.ofSeconds(1));

Map<String, Long> convertedTimeouts = new HashMap<>();
convertedTimeouts.put("implicit", 1000L);

chromeOptions.setTimeouts(timeouts);
Map<String, Object> mappedOptions = chromeOptions.asMap();
assertThat(mappedOptions.get("timeouts")).isEqualTo(convertedTimeouts);
}
Map<String, Long> expectedTimeouts = new HashMap<>();

@Test(expected = IllegalArgumentException.class)
public void canNotAddInvalidTimeout() {
ChromeOptions chromeOptions = new ChromeOptions();
Map<String, Duration> timeouts = new HashMap<>();
timeouts.put("foo", Duration.ofSeconds(1));
expectedTimeouts.put("implicit", 1000L);
assertThat(expectedTimeouts).isEqualTo(mappedOptions.get("timeouts"));

chromeOptions.setTimeouts(timeouts);
chromeOptions.setPageLoadTimeout(Duration.ofSeconds(2));
expectedTimeouts.put("pageLoad", 2000L);
Map<String, Object> mappedOptions2 = chromeOptions.asMap();
assertThat(expectedTimeouts).isEqualTo(mappedOptions2.get("timeouts"));
}

@Test
Expand Down

0 comments on commit eeaee77

Please sign in to comment.