Path: blob/trunk/java/src/org/openqa/selenium/Capabilities.java
3987 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617package org.openqa.selenium;1819import java.io.Serializable;20import java.util.Collections;21import java.util.Map;22import java.util.Objects;23import java.util.Optional;24import java.util.Set;25import java.util.stream.Stream;26import org.jspecify.annotations.Nullable;2728/** Describes a series of key/value pairs that encapsulate aspects of a browser. */29public interface Capabilities extends Serializable {3031default String getBrowserName() {32return String.valueOf(Optional.ofNullable(getCapability("browserName")).orElse(""));33}3435default @Nullable Platform getPlatformName() {36return Stream.of("platformName")37.map(this::getCapability)38.filter(Objects::nonNull)39.map(40cap -> {41if (cap instanceof Platform) {42return (Platform) cap;43}4445try {46return Platform.fromString((String.valueOf(cap)));47} catch (WebDriverException e) {48return null;49}50})51.filter(Objects::nonNull)52.findFirst()53.orElse(null);54}5556default String getBrowserVersion() {57return String.valueOf(Optional.ofNullable(getCapability("browserVersion")).orElse(""));58}5960/**61* @return The capabilities as a Map.62*/63Map<String, Object> asMap();6465/**66* @param capabilityName The capability to return.67* @return The value, or null if not set.68* @see org.openqa.selenium.remote.CapabilityType69*/70@Nullable Object getCapability(String capabilityName);7172/**73* @param capabilityName The capability to check.74* @return Whether the value is not null and not false.75* @see org.openqa.selenium.remote.CapabilityType76*/77default boolean is(String capabilityName) {78Object cap = getCapability(capabilityName);79if (cap == null) {80return false;81}82return cap instanceof Boolean ? (Boolean) cap : Boolean.parseBoolean(String.valueOf(cap));83}8485/**86* Merge two {@link Capabilities} together and return the union of the two as a new {@link87* Capabilities} instance. Capabilities from {@code other} will override those in {@code this}.88*/89default Capabilities merge(Capabilities other) {90return new ImmutableCapabilities(new MutableCapabilities(this).merge(other));91}9293default Set<String> getCapabilityNames() {94return Collections.unmodifiableSet(asMap().keySet());95}96}979899