how to handle alerts in android using appium

后端 未结 9 1894
野趣味
野趣味 2020-12-11 08:18

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:

drive         


        
相关标签:
9条回答
  • 2020-12-11 08:19

    // first check alert is present or not

    public boolean isAlertPresent() {
        try {
            mobDriver.switchTo().alert();
            log.info("ALERT IS PRESENT !! ");
            return true;
        } catch (Exception e) {
            log.info("ALERT IS NOT PRESENT !! ");
            return false;
        }
    }
    
    public void mobileAlertHandle() {
        if (isAlertPresent()) {
            Alert alert = mobDriver.switchTo().alert();
            alert.accept();
       }
    }
    

    if this does not work then inspect your element and try with id or name

    ex: mobDriver.findElementById("android:id/button2").click()

    0 讨论(0)
  • 2020-12-11 08:23

    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();
    
    0 讨论(0)
  • 2020-12-11 08:24

    So an updated answer on this is this: an AlertDialog is a system level element, so clicking on accept button you should use:

    androidDriver.findElementById("android:id/button1").click()
    

    else for cancel do this:

    androidDriver.findElementById("android:id/button2").click()
    
    0 讨论(0)
  • 2020-12-11 08:25

    Please use the below code, Add some wait before clicking on OK Button. After that pass the xpath of you OK Button.

    synchronized (driver)
    {
    driver.wait(2000);
    }
    driver.context(NATIVE_APP);
    driver.findElementByXPath{("//android.widget.Button[@resourceid=
    ‘android:id/button1’]").click();
    
    0 讨论(0)
  • 2020-12-11 08:27

    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.

    0 讨论(0)
  • 2020-12-11 08:27

    Appium comes with a default capability to accept, dismiss alerts

    capabilities.SetCapability("autoAcceptAlerts", true);
    capabilities.SetCapability("autoDismissAlerts", true);
    
    0 讨论(0)
提交回复
热议问题