Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support for Appium Chrome Dev Protocol Commands #1375

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/io/appium/java_client/AppiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
@SuppressWarnings("unchecked")
public class AppiumDriver<T extends WebElement>
extends DefaultGenericMobileDriver<T> implements ComparesImages, FindsByImage<T>, FindsByCustom<T>,
ExecutesDriverScript, LogsEvents, HasSettings {
ExecutesDriverScript, LogsEvents, HasSettings,ExecuteCDPCommand {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature is currently only present in AndroidDriver


private static final ErrorHandler errorHandler = new ErrorHandler(new ErrorCodesMobile(), true);
// frequently used command parameters
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/io/appium/java_client/ExecuteCDPCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client;

import org.openqa.selenium.remote.Response;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.appium.java_client.MobileCommand.EXECUTE_CDP_COMMAND;

public interface ExecuteCDPCommand extends ExecutesMethod {

/**
* Allows to execute ChromeDevProtocol commands against Appium browser session
*
* @since Appium 1.18
* @param command Command to execute against the browser (For Ref : https://chromedevtools.github.io/devtools-protocol/)
* @param params additional parameters required to execute the command
* @return Value (Output of the command execution)
* @throws org.openqa.selenium.WebDriverException if there was a failure while executing the command
*/
default Map<String,Object> executeCdpCommand(String command, @Nullable Map<String, Object> params) {
Map<String, Object> data = new HashMap<>();
data.put("cmd",checkNotNull(command));
if(params != null){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please auto format all files, so spaces are set properly everywhere

data.put("params",params);
}else{
params = new HashMap<>();
data.put("params",params);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data.put("params", params == null ? Collections.emptyMap() : params)

}
Response response = execute(EXECUTE_CDP_COMMAND, data);
Map<String, Object> value = (Map<String, Object>) response.getValue();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no need in the intermediate value variable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid warning of Unchecked cast, Im continuing with the same representation but will be changing to return as Immutable map

return value;
}

/**
* Allows to execute ChromeDevProtocol commands against Appium browser session without parameters
*
* @since Appium 1.18
* @param command Command to execute against the browser (For Ref : https://chromedevtools.github.io/devtools-protocol/)
* @return Value (Output of the command execution)
* @throws org.openqa.selenium.WebDriverException if there was a failure while executing the command
*/
default Map<String,Object> executeCdpCommand(String command) {
return executeCdpCommand(command,null);
}
}
3 changes: 3 additions & 0 deletions src/main/java/io/appium/java_client/MobileCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public class MobileCommand {
protected static final String COMPARE_IMAGES;
protected static final String EXECUTE_DRIVER_SCRIPT;
protected static final String GET_ALLSESSION;
protected static final String EXECUTE_CDP_COMMAND;

public static final Map<String, CommandInfo> commandRepository;

Expand Down Expand Up @@ -192,6 +193,7 @@ public class MobileCommand {
COMPARE_IMAGES = "compareImages";
EXECUTE_DRIVER_SCRIPT = "executeDriverScript";
GET_ALLSESSION = "getAllSessions";
EXECUTE_CDP_COMMAND = "executeCdp";

commandRepository = new HashMap<>();
commandRepository.put(RESET, postC("/session/:sessionId/appium/app/reset"));
Expand Down Expand Up @@ -282,6 +284,7 @@ public class MobileCommand {
commandRepository.put(COMPARE_IMAGES, postC("/session/:sessionId/appium/compare_images"));
commandRepository.put(EXECUTE_DRIVER_SCRIPT, postC("/session/:sessionId/appium/execute_driver"));
commandRepository.put(GET_ALLSESSION, getC("/sessions"));
commandRepository.put(EXECUTE_CDP_COMMAND, postC("/session/:sessionId/goog/cdp/execute"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the vendor name is configurable. Please rename the constant to EXECUTE_GOOGLE_CDP_COMMAND

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client.appium;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import io.appium.java_client.remote.MobileBrowserType;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import java.util.HashMap;
import java.util.Map;

import static java.time.Duration.ofSeconds;
import static org.junit.Assert.assertNotNull;

public class ExecuteCDPCommandTest {

private WebDriver driver;

private AppiumDriverLocalService service;

@FindBy(name = "q")
private WebElement searchTextField;


/**
* The setting up.
*/
@Before public void setUp() {
service = AppiumDriverLocalService.buildDefaultService();
service.start();

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, MobileBrowserType.CHROME);
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME,"UiAutomator2");
driver = new AndroidDriver<RemoteWebElement>(service.getUrl(), capabilities);
//This time out is set because test can be run on slow Android SDK emulator
PageFactory.initElements(new AppiumFieldDecorator(driver, ofSeconds(5)), this);
}

/**
* finishing.
*/
@After public void tearDown() {
if (driver != null) {
driver.quit();
}

if (service != null) {
service.stop();
}
}

@Test public void testExecuteCDPCommandWithoutParam() {
driver.get("https://www.google.com");
searchTextField.sendKeys("Hello");
Map<String,Object> cookies = ((AppiumDriver)driver).executeCdpCommand("Page.getCookies");
assertNotNull(cookies);
}

@Test public void testExecuteCDPCommandWithParams() {
Map<String,Object> params = new HashMap();
params.put("latitude", 13.0827);
params.put("longitude",80.2707);
params.put("accuracy",1);
((AppiumDriver)driver).executeCdpCommand("Emulation.setGeolocationOverride",params);
driver.get("https://www.google.com");
}

}