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

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

Run io.appium automation tests on LambdaTest cloud grid

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

AndroidDriver.java

Source: AndroidDriver.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.android;
18
19import static io.appium.java_client.android.AndroidMobileCommandHelper.endTestCoverageCommand;
20import static io.appium.java_client.android.AndroidMobileCommandHelper.openNotificationsCommand;
21import static io.appium.java_client.android.AndroidMobileCommandHelper.toggleLocationServicesCommand;
22import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT;
23
24import com.google.common.collect.ImmutableMap;
25
26import io.appium.java_client.AppiumDriver;
27import io.appium.java_client.CommandExecutionHelper;
28import io.appium.java_client.ExecuteCDPCommand;
29import io.appium.java_client.HasAppStrings;
30import io.appium.java_client.HasDeviceTime;
31import io.appium.java_client.HasOnScreenKeyboard;
32import io.appium.java_client.HidesKeyboard;
33import io.appium.java_client.InteractsWithApps;
34import io.appium.java_client.PullsFiles;
35import io.appium.java_client.LocksDevice;
36import io.appium.java_client.PerformsTouchActions;
37import io.appium.java_client.PushesFiles;
38import io.appium.java_client.SupportsLegacyAppManagement;
39import io.appium.java_client.android.connection.HasNetworkConnection;
40import io.appium.java_client.android.geolocation.SupportsExtendedGeolocationCommands;
41import io.appium.java_client.android.nativekey.PressesKey;
42import io.appium.java_client.battery.HasBattery;
43import io.appium.java_client.remote.SupportsContextSwitching;
44import io.appium.java_client.remote.SupportsLocation;
45import io.appium.java_client.remote.SupportsRotation;
46import io.appium.java_client.screenrecording.CanRecordScreen;
47import io.appium.java_client.service.local.AppiumDriverLocalService;
48import io.appium.java_client.service.local.AppiumServiceBuilder;
49import io.appium.java_client.ws.StringWebSocketClient;
50import org.openqa.selenium.Capabilities;
51import org.openqa.selenium.Platform;
52import org.openqa.selenium.remote.HttpCommandExecutor;
53import org.openqa.selenium.remote.html5.RemoteLocationContext;
54import org.openqa.selenium.remote.http.HttpClient;
55
56import java.net.URL;
57import java.util.Collections;
58import java.util.Map;
59
60/**
61 * Android driver implementation.
62 */
63public class AndroidDriver extends AppiumDriver implements
64        PressesKey,
65        SupportsRotation,
66        SupportsContextSwitching,
67        SupportsLocation,
68        PerformsTouchActions,
69        HidesKeyboard,
70        HasDeviceTime,
71        PullsFiles,
72        InteractsWithApps,
73        SupportsLegacyAppManagement,
74        HasAppStrings,
75        HasNetworkConnection,
76        PushesFiles,
77        StartsActivity,
78        LocksDevice,
79        HasAndroidSettings,
80        HasAndroidDeviceDetails,
81        HasSupportedPerformanceDataType,
82        AuthenticatesByFinger,
83        HasOnScreenKeyboard,
84        CanRecordScreen,
85        SupportsSpecialEmulatorCommands,
86        SupportsNetworkStateManagement,
87        ListensToLogcatMessages,
88        HasAndroidClipboard,
89        HasBattery<AndroidBatteryInfo>,
90        ExecuteCDPCommand,
91        CanReplaceElementValue,
92        SupportsExtendedGeolocationCommands {
93    private static final String ANDROID_PLATFORM = Platform.ANDROID.name();
94
95    private StringWebSocketClient logcatClient;
96
97    /**
98     * Creates a new instance based on command {@code executor} and {@code capabilities}.
99     *
100     * @param executor is an instance of {@link HttpCommandExecutor}
101     *                 or class that extends it. Default commands or another vendor-specific
102     *                 commands may be specified there.
103     * @param capabilities take a look at {@link Capabilities}
104     */
105    public AndroidDriver(HttpCommandExecutor executor, Capabilities capabilities) {
106        super(executor, ensurePlatformName(capabilities, ANDROID_PLATFORM));
107    }
108
109    /**
110     * Creates a new instance based on Appium server URL and {@code capabilities}.
111     *
112     * @param remoteAddress is the address of remotely/locally started Appium server
113     * @param capabilities take a look at {@link Capabilities}
114     */
115    public AndroidDriver(URL remoteAddress, Capabilities capabilities) {
116        super(remoteAddress, ensurePlatformName(capabilities, ANDROID_PLATFORM));
117    }
118
119    /**
120     * Creates a new instance based on Appium server URL, HTTP client factory and {@code capabilities}.
121     *
122     * @param remoteAddress is the address of remotely/locally started Appium server
123     * @param httpClientFactory take a look at {@link HttpClient.Factory}
124     * @param capabilities take a look at {@link Capabilities}
125     */
126    public AndroidDriver(
127            URL remoteAddress, HttpClient.Factory httpClientFactory, Capabilities capabilities) {
128        super(remoteAddress, httpClientFactory, ensurePlatformName(capabilities, ANDROID_PLATFORM));
129    }
130
131    /**
132     * Creates a new instance based on Appium driver local service and {@code capabilities}.
133     *
134     * @param service take a look at {@link AppiumDriverLocalService}
135     * @param capabilities take a look at {@link Capabilities}
136     */
137    public AndroidDriver(AppiumDriverLocalService service, Capabilities capabilities) {
138        super(service, ensurePlatformName(capabilities, ANDROID_PLATFORM));
139    }
140
141    /**
142     * Creates a new instance based on Appium driver local service, HTTP client factory and {@code capabilities}.
143     *
144     * @param service take a look at {@link AppiumDriverLocalService}
145     * @param httpClientFactory take a look at {@link HttpClient.Factory}
146     * @param capabilities take a look at {@link Capabilities}
147     */
148    public AndroidDriver(
149            AppiumDriverLocalService service, HttpClient.Factory httpClientFactory, Capabilities capabilities) {
150        super(service, httpClientFactory, ensurePlatformName(capabilities, ANDROID_PLATFORM));
151    }
152
153    /**
154     * Creates a new instance based on Appium service builder and {@code capabilities}.
155     *
156     * @param builder take a look at {@link AppiumServiceBuilder}
157     * @param capabilities take a look at {@link Capabilities}
158     */
159    public AndroidDriver(AppiumServiceBuilder builder, Capabilities capabilities) {
160        super(builder, ensurePlatformName(capabilities, ANDROID_PLATFORM));
161    }
162
163    /**
164     * Creates a new instance based on Appium service builder, HTTP client factory and {@code capabilities}.
165     *
166     * @param builder take a look at {@link AppiumServiceBuilder}
167     * @param httpClientFactory take a look at {@link HttpClient.Factory}
168     * @param capabilities take a look at {@link Capabilities}
169     */
170    public AndroidDriver(AppiumServiceBuilder builder, HttpClient.Factory httpClientFactory,
171        Capabilities capabilities) {
172        super(builder, httpClientFactory, ensurePlatformName(capabilities, ANDROID_PLATFORM));
173    }
174
175    /**
176     * Creates a new instance based on HTTP client factory and {@code capabilities}.
177     *
178     * @param httpClientFactory take a look at {@link HttpClient.Factory}
179     * @param capabilities take a look at {@link Capabilities}
180     */
181    public AndroidDriver(HttpClient.Factory httpClientFactory, Capabilities capabilities) {
182        super(httpClientFactory, ensurePlatformName(capabilities, ANDROID_PLATFORM));
183    }
184
185    /**
186     * Creates a new instance based on {@code capabilities}.
187     *
188     * @param capabilities take a look at {@link Capabilities}
189     */
190    public AndroidDriver(Capabilities capabilities) {
191        super(ensurePlatformName(capabilities, ANDROID_PLATFORM));
192    }
193
194    /**
195     * Get test-coverage data.
196     *
197     * @param intent intent to broadcast.
198     * @param path   path to .ec file.
199     */
200    public void endTestCoverage(String intent, String path) {
201        CommandExecutionHelper.execute(this, endTestCoverageCommand(intent, path));
202    }
203
204    /**
205     * Open the notification shade, on Android devices.
206     */
207    public void openNotifications() {
208        CommandExecutionHelper.execute(this, openNotificationsCommand());
209    }
210
211    public void toggleLocationServices() {
212        CommandExecutionHelper.execute(this, toggleLocationServicesCommand());
213    }
214
215    @SuppressWarnings("unchecked")
216    @Override
217    public AndroidBatteryInfo getBatteryInfo() {
218        return new AndroidBatteryInfo((Map<String, Object>) execute(EXECUTE_SCRIPT, ImmutableMap.of(
219                "script", "mobile: batteryInfo", "args", Collections.emptyList())).getValue());
220    }
221
222    @Override
223    public RemoteLocationContext getLocationContext() {
224        return locationContext;
225    }
226
227    @Override
228    public synchronized StringWebSocketClient getLogcatClient() {
229        if (logcatClient == null) {
230            logcatClient = new StringWebSocketClient();
231        }
232        return logcatClient;
233    }
234}
235
Full Screen

IOSDriver.java

Source: IOSDriver.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.ios;
18
19import static io.appium.java_client.MobileCommand.prepareArguments;
20import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT;
21
22import com.google.common.collect.ImmutableMap;
23
24import io.appium.java_client.AppiumDriver;
25import io.appium.java_client.HasAppStrings;
26import io.appium.java_client.HasDeviceTime;
27import io.appium.java_client.HasOnScreenKeyboard;
28import io.appium.java_client.HidesKeyboard;
29import io.appium.java_client.HidesKeyboardWithKeyName;
30import io.appium.java_client.InteractsWithApps;
31import io.appium.java_client.PullsFiles;
32import io.appium.java_client.LocksDevice;
33import io.appium.java_client.PerformsTouchActions;
34import io.appium.java_client.PushesFiles;
35import io.appium.java_client.SupportsLegacyAppManagement;
36import io.appium.java_client.battery.HasBattery;
37import io.appium.java_client.remote.SupportsContextSwitching;
38import io.appium.java_client.remote.SupportsLocation;
39import io.appium.java_client.remote.SupportsRotation;
40import io.appium.java_client.screenrecording.CanRecordScreen;
41import io.appium.java_client.service.local.AppiumDriverLocalService;
42import io.appium.java_client.service.local.AppiumServiceBuilder;
43import io.appium.java_client.ws.StringWebSocketClient;
44import org.openqa.selenium.Alert;
45import org.openqa.selenium.Capabilities;
46import org.openqa.selenium.Platform;
47import org.openqa.selenium.remote.DriverCommand;
48import org.openqa.selenium.remote.HttpCommandExecutor;
49import org.openqa.selenium.remote.Response;
50import org.openqa.selenium.remote.html5.RemoteLocationContext;
51import org.openqa.selenium.remote.http.HttpClient;
52
53import java.net.URL;
54import java.util.Collections;
55import java.util.Map;
56
57/**
58 * iOS driver implementation.
59 */
60public class IOSDriver extends AppiumDriver implements
61        SupportsContextSwitching,
62        SupportsRotation,
63        SupportsLocation,
64        HidesKeyboard,
65        HasDeviceTime,
66        PullsFiles,
67        InteractsWithApps,
68        SupportsLegacyAppManagement,
69        HasAppStrings,
70        PerformsTouchActions,
71        HidesKeyboardWithKeyName,
72        ShakesDevice,
73        HasIOSSettings,
74        HasOnScreenKeyboard,
75        LocksDevice,
76        PerformsTouchID,
77        PushesFiles,
78        CanRecordScreen,
79        HasIOSClipboard,
80        ListensToSyslogMessages,
81        HasBattery<IOSBatteryInfo> {
82    private static final String PLATFORM_NAME = Platform.IOS.name();
83
84    private StringWebSocketClient syslogClient;
85
86    /**
87     * Creates a new instance based on command {@code executor} and {@code capabilities}.
88     *
89     * @param executor is an instance of {@link HttpCommandExecutor}
90     *                 or class that extends it. Default commands or another vendor-specific
91     *                 commands may be specified there.
92     * @param capabilities take a look at {@link Capabilities}
93     */
94    public IOSDriver(HttpCommandExecutor executor, Capabilities capabilities) {
95        super(executor, ensurePlatformName(capabilities, PLATFORM_NAME));
96    }
97
98    /**
99     * Creates a new instance based on Appium server URL and {@code capabilities}.
100     *
101     * @param remoteAddress is the address of remotely/locally started Appium server
102     * @param capabilities take a look at {@link Capabilities}
103     */
104    public IOSDriver(URL remoteAddress, Capabilities capabilities) {
105        super(remoteAddress, ensurePlatformName(capabilities, PLATFORM_NAME));
106    }
107
108    /**
109     * Creates a new instance based on Appium server URL, HTTP client factory and {@code capabilities}.
110     *
111     * @param remoteAddress is the address of remotely/locally started Appium server
112     * @param httpClientFactory take a look at {@link HttpClient.Factory}
113     * @param capabilities take a look at {@link Capabilities}
114     */
115    public IOSDriver(URL remoteAddress, HttpClient.Factory httpClientFactory,
116        Capabilities capabilities) {
117        super(remoteAddress, httpClientFactory, ensurePlatformName(capabilities, PLATFORM_NAME));
118    }
119
120    /**
121     * Creates a new instance based on Appium driver local service and {@code capabilities}.
122     *
123     * @param service take a look at {@link AppiumDriverLocalService}
124     * @param capabilities take a look at {@link Capabilities}
125     */
126    public IOSDriver(AppiumDriverLocalService service, Capabilities capabilities) {
127        super(service, ensurePlatformName(capabilities, PLATFORM_NAME));
128    }
129
130    /**
131     * Creates a new instance based on Appium driver local service, HTTP client factory and {@code capabilities}.
132     *
133     * @param service take a look at {@link AppiumDriverLocalService}
134     * @param httpClientFactory take a look at {@link HttpClient.Factory}
135     * @param capabilities take a look at {@link Capabilities}
136     */
137    public IOSDriver(AppiumDriverLocalService service, HttpClient.Factory httpClientFactory,
138        Capabilities capabilities) {
139        super(service, httpClientFactory, ensurePlatformName(capabilities, PLATFORM_NAME));
140    }
141
142    /**
143     * Creates a new instance based on Appium service builder and {@code capabilities}.
144     *
145     * @param builder take a look at {@link AppiumServiceBuilder}
146     * @param capabilities take a look at {@link Capabilities}
147     */
148    public IOSDriver(AppiumServiceBuilder builder, Capabilities capabilities) {
149        super(builder, ensurePlatformName(capabilities, PLATFORM_NAME));
150    }
151
152    /**
153     * Creates a new instance based on Appium service builder, HTTP client factory and {@code capabilities}.
154     *
155     * @param builder take a look at {@link AppiumServiceBuilder}
156     * @param httpClientFactory take a look at {@link HttpClient.Factory}
157     * @param capabilities take a look at {@link Capabilities}
158     */
159    public IOSDriver(AppiumServiceBuilder builder, HttpClient.Factory httpClientFactory,
160        Capabilities capabilities) {
161        super(builder, httpClientFactory, ensurePlatformName(capabilities, PLATFORM_NAME));
162    }
163
164    /**
165     * Creates a new instance based on HTTP client factory and {@code capabilities}.
166     *
167     * @param httpClientFactory take a look at {@link HttpClient.Factory}
168     * @param capabilities take a look at {@link Capabilities}
169     */
170    public IOSDriver(HttpClient.Factory httpClientFactory, Capabilities capabilities) {
171        super(httpClientFactory, ensurePlatformName(capabilities, PLATFORM_NAME));
172    }
173
174    /**
175     * Creates a new instance based on {@code capabilities}.
176     *
177     * @param capabilities take a look at {@link Capabilities}
178     */
179    public IOSDriver(Capabilities capabilities) {
180        super(ensurePlatformName(capabilities, PLATFORM_NAME));
181    }
182
183    @Override public TargetLocator switchTo() {
184        return new InnerTargetLocator();
185    }
186
187    @SuppressWarnings("unchecked")
188    @Override
189    public IOSBatteryInfo getBatteryInfo() {
190        return new IOSBatteryInfo((Map<String, Object>) execute(EXECUTE_SCRIPT, ImmutableMap.of(
191                "script", "mobile: batteryInfo", "args", Collections.emptyList())).getValue());
192    }
193
194    private class InnerTargetLocator extends RemoteTargetLocator {
195        @Override public Alert alert() {
196            return new IOSAlert(super.alert());
197        }
198    }
199
200    class IOSAlert implements Alert {
201
202        private final Alert alert;
203
204        IOSAlert(Alert alert) {
205            this.alert = alert;
206        }
207
208        @Override public void dismiss() {
209            execute(DriverCommand.DISMISS_ALERT);
210        }
211
212        @Override public void accept() {
213            execute(DriverCommand.ACCEPT_ALERT);
214        }
215
216        @Override public String getText() {
217            Response response = execute(DriverCommand.GET_ALERT_TEXT);
218            return response.getValue().toString();
219        }
220
221        @Override public void sendKeys(String keysToSend) {
222            execute(DriverCommand.SET_ALERT_VALUE, prepareArguments("value", keysToSend));
223        }
224
225    }
226
227    @Override
228    public RemoteLocationContext getLocationContext() {
229        return locationContext;
230    }
231
232    @Override
233    public synchronized StringWebSocketClient getSyslogClient() {
234        if (syslogClient == null) {
235            syslogClient = new StringWebSocketClient();
236        }
237        return syslogClient;
238    }
239}
240
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 context code on LambdaTest Cloud Grid

Execute automation tests with context 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)