How to use ExecuteCDPCommand class of io.appium.java_client package

Best io.appium code snippet using io.appium.java_client.ExecuteCDPCommand

Run io.appium automation tests on LambdaTest cloud grid

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

ExecuteCDPCommandTest.java

Source: ExecuteCDPCommandTest.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 io.appium.java_client.pagefactory.AppiumFieldDecorator;
20import io.appium.java_client.remote.MobileBrowserType;
21import io.appium.java_client.remote.MobileCapabilityType;
22import io.appium.java_client.service.local.AppiumDriverLocalService;
23import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
26import org.openqa.selenium.WebDriver;
27import org.openqa.selenium.WebElement;
28import org.openqa.selenium.remote.DesiredCapabilities;
29import org.openqa.selenium.remote.RemoteWebElement;
30import org.openqa.selenium.support.FindBy;
31import org.openqa.selenium.support.PageFactory;
32
33import java.util.HashMap;
34import java.util.Map;
35
36import static java.time.Duration.ofSeconds;
37import static org.junit.Assert.assertNotNull;
38
39public class ExecuteCDPCommandTest {
40
41    private WebDriver driver;
42
43    private AppiumDriverLocalService service;
44
45    @FindBy(name = "q")
46    private WebElement searchTextField;
47
48
49    /**
50     * The setting up.
51     */
52    @Before
53    public void setUp() {
54        service = AppiumDriverLocalService.buildDefaultService();
55        service.start();
56
57        DesiredCapabilities capabilities = new DesiredCapabilities();
58        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
59        capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, MobileBrowserType.CHROME);
60        capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
61        driver = new AndroidDriver<RemoteWebElement>(service.getUrl(), capabilities);
62        //This time out is set because test can be run on slow Android SDK emulator
63        PageFactory.initElements(new AppiumFieldDecorator(driver, ofSeconds(5)), this);
64    }
65
66    /**
67     * finishing.
68     */
69    @After
70    public void tearDown() {
71        if (driver != null) {
72            driver.quit();
73        }
74
75        if (service != null) {
76            service.stop();
77        }
78    }
79
80    @Test
81    public void testExecuteCDPCommandWithoutParam() {
82        driver.get("https://www.google.com");
83        searchTextField.sendKeys("Hello");
84        Map<String, Object> cookies = ((AndroidDriver) driver).executeCdpCommand("Page.getCookies");
85        assertNotNull(cookies);
86    }
87
88    @Test
89    public void testExecuteCDPCommandWithParams() {
90        Map<String, Object> params = new HashMap();
91        params.put("latitude", 13.0827);
92        params.put("longitude", 80.2707);
93        params.put("accuracy", 1);
94        ((AndroidDriver) driver).executeCdpCommand("Emulation.setGeolocationOverride", params);
95        driver.get("https://www.google.com");
96    }
97
98}
99
Full Screen

ExecuteCDPCommand.java

Source: ExecuteCDPCommand.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;
18
19import com.google.common.collect.ImmutableMap;
20import org.openqa.selenium.remote.Response;
21
22import javax.annotation.Nullable;
23import java.util.Collections;
24import java.util.HashMap;
25import java.util.Map;
26
27import static com.google.common.base.Preconditions.checkNotNull;
28import static io.appium.java_client.MobileCommand.EXECUTE_GOOGLE_CDP_COMMAND;
29
30public interface ExecuteCDPCommand extends ExecutesMethod {
31
32    /**
33     * Allows to execute ChromeDevProtocol commands against Android Chrome browser session.
34     *
35     * @param command Command to execute against the browser (For Ref : https://chromedevtools.github.io/devtools-protocol/)
36     * @param params additional parameters required to execute the command
37     * @return Value (Output of the command execution)
38     * @throws org.openqa.selenium.WebDriverException if there was a failure while executing the command
39     * @since Appium 1.18
40     */
41    default Map<String, Object> executeCdpCommand(String command, @Nullable Map<String, Object> params) {
42        Map<String, Object> data = new HashMap<>();
43        data.put("cmd", checkNotNull(command));
44        data.put("params", params == null ? Collections.emptyMap() : params);
45        Response response = execute(EXECUTE_GOOGLE_CDP_COMMAND, data);
46        //noinspection unchecked
47        return ImmutableMap.copyOf((Map<String, Object>) response.getValue());
48    }
49
50    /**
51     * Allows to execute ChromeDevProtocol commands against Android Chrome browser session without parameters.
52     *
53     * @param command Command to execute against the browser (For Ref : https://chromedevtools.github.io/devtools-protocol/)
54     * @return Value (Output of the command execution)
55     * @throws org.openqa.selenium.WebDriverException if there was a failure while executing the command
56     * @since Appium 1.18
57     */
58    default Map<String, Object> executeCdpCommand(String command) {
59        return executeCdpCommand(command, null);
60    }
61}
62
Full Screen

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.FindsByAndroidDataMatcher;
30import io.appium.java_client.FindsByAndroidViewMatcher;
31import io.appium.java_client.FindsByAndroidUIAutomator;
32import io.appium.java_client.FindsByAndroidViewTag;
33import io.appium.java_client.HasOnScreenKeyboard;
34import io.appium.java_client.LocksDevice;
35import io.appium.java_client.android.connection.HasNetworkConnection;
36import io.appium.java_client.android.nativekey.PressesKey;
37import io.appium.java_client.battery.HasBattery;
38import io.appium.java_client.remote.MobilePlatform;
39import io.appium.java_client.screenrecording.CanRecordScreen;
40import io.appium.java_client.service.local.AppiumDriverLocalService;
41import io.appium.java_client.service.local.AppiumServiceBuilder;
42import io.appium.java_client.ws.StringWebSocketClient;
43import org.openqa.selenium.Capabilities;
44import org.openqa.selenium.WebElement;
45import org.openqa.selenium.remote.HttpCommandExecutor;
46import org.openqa.selenium.remote.http.HttpClient;
47
48import java.net.URL;
49import java.util.Collections;
50import java.util.Map;
51
52/**
53 * Android driver implementation.
54 *
55 * @param <T> the required type of class which implement {@link org.openqa.selenium.WebElement}.
56 *           Instances of the defined type will be returned via findElement* and findElements*.
57 *           Warning (!!!). Allowed types:
58 *           {@link org.openqa.selenium.WebElement}
59 *           {@link org.openqa.selenium.remote.RemoteWebElement}
60 *           {@link io.appium.java_client.MobileElement}
61 *           {@link io.appium.java_client.android.AndroidElement}
62 */
63public class AndroidDriver<T extends WebElement>
64    extends AppiumDriver<T>
65    implements PressesKey, HasNetworkConnection, PushesFiles, StartsActivity,
66        FindsByAndroidUIAutomator<T>, FindsByAndroidViewTag<T>, FindsByAndroidDataMatcher<T>,
67        FindsByAndroidViewMatcher<T>, LocksDevice, HasAndroidSettings, HasAndroidDeviceDetails,
68        HasSupportedPerformanceDataType, AuthenticatesByFinger, HasOnScreenKeyboard,
69        CanRecordScreen, SupportsSpecialEmulatorCommands,
70        SupportsNetworkStateManagement, ListensToLogcatMessages, HasAndroidClipboard,
71        HasBattery<AndroidBatteryInfo>, ExecuteCDPCommand {
72
73    private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;
74
75    private StringWebSocketClient logcatClient;
76
77    /**
78     * Creates a new instance based on command {@code executor} and {@code capabilities}.
79     *
80     * @param executor is an instance of {@link HttpCommandExecutor}
81     *                 or class that extends it. Default commands or another vendor-specific
82     *                 commands may be specified there.
83     * @param capabilities take a look at {@link Capabilities}
84     */
85    public AndroidDriver(HttpCommandExecutor executor, Capabilities capabilities) {
86        super(executor, updateDefaultPlatformName(capabilities, ANDROID_PLATFORM));
87    }
88
89    /**
90     * Creates a new instance based on Appium server URL and {@code capabilities}.
91     *
92     * @param remoteAddress is the address of remotely/locally started Appium server
93     * @param desiredCapabilities take a look at {@link Capabilities}
94     */
95    public AndroidDriver(URL remoteAddress, Capabilities desiredCapabilities) {
96        super(remoteAddress, updateDefaultPlatformName(desiredCapabilities, ANDROID_PLATFORM));
97    }
98
99    /**
100     * Creates a new instance based on Appium server URL, HTTP client factory and {@code capabilities}.
101     *
102     * @param remoteAddress is the address of remotely/locally started Appium server
103     * @param httpClientFactory take a look at {@link HttpClient.Factory}
104     * @param desiredCapabilities take a look at {@link Capabilities}
105     */
106    public AndroidDriver(URL remoteAddress, HttpClient.Factory httpClientFactory,
107        Capabilities desiredCapabilities) {
108        super(remoteAddress, httpClientFactory,
109            updateDefaultPlatformName(desiredCapabilities, ANDROID_PLATFORM));
110    }
111
112    /**
113     * Creates a new instance based on Appium driver local service and {@code capabilities}.
114     *
115     * @param service take a look at {@link AppiumDriverLocalService}
116     * @param desiredCapabilities take a look at {@link Capabilities}
117     */
118    public AndroidDriver(AppiumDriverLocalService service, Capabilities desiredCapabilities) {
119        super(service, updateDefaultPlatformName(desiredCapabilities, ANDROID_PLATFORM));
120    }
121
122    /**
123     * Creates a new instance based on Appium driver local service, HTTP client factory and {@code capabilities}.
124     *
125     * @param service take a look at {@link AppiumDriverLocalService}
126     * @param httpClientFactory take a look at {@link HttpClient.Factory}
127     * @param desiredCapabilities take a look at {@link Capabilities}
128     */
129    public AndroidDriver(AppiumDriverLocalService service, HttpClient.Factory httpClientFactory,
130        Capabilities desiredCapabilities) {
131        super(service, httpClientFactory,
132            updateDefaultPlatformName(desiredCapabilities, ANDROID_PLATFORM));
133    }
134
135    /**
136     * Creates a new instance based on Appium service builder and {@code capabilities}.
137     *
138     * @param builder take a look at {@link AppiumServiceBuilder}
139     * @param desiredCapabilities take a look at {@link Capabilities}
140     */
141    public AndroidDriver(AppiumServiceBuilder builder, Capabilities desiredCapabilities) {
142        super(builder, updateDefaultPlatformName(desiredCapabilities, ANDROID_PLATFORM));
143    }
144
145    /**
146     * Creates a new instance based on Appium service builder, HTTP client factory and {@code capabilities}.
147     *
148     * @param builder take a look at {@link AppiumServiceBuilder}
149     * @param httpClientFactory take a look at {@link HttpClient.Factory}
150     * @param desiredCapabilities take a look at {@link Capabilities}
151     */
152    public AndroidDriver(AppiumServiceBuilder builder, HttpClient.Factory httpClientFactory,
153        Capabilities desiredCapabilities) {
154        super(builder, httpClientFactory,
155            updateDefaultPlatformName(desiredCapabilities, ANDROID_PLATFORM));
156    }
157
158    /**
159     * Creates a new instance based on HTTP client factory and {@code capabilities}.
160     *
161     * @param httpClientFactory take a look at {@link HttpClient.Factory}
162     * @param desiredCapabilities take a look at {@link Capabilities}
163     */
164    public AndroidDriver(HttpClient.Factory httpClientFactory, Capabilities desiredCapabilities) {
165        super(httpClientFactory, updateDefaultPlatformName(desiredCapabilities, ANDROID_PLATFORM));
166    }
167
168    /**
169     * Creates a new instance based on {@code capabilities}.
170     *
171     * @param desiredCapabilities take a look at {@link Capabilities}
172     */
173    public AndroidDriver(Capabilities desiredCapabilities) {
174        super(updateDefaultPlatformName(desiredCapabilities, ANDROID_PLATFORM));
175    }
176
177    /**
178     * Get test-coverage data.
179     *
180     * @param intent intent to broadcast.
181     * @param path   path to .ec file.
182     */
183    public void endTestCoverage(String intent, String path) {
184        CommandExecutionHelper.execute(this, endTestCoverageCommand(intent, path));
185    }
186
187    /**
188     * Open the notification shade, on Android devices.
189     */
190    public void openNotifications() {
191        CommandExecutionHelper.execute(this, openNotificationsCommand());
192    }
193
194    public void toggleLocationServices() {
195        CommandExecutionHelper.execute(this, toggleLocationServicesCommand());
196    }
197
198    @SuppressWarnings("unchecked")
199    @Override
200    public AndroidBatteryInfo getBatteryInfo() {
201        return new AndroidBatteryInfo((Map<String, Object>) execute(EXECUTE_SCRIPT, ImmutableMap.of(
202                "script", "mobile: batteryInfo", "args", Collections.emptyList())).getValue());
203    }
204
205    @Override
206    public synchronized StringWebSocketClient getLogcatClient() {
207        if (logcatClient == null) {
208            logcatClient = new StringWebSocketClient();
209        }
210        return logcatClient;
211    }
212}
213
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 methods in ExecuteCDPCommand

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)