Skip to content

Commit

Permalink
Merge pull request #459 from SrinivasanTarget/alerthandling
Browse files Browse the repository at this point in the history
Alert API Implementation for iOS
  • Loading branch information
TikhomirovSergey committed Aug 19, 2016
2 parents a17d183 + 97580f8 commit 1e61110
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/appium/java_client/MobileCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public static CommandInfo deleteC(String url) {
* @param value is the parameter value.
* @return built {@link ImmutableMap}.
*/
protected static ImmutableMap<String, Object> prepareArguments(String param,
public static ImmutableMap<String, Object> prepareArguments(String param,
Object value) {
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
builder.put(param, value);
Expand All @@ -166,7 +166,7 @@ protected static ImmutableMap<String, Object> prepareArguments(String param,
* @param values is the array with parameter values.
* @return built {@link ImmutableMap}.
*/
protected static ImmutableMap<String, Object> prepareArguments(String[] params,
public static ImmutableMap<String, Object> prepareArguments(String[] params,
Object[] values) {
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
for (int i = 0; i < params.length; i++) {
Expand Down
53 changes: 52 additions & 1 deletion src/main/java/io/appium/java_client/ios/IOSDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <T> the required type of class which implement
* {@link org.openqa.selenium.WebElement}.
Expand Down Expand Up @@ -220,4 +225,50 @@ public List<T> 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);
}

}
}
16 changes: 14 additions & 2 deletions src/test/java/io/appium/java_client/ios/IOSAlertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()));
}
}

0 comments on commit 1e61110

Please sign in to comment.