-
-
Notifications
You must be signed in to change notification settings - Fork 765
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support for Appium Chrome Dev Protocol Commands (#1375)
* Client support for Appium : Chrome Dev Protocol * Review Changes - Client support for Appium : Chrome Dev Protocol * Review changes for documentation * CheckStyle Fix
- Loading branch information
1 parent
f27d29d
commit c33b577
Showing
4 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/main/java/io/appium/java_client/ExecuteCDPCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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 com.google.common.collect.ImmutableMap; | ||
import org.openqa.selenium.remote.Response; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
import static io.appium.java_client.MobileCommand.EXECUTE_GOOGLE_CDP_COMMAND; | ||
|
||
public interface ExecuteCDPCommand extends ExecutesMethod { | ||
|
||
/** | ||
* Allows to execute ChromeDevProtocol commands against Android Chrome browser session. | ||
* | ||
* @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 | ||
* @since Appium 1.18 | ||
*/ | ||
default Map<String, Object> executeCdpCommand(String command, @Nullable Map<String, Object> params) { | ||
Map<String, Object> data = new HashMap<>(); | ||
data.put("cmd", checkNotNull(command)); | ||
data.put("params", params == null ? Collections.emptyMap() : params); | ||
Response response = execute(EXECUTE_GOOGLE_CDP_COMMAND, data); | ||
//noinspection unchecked | ||
return ImmutableMap.copyOf((Map<String, Object>) response.getValue()); | ||
} | ||
|
||
/** | ||
* Allows to execute ChromeDevProtocol commands against Android Chrome browser session without parameters. | ||
* | ||
* @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 | ||
* @since Appium 1.18 | ||
*/ | ||
default Map<String, Object> executeCdpCommand(String command) { | ||
return executeCdpCommand(command, null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/test/java/io/appium/java_client/android/ExecuteCDPCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* 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.android; | ||
|
||
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 = ((AndroidDriver) 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); | ||
((AndroidDriver) driver).executeCdpCommand("Emulation.setGeolocationOverride", params); | ||
driver.get("https://www.google.com"); | ||
} | ||
|
||
} |