1

I have a PWA (which means essentially a webpage + serviceworker) that asks for the user's camera and location via getUserMedia and getCurrentPosition calls. These create an OS level dialog box with options to Allow | Deny. It appears that Selenium driver cannot manipulate these dialogs. Is there any way to get past the dialog's using Selenium's RemoteWebDriver? The objective is to run the webpage on mobile devices using a service like BrowserStack.

2
  • Is your app a native or web app? Commented Sep 7, 2019 at 5:22
  • Its a web app, a webpage with a serviceworker basically
    – patrick
    Commented Sep 8, 2019 at 1:11

1 Answer 1

0

The trick is to flip the driver into NATIVE_APP mode (this seemed a bit dubious since I have a normal webpage, but it worked)

Here's a test console app for dot net framework 4.
On some phones 'Allow' might be 'ALLOW' and the XPath matching is case sensitive

Note: if you are using BrowserStack and run out of minutes you have to choose the 'Automate Mobile' plan, their pricing page is highly confusing

static void Main(string[] args)
    {
        DesiredCapabilities capability = new DesiredCapabilities();
        capability.SetCapability("device", "Samsung Galaxy S9");
        capability.SetCapability("real_mobile", "true");
        capability.SetCapability("browserstack.user", username);
        capability.SetCapability("browserstack.key", password);

        capability.SetCapability("autoAcceptAlerts", "true");
        capability.SetCapability("autoGrantPermissions", "true");

        AndroidDriver<IWebElement> driver =
            new AndroidDriver<IWebElement>(
                new Uri("http://hub-cloud.browserstack.com/wd/hub"), capability);


        driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/download");
        driver.Context = ("NATIVE_APP");  // this does the trick <<<< 

        IWebElement ll = driver.FindElement(By.XPath("//*[text()='some-file.txt']"));
        ll.Click();
        System.Threading.Thread.Sleep(2000);

        // To accept/block the popup, 
        //you need to switch the context to “NATIVE_APP“ 
        //and click on the Allow/Block button.
        //driver.Context = ("NATIVE_APP");
        ll = driver.FindElement(By.XPath(".//android.widget.Button[@text='Continue']"));
        ll.Click();
        System.Threading.Thread.Sleep(2000);

        ll = driver.FindElement(By.XPath(".//android.widget.Button[@text='ALLOW']"));
        ll.Click();

        System.Threading.Thread.Sleep(2000);
        driver.Quit();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.