Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle alerts in android using appium

How do I handle alerts in an Android web application using Appium server (1.0.1) and the Android SDK?

The below code is not working on android:

driver.switchTo().accept().alert();

Error message:

> -modal window does not get closed
like image 628
user3816956 Avatar asked Dec 26 '22 07:12

user3816956


2 Answers

You need to get the Alert before you try and accept it

This is code from some of the Appium Java Client Tests:

wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();

This should work most of the time.

If accept() isn't working, replace the driver.switchTo().alert(); and alert.accept(); with code to find the button and then click it.

If it's not finding the button wrap findElementBy(Method) code in a try/retry block, and then click on it.

like image 188
Jess Avatar answered Jan 10 '23 20:01

Jess


The best way is to use the appium inspector. Click on the element and copy the resource-id from it. Use this resource id in findElement(By.id()) method.

For me resource-id: android:id/button1

((AndroidDriver) driver).findElement(By.id("android:id/button1")).click();

This is for Android. For regular use you can use

driver.findElement(By.id("android:id/button1")).click();
like image 28
Rohan Avatar answered Jan 10 '23 20:01

Rohan