|
| 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 | + |
| 17 | +package io.appium.java_client.android; |
| 18 | + |
| 19 | +import io.appium.java_client.pagefactory.AppiumFieldDecorator; |
| 20 | +import io.appium.java_client.remote.MobileBrowserType; |
| 21 | +import io.appium.java_client.remote.MobileCapabilityType; |
| 22 | +import io.appium.java_client.service.local.AppiumDriverLocalService; |
| 23 | +import org.junit.After; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | +import org.openqa.selenium.WebDriver; |
| 27 | +import org.openqa.selenium.WebElement; |
| 28 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 29 | +import org.openqa.selenium.remote.RemoteWebElement; |
| 30 | +import org.openqa.selenium.support.FindBy; |
| 31 | +import org.openqa.selenium.support.PageFactory; |
| 32 | + |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +import static java.time.Duration.ofSeconds; |
| 37 | +import static org.junit.Assert.assertNotNull; |
| 38 | + |
| 39 | +public 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 | +} |
0 commit comments