javaseleniumtestingautomationappium

Appium in Web app: Unable to tap Allow permission button in notification pop up window


When I am opening the web app, I am getting a pop up. I'm trying to click on 'ALLOW' button in two ways:

  1. When I add permissions:
caps.setCapability("autoGrantPermissions", true);
caps.setCapability("autoAcceptAlerts", true);
......
driver.switchTo().alert().accept();

nothing happens

  1. When I try to find it by XPath:
driver.findElement(By.xpath("//android.widget.Button[@text='Allow']")).click();

I get an error:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:
{"method":"xpath","selector":"//android.widget.Button[@text='Allow']"}

  1. If I try to get it with ID:
driver.findElement(By.id("android:id/button1")).click();

I get this error:

Returned value cannot be converted to WebElement: {message=no such element: Unable to locate element: {"method":"id","selector":"android:id/button1"}

This is my screenshot from UI Automator Viewer :

enter image description here

I found this post: Unable to tap the link after tap on Allow button of permission alert in Appium? but it didn't help me.


Solution

  • First: capabilities you were trying to use are for IOS only

    On Android you have to find popup via findElement and close them yourself.

    Second: since you start Appium session for web application, before searching for native popups you must switch the context:

        String webContext = driver.getContext();
        Set<String> contexts = driver.getContextHandles();
        for (String context: contexts){
            if (context.contains("NATIVE_APP")){
                driver.context(context);
                break;
            }
        }
        driver.findElement(By.id("android:id/button1")).click();
    

    Don't forget to switch context back to web to continue:

        driver.context(webContext);