Skip to content

Commit

Permalink
Add tests to allow new builder to only have capabilities set
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Jul 10, 2018
1 parent 6b3598d commit 52a2f6a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public RemoteWebDriverBuilder withDriverService(DriverService service) {
* {@link RemoteWebDriver}.
*/
public WebDriver build() {
if (options.isEmpty()) {
if (options.isEmpty() && additionalCapabilities.isEmpty()) {
throw new SessionNotCreatedException("Refusing to create session without any capabilities");
}

Expand Down Expand Up @@ -315,7 +315,7 @@ void writePayload(JsonOutput out) {
// Try and minimise payload by finding keys that have the same value in every option. This isn't
// terribly efficient, but we expect the number of entries to be very low in almost every case,
// so this should be fine.
Map<String, Object> always = new HashMap<>(options.get(0));
Map<String, Object> always = new HashMap<>(options.isEmpty() ? new HashMap<>() : options.get(0));
for (Map<String, Object> option : options) {
for (Map.Entry<String, Object> entry : option.entrySet()) {
if (!always.containsKey(entry.getKey())) {
Expand All @@ -329,28 +329,35 @@ void writePayload(JsonOutput out) {
}
always.putAll(additionalCapabilities);

out.name("alwaysMatch");
out.beginObject();
always.forEach((key, value) -> {
out.name(key);
out.write(value);
});
out.endObject();

out.name("firstMatch");
out.beginArray();
options.forEach(option -> {
// Only write alwaysMatch if there are actually things to write
if (!always.isEmpty()) {
out.name("alwaysMatch");
out.beginObject();
option.entrySet().stream()
.filter(entry -> !always.containsKey(entry.getKey()))
.filter(entry -> !additionalCapabilities.containsKey(entry.getKey()))
.forEach(entry -> {
out.name(entry.getKey());
out.write(entry.getValue());
});
always.forEach((key, value) -> {
out.name(key);
out.write(value);
});
out.endObject();
});
out.endArray();
}

// Only write firstMatch if there are also things to write
if (!options.isEmpty()) {
out.name("firstMatch");
out.beginArray();
options.forEach(option -> {
out.beginObject();
option.entrySet().stream()
.filter(entry -> !always.containsKey(entry.getKey()))
.filter(entry -> !additionalCapabilities.containsKey(entry.getKey()))
.forEach(entry -> {
out.name(entry.getKey());
out.write(entry.getValue());
});
out.endObject();
});
out.endArray();
}

out.endObject(); // Close the "capabilities" entry

metadata.forEach((key, value) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ public void mustSpecifyAtLeastOneSetOfOptions() {
}
}

@Test
public void settingAGlobalCapabilityCountsAsAnOption() {
RemoteWebDriverBuilder builder = RemoteWebDriver.builder()
.setCapability("browserName", "cheese");

List<Capabilities> capabilities = listCapabilities(builder);

assertEquals(1, capabilities.size());
assertEquals("cheese", capabilities.get(0).getBrowserName());
}

@Test
public void simpleCaseShouldBeADropIn() {
List<Capabilities> caps =
Expand Down

0 comments on commit 52a2f6a

Please sign in to comment.