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

Alert API Implementation for iOS #459

Merged
merged 1 commit into from
Aug 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
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);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't you use

@Override public void dismiss() {
    alert.dismiss();
}

instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

@TikhomirovSergey alert.dismiss() cannot be used as RemoteWebDriver uses RemoteWebDriver.this.execute("dimissAlertW3C") i.e W3C API which we didn't adopt yet. It is the same for alert.accept() as well

}

@Override public void accept() {
execute(DriverCommand.ACCEPT_ALERT);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't you use

@Override public void dismiss() {
    alert.accept();
}

instead?

}

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