From 97580f82db2ba9d09f195200788deb041c10b7d2 Mon Sep 17 00:00:00 2001 From: SrinivasanSekar Date: Tue, 16 Aug 2016 16:09:48 +0530 Subject: [PATCH] Alert API Implementation for iOS Implementation changes Fixed Checkstyle issues Fixed Checkstyle issues --- .../io/appium/java_client/MobileCommand.java | 4 +- .../io/appium/java_client/ios/IOSDriver.java | 53 ++++++++++++++++++- .../appium/java_client/ios/IOSAlertTest.java | 16 +++++- 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/appium/java_client/MobileCommand.java b/src/main/java/io/appium/java_client/MobileCommand.java index 8249902a9..c7131ec15 100644 --- a/src/main/java/io/appium/java_client/MobileCommand.java +++ b/src/main/java/io/appium/java_client/MobileCommand.java @@ -154,7 +154,7 @@ public static CommandInfo deleteC(String url) { * @param value is the parameter value. * @return built {@link ImmutableMap}. */ - protected static ImmutableMap prepareArguments(String param, + public static ImmutableMap prepareArguments(String param, Object value) { ImmutableMap.Builder builder = ImmutableMap.builder(); builder.put(param, value); @@ -166,7 +166,7 @@ protected static ImmutableMap prepareArguments(String param, * @param values is the array with parameter values. * @return built {@link ImmutableMap}. */ - protected static ImmutableMap prepareArguments(String[] params, + public static ImmutableMap prepareArguments(String[] params, Object[] values) { ImmutableMap.Builder builder = ImmutableMap.builder(); for (int i = 0; i < params.length; i++) { diff --git a/src/main/java/io/appium/java_client/ios/IOSDriver.java b/src/main/java/io/appium/java_client/ios/IOSDriver.java index 3c8d839f8..11ebb9512 100644 --- a/src/main/java/io/appium/java_client/ios/IOSDriver.java +++ b/src/main/java/io/appium/java_client/ios/IOSDriver.java @@ -16,6 +16,7 @@ package io.appium.java_client.ios; +import static io.appium.java_client.MobileCommand.prepareArguments; import static io.appium.java_client.ios.IOSMobileCommandHelper.hideKeyboardCommand; import static io.appium.java_client.ios.IOSMobileCommandHelper.lockDeviceCommand; import static io.appium.java_client.ios.IOSMobileCommandHelper.shakeCommand; @@ -27,16 +28,20 @@ import io.appium.java_client.remote.MobilePlatform; import io.appium.java_client.service.local.AppiumDriverLocalService; import io.appium.java_client.service.local.AppiumServiceBuilder; - +import org.openqa.selenium.Alert; import org.openqa.selenium.Capabilities; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.WebElement; +import org.openqa.selenium.remote.DriverCommand; import org.openqa.selenium.remote.HttpCommandExecutor; +import org.openqa.selenium.remote.Response; import org.openqa.selenium.remote.http.HttpClient; +import org.openqa.selenium.security.Credentials; import java.net.URL; import java.util.List; + /** * @param the required type of class which implement * {@link org.openqa.selenium.WebElement}. @@ -220,4 +225,50 @@ public List findElementsByIosUIAutomation(String using) public void lockDevice(int seconds) { CommandExecutionHelper.execute(this, lockDeviceCommand(seconds)); } + + @Override public TargetLocator switchTo() { + return new InnerTargetLocator(); + } + + private class InnerTargetLocator extends RemoteTargetLocator { + @Override public Alert alert() { + return new IOSAlert(super.alert()); + } + } + + + class IOSAlert implements Alert { + + private final Alert alert; + + IOSAlert(Alert alert) { + this.alert = alert; + } + + @Override public void dismiss() { + execute(DriverCommand.DISMISS_ALERT); + } + + @Override public void accept() { + execute(DriverCommand.ACCEPT_ALERT); + } + + @Override public String getText() { + Response response = execute(DriverCommand.GET_ALERT_TEXT); + return response.getValue().toString(); + } + + @Override public void sendKeys(String keysToSend) { + execute(DriverCommand.SET_ALERT_VALUE, prepareArguments("value", keysToSend)); + } + + @Override public void setCredentials(Credentials credentials) { + alert.setCredentials(credentials); + } + + @Override public void authenticateUsing(Credentials credentials) { + alert.authenticateUsing(credentials); + } + + } } diff --git a/src/test/java/io/appium/java_client/ios/IOSAlertTest.java b/src/test/java/io/appium/java_client/ios/IOSAlertTest.java index 24eaa19b5..90f054490 100644 --- a/src/test/java/io/appium/java_client/ios/IOSAlertTest.java +++ b/src/test/java/io/appium/java_client/ios/IOSAlertTest.java @@ -16,9 +16,11 @@ package io.appium.java_client.ios; +import static junit.framework.TestCase.assertFalse; import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent; import io.appium.java_client.MobileBy; +import org.apache.commons.lang3.StringUtils; import org.junit.Test; import org.openqa.selenium.support.ui.WebDriverWait; @@ -28,13 +30,23 @@ public class IOSAlertTest extends BaseIOSTest { driver.findElement(MobileBy .IosUIAutomation(".elements().withName(\"show alert\")")).click(); WebDriverWait wating = new WebDriverWait(driver, 10000); - wating.until(alertIsPresent()).accept(); + wating.until(alertIsPresent()); + driver.switchTo().alert().accept(); } @Test public void dismissAlertTest() { driver.findElement(MobileBy .IosUIAutomation(".elements().withName(\"show alert\")")).click(); WebDriverWait wating = new WebDriverWait(driver, 10000); - wating.until(alertIsPresent()).dismiss(); + wating.until(alertIsPresent()); + driver.switchTo().alert().dismiss(); + } + + @Test public void getAlertTextTest() { + driver.findElement(MobileBy + .IosUIAutomation(".elements().withName(\"show alert\")")).click(); + WebDriverWait wating = new WebDriverWait(driver, 10000); + wating.until(alertIsPresent()); + assertFalse(StringUtils.isBlank(driver.switchTo().alert().getText())); } }