Skip to content

Commit

Permalink
[java] Removing deprecated caps from response
Browse files Browse the repository at this point in the history
and temporarily excluding some non W3C caps
from checks since they are returned by browser
drivers.

Fixes #10711
  • Loading branch information
diemol committed May 30, 2022
1 parent 14651c7 commit 3334607
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 78 deletions.
9 changes: 9 additions & 0 deletions java/src/org/openqa/selenium/AcceptedW3CCapabilityKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ public class AcceptedW3CCapabilityKeys implements Predicate<String> {
"^proxy$",
"^setWindowRect$",
"^strictFileInteractability$",
// TODO: Needs to be removed when ChromeDriver issue is resolved. Not a W3C capability
// https://bugs.chromium.org/p/chromedriver/issues/detail?id=4129
"^networkConnectionEnabled$",
// TODO: Needs to be removed when ChromeDriver issue is resolved. Not a W3C capability
// https://bugs.chromium.org/p/chromedriver/issues/detail?id=4129
"^chrome$",
// TODO: Needs to be removed when GeckoDriver issue is resolved. Not a W3C capability
// https://github.com/mozilla/geckodriver/issues/2023
"^platformVersion$",
"^timeouts$",
"^unhandledPromptBehavior$",
"^webSocketUrl$") // from webdriver-bidi
Expand Down
33 changes: 7 additions & 26 deletions java/src/org/openqa/selenium/remote/RemoteWebDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.logging.Level.SEVERE;
import static org.openqa.selenium.remote.CapabilityType.LOGGING_PREFS;
import static org.openqa.selenium.remote.CapabilityType.PLATFORM;
import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME;
import static org.openqa.selenium.remote.CapabilityType.SUPPORTS_JAVASCRIPT;

Expand Down Expand Up @@ -256,20 +255,18 @@ protected void startSession(Capabilities capabilities) {
if (responseValue == null) {
throw new SessionNotCreatedException(
"The underlying command executor returned a response without payload: " +
response.toString());
response);
}

if (!(responseValue instanceof Map)) {
throw new SessionNotCreatedException(
"The underlying command executor returned a response with a non well formed payload: " +
response.toString());
response);
}

@SuppressWarnings("unchecked") Map<String, Object> rawCapabilities = (Map<String, Object>) responseValue;
MutableCapabilities returnedCapabilities = new MutableCapabilities(rawCapabilities);
String platformString = (String) rawCapabilities.getOrDefault(
PLATFORM,
rawCapabilities.get(PLATFORM_NAME));
String platformString = (String) rawCapabilities.get(PLATFORM_NAME);
Platform platform;
try {
if (platformString == null || "".equals(platformString)) {
Expand All @@ -282,20 +279,8 @@ protected void startSession(Capabilities capabilities) {
// system property. Try to recover and parse this.
platform = Platform.extractFromSysProperty(platformString);
}
returnedCapabilities.setCapability(PLATFORM, platform);
returnedCapabilities.setCapability(PLATFORM_NAME, platform);

if (rawCapabilities.containsKey(SUPPORTS_JAVASCRIPT)) {
Object raw = rawCapabilities.get(SUPPORTS_JAVASCRIPT);
if (raw instanceof String) {
returnedCapabilities.setCapability(SUPPORTS_JAVASCRIPT, Boolean.parseBoolean((String) raw));
} else if (raw instanceof Boolean) {
returnedCapabilities.setCapability(SUPPORTS_JAVASCRIPT, ((Boolean) raw).booleanValue());
}
} else {
returnedCapabilities.setCapability(SUPPORTS_JAVASCRIPT, true);
}

this.capabilities = returnedCapabilities;
sessionId = new SessionId(response.getSessionId());
}
Expand Down Expand Up @@ -734,20 +719,16 @@ public String toString() {
return super.toString();
}

// w3c name first
Object platform = caps.getCapability(PLATFORM_NAME);
if (!(platform instanceof String)) {
platform = caps.getCapability(PLATFORM);
}
if (platform == null) {
platform = "unknown";
Object platformName = caps.getCapability(PLATFORM_NAME);
if (platformName == null) {
platformName = "unknown";
}

return String.format(
"%s: %s on %s (%s)",
getClass().getSimpleName(),
caps.getBrowserName(),
platform,
platformName,
getSessionId());
}

Expand Down
16 changes: 7 additions & 9 deletions java/test/org/openqa/selenium/JavascriptEnabledDriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@

package org.openqa.selenium;

import org.junit.Test;
import org.openqa.selenium.interactions.Locatable;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.testing.JUnit4TestBase;
import org.openqa.selenium.testing.NoDriverAfterTest;
import org.openqa.selenium.testing.NotYetImplemented;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assume.assumeTrue;
import static org.openqa.selenium.WaitingConditions.elementTextToEqual;
import static org.openqa.selenium.WaitingConditions.elementValueToEqual;
import static org.openqa.selenium.WaitingConditions.windowToBeSwitchedToWithName;
import static org.openqa.selenium.remote.CapabilityType.SUPPORTS_JAVASCRIPT;
import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;

import org.junit.Test;
import org.openqa.selenium.interactions.Locatable;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.testing.JUnit4TestBase;
import org.openqa.selenium.testing.NoDriverAfterTest;
import org.openqa.selenium.testing.NotYetImplemented;

public class JavascriptEnabledDriverTest extends JUnit4TestBase {

@Test
Expand Down Expand Up @@ -189,7 +188,6 @@ public void testShouldBeAbleToClickIfEvenSomethingHorribleHappens() {
@Test
public void testShouldBeAbleToGetTheLocationOfAnElement() {
assumeTrue(driver instanceof JavascriptExecutor);
assumeTrue(((HasCapabilities) driver).getCapabilities().is(SUPPORTS_JAVASCRIPT));

driver.get(pages.javascriptPage);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,6 @@

package org.openqa.selenium.remote;

import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.openqa.selenium.remote.WebDriverFixture.echoCapabilities;
import static org.openqa.selenium.remote.WebDriverFixture.exceptionResponder;
import static org.openqa.selenium.remote.WebDriverFixture.nullResponder;
import static org.openqa.selenium.remote.WebDriverFixture.nullValueResponder;
import static org.openqa.selenium.remote.WebDriverFixture.valueResponder;

import com.google.common.collect.ImmutableMap;

import org.junit.Test;
Expand All @@ -55,6 +39,22 @@
import java.time.Duration;
import java.util.UUID;

import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.openqa.selenium.remote.WebDriverFixture.echoCapabilities;
import static org.openqa.selenium.remote.WebDriverFixture.exceptionResponder;
import static org.openqa.selenium.remote.WebDriverFixture.nullResponder;
import static org.openqa.selenium.remote.WebDriverFixture.nullValueResponder;
import static org.openqa.selenium.remote.WebDriverFixture.valueResponder;

@Category(UnitTests.class)
public class RemoteWebDriverInitializationTest {
private boolean quitCalled = false;
Expand Down Expand Up @@ -172,24 +172,7 @@ public void canHandleNonStandardCapabilitiesReturnedByRemoteEnd() throws IOExcep
CommandExecutor executor = mock(CommandExecutor.class);
when(executor.execute(any())).thenReturn(resp);
RemoteWebDriver driver = new RemoteWebDriver(executor, new ImmutableCapabilities());
assertThat(driver.getCapabilities().getCapability("platform")).isEqualTo(Platform.UNIX);
}

private class BadStartSessionRemoteWebDriver extends RemoteWebDriver {
public BadStartSessionRemoteWebDriver(CommandExecutor executor,
Capabilities desiredCapabilities) {
super(executor, desiredCapabilities);
}

@Override
protected void startSession(Capabilities desiredCapabilities) {
throw new RuntimeException("Stub session that should fail");
}

@Override
public void quit() {
quitCalled = true;
}
assertThat(driver.getCapabilities().getCapability("platformName")).isEqualTo(Platform.UNIX);
}

@Test
Expand Down Expand Up @@ -225,4 +208,21 @@ public void verifyNoCommands(CommandExecutor executor) {
}
verifyNoMoreInteractions(executor);
}

private class BadStartSessionRemoteWebDriver extends RemoteWebDriver {
public BadStartSessionRemoteWebDriver(CommandExecutor executor,
Capabilities desiredCapabilities) {
super(executor, desiredCapabilities);
}

@Override
protected void startSession(Capabilities desiredCapabilities) {
throw new RuntimeException("Stub session that should fail");
}

@Override
public void quit() {
quitCalled = true;
}
}
}
18 changes: 9 additions & 9 deletions java/test/org/openqa/selenium/support/ui/WebDriverWaitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@

package org.openqa.selenium.support.ui;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;

import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
Expand All @@ -49,6 +42,13 @@
import java.io.IOException;
import java.time.Duration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;

@Category(UnitTests.class)
public class WebDriverWaitTest {

Expand Down Expand Up @@ -77,8 +77,8 @@ public void shouldIncludeRemoteInfoForWrappedDriverTimeout() throws IOException
new WebDriverWait(testDriver, Duration.ofSeconds(1), Duration.ofMillis(200), clock, clock);

assertThatExceptionOfType(TimeoutException.class)
.isThrownBy(() -> wait.until(d -> false))
.withMessageContaining("Capabilities {javascriptEnabled: true, platform: ANY, platformName: ANY}")
.isThrownBy(() -> wait.until(d -> false))
.withMessageContaining("Capabilities {platformName: ANY}")
.withMessageContaining("Session ID: foo");
}

Expand Down

0 comments on commit 3334607

Please sign in to comment.