Search code examples
javaandroidseleniumbrowserstack

How to dismiss/accept Device Location Popup on Android devices - Selenium with Browser stack


I am stuck with this issue from past some days. Can anyone please help or provide some idea on how it can be achieved.

What/Where I am performing:

  1. Browser: Chrome
  2. Device: Samsung Galaxy S20
  3. Remote: Browser Stack
  4. Test: Selenium with Java connecting Browser Stack

I have tried many ways to dismiss the location popup appearing post navigating to the URL, but each time Selenium tests doesn't even dismiss or recognise that alert. Below is the picture of the device location popup I am receiving upon navigation:

enter image description here

Code that I have tried:

Capabilities:

    caps.setCapability("os_version", "10.0");
    caps.setCapability("device", "Samsung Galaxy S20");
    caps.setCapability("realMobile", "true");
    caps.setCapability("browser", "Chrome");
    caps.setCapability("browserstack.debug", "true");
    caps.setCapability("browserstack.console", "errors");
    caps.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
    caps.setCapability("name", scenario);

I have seen at many places to add "Autograntpermissions". Tried that as well, but didn't work

caps.setCapability("autoGrantPermissions", "true");

I have tried the usual switching to an alert and dismissing it or accepting it but it doesn't work either. Throws "No Such Alert"

driver.switchTo().alert().dismiss(); 

I thought, maybe this might work, by adding a profile, but it doesn't seem to detect as well and always the popup remains

ChromeOptions options = new ChromeOptions();
Map < String, Object > prefs = new HashMap < String, Object > ();
Map < String, Object > profile = new HashMap < String, Object > ();
Map < String, Object > contentSettings = new HashMap < String, Object > ();

contentSettings.put("geolocation", 1);
profile.put("managed_default_content_settings", contentSettings);
prefs.put("profile", profile);
options.setExperimentalOption("prefs", prefs);

caps.setCapability(ChromeOptions.CAPABILITY, options);
//... other capabilities

Solution

  • The alert is triggered by JS on the webpage but is displayed in the NATIVE_APP context. Any alert that's not part of the webpage usually shows up in the NATIVE context.

    This alert needs to be handled via Appium as mentioned here:

    https://www.browserstack.com/docs/automate/selenium/handle-permission-pop-ups#handle-know-your-location-pop-up

    driver.switch_to.context('NATIVE_APP')
    driver.find_element_by_xpath(".//android.widget.Button[@text='Allow']").click()
    time.sleep(2)