How to allow camera permission (OS level dialog) using Selenium RemoteWebDriver?



  • 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.



  • 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();
        }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2