How to use test method of org.openqa.selenium.remote.AcceptedW3CCapabilityKeys class

Best Selenium code snippet using org.openqa.selenium.remote.AcceptedW3CCapabilityKeys.test

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * See the NOTICE file distributed with this work for additional
5 * information regarding copyright ownership.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package io.appium.java_client.remote;
18
19import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.ImmutableSet;
21import org.openqa.selenium.Capabilities;
22import org.openqa.selenium.internal.Require;
23import org.openqa.selenium.remote.AcceptedW3CCapabilityKeys;
24import org.openqa.selenium.remote.CommandPayload;
25
26import java.util.Map;
27
28import static io.appium.java_client.internal.CapabilityHelpers.APPIUM_PREFIX;
29import static org.openqa.selenium.remote.DriverCommand.NEW_SESSION;
30
31public class AppiumNewSessionCommandPayload extends CommandPayload {
32    private static final AcceptedW3CCapabilityKeys ACCEPTED_W3C_PATTERNS = new AcceptedW3CCapabilityKeys();
33
34    /**
35     * Appends "appium:" prefix to all non-prefixed non-standard capabilities.
36     *
37     * @param possiblyInvalidCapabilities user-provided capabilities mapping.
38     * @return Fixed capabilities mapping.
39     */
40    private static Map<String, Object> makeW3CSafe(Capabilities possiblyInvalidCapabilities) {
41        return Require.nonNull("Capabilities", possiblyInvalidCapabilities)
42                .asMap().entrySet().stream()
43                .collect(ImmutableMap.toImmutableMap(entry -> ACCEPTED_W3C_PATTERNS.test(entry.getKey())
44                                ? entry.getKey()
45                                : APPIUM_PREFIX + entry.getKey(),
46                        Map.Entry::getValue));
47    }
48
49    /**
50     * Overrides the default new session behavior to
51     * only handle W3C capabilities.
52     *
53     * @param capabilities User-provided capabilities.
54     */
55    public AppiumNewSessionCommandPayload(Capabilities capabilities) {
56        super(NEW_SESSION, ImmutableMap.of(
57                "capabilities", ImmutableSet.of(makeW3CSafe(capabilities)),
58                "desiredCapabilities", capabilities
59        ));
60    }
61}
62
Full Screen
copy
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * See the NOTICE file distributed with this work for additional
5 * information regarding copyright ownership.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package io.appium.java_client.remote.options;
18
19import org.openqa.selenium.Capabilities;
20import org.openqa.selenium.MutableCapabilities;
21import org.openqa.selenium.Platform;
22import org.openqa.selenium.WebDriverException;
23import org.openqa.selenium.internal.Require;
24import org.openqa.selenium.remote.AcceptedW3CCapabilityKeys;
25import org.openqa.selenium.remote.CapabilityType;
26
27import javax.annotation.Nullable;
28import java.lang.reflect.Constructor;
29import java.lang.reflect.InvocationTargetException;
30import java.util.Map;
31import java.util.Optional;
32import java.util.stream.Collectors;
33
34import static io.appium.java_client.internal.CapabilityHelpers.APPIUM_PREFIX;
35import static java.util.Collections.unmodifiableMap;
36
37/**
38 * This class represents capabilities that are available in the base driver,
39 * e.g. are acceptable by any Appium driver
40 *
41 * @param <T> The child class for a proper chaining.
42 */
43@SuppressWarnings({"unused", "UnusedReturnValue"})
44public class BaseOptions<T extends BaseOptions<T>> extends MutableCapabilities implements
45        CanSetCapability<T>,
46        SupportsAutomationNameOption<T>,
47        SupportsEventTimingsOption<T>,
48        SupportsPrintPageSourceOnFindFailureOption<T>,
49        SupportsNoResetOption<T>,
50        SupportsFullResetOption<T>,
51        SupportsNewCommandTimeoutOption<T>,
52        SupportsBrowserNameOption<T>,
53        SupportsPlatformVersionOption<T> {
54    private static final AcceptedW3CCapabilityKeys W3C_KEY_PATTERNS = new AcceptedW3CCapabilityKeys();
55
56    /**
57     * Creates new instance with no preset capabilities.
58     */
59    public BaseOptions() {
60    }
61
62    /**
63     * Creates new instance with provided capabilities.
64     *
65     * @param source Capabilities map to merge into new instance
66     */
67    public BaseOptions(Map<String, ?> source) {
68        super(source);
69    }
70
71    /**
72     * Creates new instance with provided capabilities.
73     *
74     * @param source is Capabilities instance to merge into new instance
75     */
76    public BaseOptions(Capabilities source) {
77        super(source);
78    }
79
80    /**
81     * Set the kind of mobile device or emulator to use.
82     *
83     * @param platform the kind of mobile device or emulator to use.
84     * @return self instance for chaining.
85     * @see CapabilityType#PLATFORM_NAME
86     */
87    public T setPlatformName(String platform) {
88        return amend(CapabilityType.PLATFORM_NAME, platform);
89    }
90
91    @Override
92    @Nullable
93    public Platform getPlatformName() {
94        return Optional.ofNullable(getCapability(CapabilityType.PLATFORM_NAME))
95                .map(cap -> {
96                    if (cap instanceof Platform) {
97                        return (Platform) cap;
98                    }
99
100                    try {
101                        return Platform.fromString((String.valueOf(cap)));
102                    } catch (WebDriverException e) {
103                        return null;
104                    }
105                })
106                .orElse(null);
107    }
108
109    @Override
110    public Map<String, Object> asMap() {
111        return unmodifiableMap(super.asMap().entrySet().stream()
112                .collect(Collectors.toMap(entry -> W3C_KEY_PATTERNS.test(entry.getKey())
113                        ? entry.getKey() : APPIUM_PREFIX + entry.getKey(), Map.Entry::getValue)
114                ));
115    }
116
117    @Override
118    public T merge(Capabilities extraCapabilities) {
119        T result = this.clone();
120        extraCapabilities.asMap().forEach((key, value) -> {
121            if (value != null) {
122                result.setCapability(key, value);
123            }
124        });
125        return result;
126    }
127
128    /**
129     * Makes a deep clone of the current Options instance.
130     *
131     * @return A deep instance clone.
132     */
133    @SuppressWarnings("MethodDoesntCallSuperMethod")
134    public T clone() {
135        try {
136            Constructor<?> constructor = getClass().getConstructor(Capabilities.class);
137            //noinspection unchecked
138            return (T) constructor.newInstance(this);
139        } catch (InvocationTargetException | NoSuchMethodException
140                | InstantiationException | IllegalAccessException e) {
141            throw new IllegalStateException(e);
142        }
143    }
144
145    @Override
146    public void setCapability(String key, @Nullable Object value) {
147        Require.nonNull("Capability name", key);
148        super.setCapability(W3C_KEY_PATTERNS.test(key) ? key : APPIUM_PREFIX + key, value);
149    }
150
151    @Override
152    @Nullable
153    public Object getCapability(String capabilityName) {
154        Object value = super.getCapability(capabilityName);
155        if (value == null) {
156            value = super.getCapability(APPIUM_PREFIX + capabilityName);
157        }
158        return value;
159    }
160}
Full Screen
copy
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18package org.openqa.selenium.safari;
19
20import org.junit.Test;
21import org.junit.experimental.categories.Category;
22import org.openqa.selenium.ImmutableCapabilities;
23import org.openqa.selenium.remote.AcceptedW3CCapabilityKeys;
24import org.openqa.selenium.remote.CapabilityType;
25import org.openqa.selenium.testing.UnitTests;
26
27import java.util.Map;
28import java.util.Set;
29import java.util.function.Predicate;
30
31import static java.util.stream.Collectors.toSet;
32import static org.assertj.core.api.Assertions.assertThat;
33import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
34import static org.openqa.selenium.remote.Browser.SAFARI;
35
36@Category(UnitTests.class)
37public class SafariOptionsTest {
38
39  @Test
40  public void roundTrippingToCapabilitiesAndBackWorks() {
41    SafariOptions expected = new SafariOptions().setUseTechnologyPreview(true);
42
43    // Convert to a Map so we can create a standalone capabilities instance, which we then use to
44    // create a new set of options. This is the round trip, ladies and gentlemen.
45    SafariOptions seen = new SafariOptions(new ImmutableCapabilities(expected.asMap()));
46
47    assertThat(seen).isEqualTo(expected);
48  }
49
50  @Test
51  public void canConstructFromCapabilities() {
52    SafariOptions options = new SafariOptions();
53    assertThat(options.getUseTechnologyPreview()).isFalse();
54
55    options = new SafariOptions(
56      new ImmutableCapabilities(CapabilityType.BROWSER_NAME, SafariOptions.SAFARI_TECH_PREVIEW));
57    assertThat(options.getUseTechnologyPreview()).isTrue();
58
59    options = new SafariOptions(
60      new ImmutableCapabilities(CapabilityType.BROWSER_NAME, SAFARI.browserName()));
61    assertThat(options.getUseTechnologyPreview()).isFalse();
62  }
63
64  @Test
65  public void canSetAutomaticInspection() {
66    SafariOptions options = new SafariOptions().setAutomaticInspection(true);
67    assertThat(options.getAutomaticInspection()).isTrue();
68  }
69
70  @Test
71  public void canSetAutomaticProfiling() {
72    SafariOptions options = new SafariOptions().setAutomaticProfiling(true);
73    assertThat(options.getAutomaticProfiling()).isTrue();
74  }
75
76  @Test
77  public void settingTechnologyPreviewModeAlsoChangesBrowserName() {
78    SafariOptions options = new SafariOptions();
79    assertThat(options.getBrowserName()).isEqualTo(SAFARI.browserName());
80
81    options.setUseTechnologyPreview(true);
82    assertThat(options.getBrowserName()).isEqualTo(SafariOptions.SAFARI_TECH_PREVIEW);
83
84    options.setUseTechnologyPreview(false);
85    assertThat(options.getBrowserName()).isEqualTo(SAFARI.browserName());
86  }
87
88  @Test
89  public void optionsAsMapShouldBeImmutable() {
90    Map<String, Object> options = new SafariOptions().asMap();
91    assertThatExceptionOfType(UnsupportedOperationException.class)
92        .isThrownBy(() -> options.put("browserType", "chrome"));
93  }
94
95  @Test
96  public void isW3CSafe() {
97    Map<String, Object> converted = new SafariOptions()
98      .setUseTechnologyPreview(true)
99      .setAutomaticInspection(true)
100      .setAutomaticProfiling(true)
101      .asMap();
102
103    Predicate<String> badKeys = new AcceptedW3CCapabilityKeys().negate();
104    Set<String> seen = converted.keySet().stream()
105      .filter(badKeys)
106      .collect(toSet());
107
108    assertThat(seen).isEmpty();
109  }
110}
111
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in AcceptedW3CCapabilityKeys

Trigger test code on LambdaTest Cloud Grid

Execute automation tests with test on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie