Path: blob/trunk/java/src/org/openqa/selenium/Capabilities.java
1865 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.NullMarked;27import org.jspecify.annotations.Nullable;2829/** Describes a series of key/value pairs that encapsulate aspects of a browser. */30@NullMarked31public interface Capabilities extends Serializable {3233default String getBrowserName() {34return String.valueOf(Optional.ofNullable(getCapability("browserName")).orElse(""));35}3637default @Nullable Platform getPlatformName() {38return Stream.of("platformName")39.map(this::getCapability)40.filter(Objects::nonNull)41.map(42cap -> {43if (cap instanceof Platform) {44return (Platform) cap;45}4647try {48return Platform.fromString((String.valueOf(cap)));49} catch (WebDriverException e) {50return null;51}52})53.filter(Objects::nonNull)54.findFirst()55.orElse(null);56}5758default String getBrowserVersion() {59return String.valueOf(Optional.ofNullable(getCapability("browserVersion")).orElse(""));60}6162/**63* @return The capabilities as a Map.64*/65Map<String, Object> asMap();6667/**68* @param capabilityName The capability to return.69* @return The value, or null if not set.70* @see org.openqa.selenium.remote.CapabilityType71*/72@Nullable Object getCapability(String capabilityName);7374/**75* @param capabilityName The capability to check.76* @return Whether the value is not null and not false.77* @see org.openqa.selenium.remote.CapabilityType78*/79default boolean is(String capabilityName) {80Object cap = getCapability(capabilityName);81if (cap == null) {82return false;83}84return cap instanceof Boolean ? (Boolean) cap : Boolean.parseBoolean(String.valueOf(cap));85}8687/**88* Merge two {@link Capabilities} together and return the union of the two as a new {@link89* Capabilities} instance. Capabilities from {@code other} will override those in {@code this}.90*/91default Capabilities merge(Capabilities other) {92return new ImmutableCapabilities(new MutableCapabilities(this).merge(other));93}9495default Set<String> getCapabilityNames() {96return Collections.unmodifiableSet(asMap().keySet());97}98}99100101