Skip to content

Commit 1e61110

Browse files
Merge pull request #459 from SrinivasanTarget/alerthandling
Alert API Implementation for iOS
2 parents a17d183 + 97580f8 commit 1e61110

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

src/main/java/io/appium/java_client/MobileCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public static CommandInfo deleteC(String url) {
154154
* @param value is the parameter value.
155155
* @return built {@link ImmutableMap}.
156156
*/
157-
protected static ImmutableMap<String, Object> prepareArguments(String param,
157+
public static ImmutableMap<String, Object> prepareArguments(String param,
158158
Object value) {
159159
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
160160
builder.put(param, value);
@@ -166,7 +166,7 @@ protected static ImmutableMap<String, Object> prepareArguments(String param,
166166
* @param values is the array with parameter values.
167167
* @return built {@link ImmutableMap}.
168168
*/
169-
protected static ImmutableMap<String, Object> prepareArguments(String[] params,
169+
public static ImmutableMap<String, Object> prepareArguments(String[] params,
170170
Object[] values) {
171171
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
172172
for (int i = 0; i < params.length; i++) {

src/main/java/io/appium/java_client/ios/IOSDriver.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.appium.java_client.ios;
1818

19+
import static io.appium.java_client.MobileCommand.prepareArguments;
1920
import static io.appium.java_client.ios.IOSMobileCommandHelper.hideKeyboardCommand;
2021
import static io.appium.java_client.ios.IOSMobileCommandHelper.lockDeviceCommand;
2122
import static io.appium.java_client.ios.IOSMobileCommandHelper.shakeCommand;
@@ -27,16 +28,20 @@
2728
import io.appium.java_client.remote.MobilePlatform;
2829
import io.appium.java_client.service.local.AppiumDriverLocalService;
2930
import io.appium.java_client.service.local.AppiumServiceBuilder;
30-
31+
import org.openqa.selenium.Alert;
3132
import org.openqa.selenium.Capabilities;
3233
import org.openqa.selenium.WebDriverException;
3334
import org.openqa.selenium.WebElement;
35+
import org.openqa.selenium.remote.DriverCommand;
3436
import org.openqa.selenium.remote.HttpCommandExecutor;
37+
import org.openqa.selenium.remote.Response;
3538
import org.openqa.selenium.remote.http.HttpClient;
39+
import org.openqa.selenium.security.Credentials;
3640

3741
import java.net.URL;
3842
import java.util.List;
3943

44+
4045
/**
4146
* @param <T> the required type of class which implement
4247
* {@link org.openqa.selenium.WebElement}.
@@ -220,4 +225,50 @@ public List<T> findElementsByIosUIAutomation(String using)
220225
public void lockDevice(int seconds) {
221226
CommandExecutionHelper.execute(this, lockDeviceCommand(seconds));
222227
}
228+
229+
@Override public TargetLocator switchTo() {
230+
return new InnerTargetLocator();
231+
}
232+
233+
private class InnerTargetLocator extends RemoteTargetLocator {
234+
@Override public Alert alert() {
235+
return new IOSAlert(super.alert());
236+
}
237+
}
238+
239+
240+
class IOSAlert implements Alert {
241+
242+
private final Alert alert;
243+
244+
IOSAlert(Alert alert) {
245+
this.alert = alert;
246+
}
247+
248+
@Override public void dismiss() {
249+
execute(DriverCommand.DISMISS_ALERT);
250+
}
251+
252+
@Override public void accept() {
253+
execute(DriverCommand.ACCEPT_ALERT);
254+
}
255+
256+
@Override public String getText() {
257+
Response response = execute(DriverCommand.GET_ALERT_TEXT);
258+
return response.getValue().toString();
259+
}
260+
261+
@Override public void sendKeys(String keysToSend) {
262+
execute(DriverCommand.SET_ALERT_VALUE, prepareArguments("value", keysToSend));
263+
}
264+
265+
@Override public void setCredentials(Credentials credentials) {
266+
alert.setCredentials(credentials);
267+
}
268+
269+
@Override public void authenticateUsing(Credentials credentials) {
270+
alert.authenticateUsing(credentials);
271+
}
272+
273+
}
223274
}

src/test/java/io/appium/java_client/ios/IOSAlertTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
package io.appium.java_client.ios;
1818

19+
import static junit.framework.TestCase.assertFalse;
1920
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
2021

2122
import io.appium.java_client.MobileBy;
23+
import org.apache.commons.lang3.StringUtils;
2224
import org.junit.Test;
2325
import org.openqa.selenium.support.ui.WebDriverWait;
2426

@@ -28,13 +30,23 @@ public class IOSAlertTest extends BaseIOSTest {
2830
driver.findElement(MobileBy
2931
.IosUIAutomation(".elements().withName(\"show alert\")")).click();
3032
WebDriverWait wating = new WebDriverWait(driver, 10000);
31-
wating.until(alertIsPresent()).accept();
33+
wating.until(alertIsPresent());
34+
driver.switchTo().alert().accept();
3235
}
3336

3437
@Test public void dismissAlertTest() {
3538
driver.findElement(MobileBy
3639
.IosUIAutomation(".elements().withName(\"show alert\")")).click();
3740
WebDriverWait wating = new WebDriverWait(driver, 10000);
38-
wating.until(alertIsPresent()).dismiss();
41+
wating.until(alertIsPresent());
42+
driver.switchTo().alert().dismiss();
43+
}
44+
45+
@Test public void getAlertTextTest() {
46+
driver.findElement(MobileBy
47+
.IosUIAutomation(".elements().withName(\"show alert\")")).click();
48+
WebDriverWait wating = new WebDriverWait(driver, 10000);
49+
wating.until(alertIsPresent());
50+
assertFalse(StringUtils.isBlank(driver.switchTo().alert().getText()));
3951
}
4052
}

0 commit comments

Comments
 (0)