How to use getContextHandles method of io.appium.java_client.remote.SupportsContextSwitching class

Best io.appium code snippet using io.appium.java_client.remote.SupportsContextSwitching.getContextHandles

Run io.appium automation tests on LambdaTest cloud grid

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

SupportsContextSwitching.java

Source: SupportsContextSwitching.java Github

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 io.appium.java_client.ExecutesMethod;
21import io.appium.java_client.NoSuchContextException;
22import org.openqa.selenium.ContextAware;
23import org.openqa.selenium.WebDriver;
24import org.openqa.selenium.WebDriverException;
25import org.openqa.selenium.remote.DriverCommand;
26import org.openqa.selenium.remote.Response;
27
28import javax.annotation.Nullable;
29import java.util.LinkedHashSet;
30import java.util.List;
31import java.util.Set;
32
33import static com.google.common.base.Preconditions.checkNotNull;
34
35public interface SupportsContextSwitching extends WebDriver, ContextAware, ExecutesMethod {
36    /**
37     * Switches to the given context.
38     *
39     * @param name The name of the context to switch to.
40     * @return self instance for chaining.
41     */
42    default WebDriver context(String name) {
43        checkNotNull(name, "Must supply a context name");
44        try {
45            execute(DriverCommand.SWITCH_TO_CONTEXT, ImmutableMap.of("name", name));
46            return this;
47        } catch (WebDriverException e) {
48            throw new NoSuchContextException(e.getMessage(), e);
49        }
50    }
51
52    /**
53     * Get the names of available contexts.
54     *
55     * @return List list of context names.
56     */
57    default Set<String> getContextHandles() {
58        Response response = execute(DriverCommand.GET_CONTEXT_HANDLES, ImmutableMap.of());
59        Object value = response.getValue();
60        try {
61            //noinspection unchecked
62            List<String> returnedValues = (List<String>) value;
63            return new LinkedHashSet<>(returnedValues);
64        } catch (ClassCastException ex) {
65            throw new WebDriverException(
66                    "Returned value cannot be converted to List<String>: " + value, ex);
67        }
68    }
69
70    /**
71     * Get the name of the current context.
72     *
73     * @return Context name or null if it cannot be determined.
74     */
75    @Nullable
76    default String getContext() {
77        String contextName =
78                String.valueOf(execute(DriverCommand.GET_CURRENT_CONTEXT_HANDLE).getValue());
79        return "null".equalsIgnoreCase(contextName) ? null : contextName;
80    }
81}
82
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 SupportsContextSwitching

Trigger getContextHandles code on LambdaTest Cloud Grid

Execute automation tests with getContextHandles 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
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)