forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure OSS and W3C New Session payloads make an effort to match
Assuming everyone goes through the standalone server. I have a sneaking suspicion that folks don't always do this, and nor should they. But, let's see if we can make this work....
- Loading branch information
Showing
4 changed files
with
253 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
java/server/test/org/openqa/selenium/remote/server/SyntheticNewSessionPayloadTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package org.openqa.selenium.remote.server; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.gson.reflect.TypeToken; | ||
|
||
import org.junit.Test; | ||
import org.openqa.selenium.Capabilities; | ||
import org.openqa.selenium.ImmutableCapabilities; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.lang.reflect.Type; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* There's an interesting problem that appears relatively often with New Session and incorrectly | ||
* prepared payloads. The key problem manifests where something is added to the OSS payload, and | ||
* this is incorrectly reflected in the w3c payload. To work around this, we'll inspect the two sets | ||
* of data. If we can expand the W3C payloads (using the guidelines from the spec) into something | ||
* that matches the OSS payload, we'll just forward the blob unchanged. If, however, we can't then | ||
* we need to do some fancy foot work, in which we'll: | ||
* | ||
* <ol> | ||
* <li>Create a new W3C "firstMatch" blob that matches the OSS payload</li> | ||
* <li>Expand all W3C payloads, so that they are complete</li> | ||
* <li>Forward a new New Session payload composed of the OSS payload and the combined list of | ||
* "firstMatches", with the OSS equivalent first.</li> | ||
* </ol> | ||
* <p> | ||
* This test has been broken out to make this behaviour clearer, and to allow for this comment. | ||
*/ | ||
public class SyntheticNewSessionPayloadTest { | ||
|
||
private static Type MAP_TYPE = new TypeToken<Map<String, Object>>(){}.getType(); | ||
|
||
@Test | ||
public void shouldDoNothingIfOssAndW3CPayloadsAreBothEmpty() { | ||
Map<String, Object> empty = ImmutableMap.of( | ||
"desiredCapabilities", ImmutableMap.of(), | ||
"capabilities", ImmutableMap.of( | ||
"alwaysMatch", ImmutableMap.of(), | ||
"firstMatch", ImmutableList.of())); | ||
|
||
List<Capabilities> allCaps = getCapabilities(empty); | ||
|
||
assertEquals(ImmutableList.of(new ImmutableCapabilities(new HashMap<>())), allCaps); | ||
} | ||
|
||
@Test | ||
public void shouldDoNothingIfOssPayloadMatchesAlwaysMatchAndThereAreNoFirstMatches() { | ||
ImmutableMap<String, String> identicalCaps = ImmutableMap.of("browserName", "cheese"); | ||
|
||
Map<String, Object> payload= ImmutableMap.of( | ||
"desiredCapabilities", identicalCaps, | ||
"capabilities", ImmutableMap.of( | ||
"alwaysMatch", identicalCaps)); | ||
|
||
List<Capabilities> allCaps = getCapabilities(payload); | ||
|
||
assertEquals(ImmutableList.of(new ImmutableCapabilities(identicalCaps)), allCaps); | ||
} | ||
|
||
@Test | ||
public void shouldDoNothingIfOssPayloadMatchesAFirstMatchAndThereIsNoAlwaysMatch() { | ||
ImmutableMap<String, String> identicalCaps = ImmutableMap.of("browserName", "cheese"); | ||
|
||
Map<String, Object> payload= ImmutableMap.of( | ||
"desiredCapabilities", identicalCaps, | ||
"capabilities", ImmutableMap.of( | ||
"firstMatch", ImmutableList.of(identicalCaps))); | ||
|
||
List<Capabilities> allCaps = getCapabilities(payload); | ||
|
||
assertEquals(ImmutableList.of(new ImmutableCapabilities(identicalCaps)), allCaps); | ||
} | ||
|
||
@Test | ||
public void shouldDoNothingIfOssPayloadMatchesAValidMergedW3CPayload() { | ||
ImmutableMap<String, String> caps = ImmutableMap.of( | ||
"browserName", "cheese", | ||
"se:cake", "more cheese"); | ||
|
||
Map<String, Object> payload= ImmutableMap.of( | ||
"desiredCapabilities", caps, | ||
"capabilities", ImmutableMap.of( | ||
"alwaysMatch", ImmutableMap.of("browserName", "cheese"), | ||
"firstMatch", ImmutableList.of(ImmutableMap.of("se:cake", "more cheese")))); | ||
|
||
List<Capabilities> allCaps = getCapabilities(payload); | ||
|
||
assertEquals(ImmutableList.of(new ImmutableCapabilities(caps)), allCaps); | ||
} | ||
|
||
@Test | ||
public void shouldExpandAllW3CMatchesToFirstMatchesAndRemoveAlwaysMatchIfSynthesizingAPayload() | ||
throws IOException { | ||
Map<String, Object> payload = ImmutableMap.of( | ||
// OSS capabilities request a chrome webdriver | ||
"desiredCapabilities", ImmutableMap.of("browserName", "chrome"), | ||
// Yet the w3c ones ask for IE and edge | ||
"capabilities", ImmutableMap.of( | ||
"alwaysMatch", ImmutableMap.of("se:cake", "cheese"), | ||
"firstMatch", ImmutableList.of( | ||
ImmutableMap.of("browserName", "edge"), | ||
ImmutableMap.of("browserName", "cheese")))); | ||
|
||
try (NewSessionPayload newSession = new NewSessionPayload(payload)) { | ||
List<Capabilities> allCaps = newSession.stream().collect(ImmutableList.toImmutableList()); | ||
|
||
assertEquals(3, allCaps.size()); | ||
assertTrue(allCaps.contains(new ImmutableCapabilities(ImmutableMap.of("browserName", "cheese", "se:cake", "cheese")))); | ||
assertTrue(allCaps.contains(new ImmutableCapabilities(ImmutableMap.of("browserName", "chrome")))); | ||
assertTrue(allCaps.contains(new ImmutableCapabilities(ImmutableMap.of("browserName", "edge", "se:cake", "cheese")))); | ||
} | ||
} | ||
|
||
// @Test | ||
// public void ossPayloadWillBeFirstW3CPayload() { | ||
// // This is one of the common cases --- asking for marionette to be false. There's no way to | ||
// // encode this legally into the w3c payload (as it doesn't start with "se:"), yet it's a use- | ||
// // case that needs to be properly supported. | ||
// Map<String, Object> rawCapabilities = ImmutableMap.of( | ||
// "desiredCapabilities", ImmutableMap.of("marionette", false), | ||
// "capabilties", ImmutableMap.of( | ||
// "alwaysMatch", ImmutableMap.of("browserName", "chrome"))); | ||
// | ||
// List<Capabilities> allCaps = getCapabilities(rawCapabilities); | ||
// | ||
// assertEquals(2, allCaps.size()); | ||
// assertEquals(false, allCaps.get(0).getCapability("marionette")); | ||
// } | ||
|
||
private List<Capabilities> getCapabilities(Map<String, Object> payload) { | ||
try (NewSessionPayload newSessionPayload = new NewSessionPayload(payload)) { | ||
return newSessionPayload.stream().collect(ImmutableList.toImmutableList()); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
} |