Webinar - Join us on 26th Oct. to hear all about Selenium 4 and its exciting new features from David Burns, Core Contributor to Selenium. Register here

Handle Permission Pop-ups

This article shows you how to handle user permission pop-ups, alerts, and prompts in your automated tests on BrowserStack.

Introduction

Some functionalities in your web apps may need user-permissions. For instance:

The browser then triggers a pop-up box with Confirm or Deny interactions. In this page, you’ll see how to automate those interactions on remote browsers and real mobile devices.

Handle “Know your location” pop-up

You can test what happens when your web app is allowed to (or blocked from) accessing the device location, both while testing in Desktop or in Mobile devices:

A pop-up like the following will open in the desktop where the test is running:

Browser Location Access Popup

The snippet below lets you automate an Allow or Block interaction when the remote Chrome browser asks for location.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class AllowGeoLocationPopup {
  public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
  public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

  public static void main(String[] args) throws Exception {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("browser", "Chrome");
    caps.setCapability("browser_version", "75.0");
    caps.setCapability("os", "Windows");
    caps.setCapability("os_version", "10");

    // INIT CHROME OPTIONS
    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 > ();

    // SET CHROME OPTIONS
    // 0 - Default, 1 - Allow, 2 - Block
    contentSettings.put("geolocation", 1);
    profile.put("managed_default_content_settings", contentSettings);
    prefs.put("profile", profile);
    options.setExperimentalOption("prefs", prefs);

    // SET CAPABILITY
    caps.setCapability(ChromeOptions.CAPABILITY, options);
    WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
    driver.get("https://the-internet.herokuapp.com/geolocation");
    driver.findElement(By.xpath("//*[@id='content']/div/button")).click();
    Thread.sleep(5000);
    driver.quit();
  }
}
var webdriver = require("selenium-webdriver");

// SET CAPABILITY
var capabilities = {
  'browserName' : 'Chrome',
  'browser_version' : '75.0',
  'os' : 'Windows',
  'os_version' : '10',
  'browserstack.user' : 'YOUR_USERNAME',
  'browserstack.key' : 'YOUR_ACCESS_KEY',

  // SET CHROME OPTIONS
  'goog:chromeOptions': {
    prefs: {
      // 0 - Default, 1 - Allow, 2 - Block
      'profile.managed_default_content_settings.geolocation' : 1
    }
  }
}

var driver = new webdriver.Builder()
  .usingServer('http://hub-cloud.browserstack.com/wd/hub/')
  .withCapabilities(capabilities)
  .build();

driver.get('https://the-internet.herokuapp.com/geolocation')
  .then(function(){
    driver.findElement(webdriver.By.xpath("//*[@id='content']/div/button")).click()
      .then(function(){
        driver.quit();
      });
  });
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;

namespace SampleCSharp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            RemoteWebDriver driver;
            Dictionary<string, object> profile = new Dictionary<string, object>();

            // 0 - Default, 1 - Allow, 2 - Block
            profile.Add("profile.default_content_setting_values.geolocation", 1);

            // INIT CHROME OPTIONS
            Dictionary<string, object> chromeOptions = new Dictionary<string, object>();

            // SET CHROME OPTIONS
            chromeOptions.Add("prefs", profile);

            // SET CAPABILITY
            ChromeOptions capability = new ChromeOptions();
            capability.AddAdditionalCapability("browser", "Chrome", true);
            capability.AddAdditionalCapability("browser_version", "75.0", true);
            capability.AddAdditionalCapability("os", "Windows", true);
            capability.AddAdditionalCapability("os_version", "10", true);
            capability.AddAdditionalCapability("browserstack.debug", "true", true);
            capability.AddAdditionalCapability("browserstack.user", "YOUR_USERNAME", true);
            capability.AddAdditionalCapability("browserstack.key", "YOUR_ACCESS_KEY", true);
            capability.AddAdditionalCapability("chromeOptions", chromeOptions, true);

            driver = new RemoteWebDriver(new Uri("http://hub-cloud.browserstack.com/wd/hub/"), capability);
            driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/geolocation");
            driver.FindElement(By.XPath("//*[@id='content']/div/button")).Click();
            Thread.Sleep(5000);
            driver.Quit();
        }

    }
}

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options

# INIT CHROME OPTIONS
chrome_options = Options()
# 0 - Default, 1 - Allow, 2 - Block

# SET CHROME OPTIONS
chrome_options.add_experimental_option("prefs", { "profile.default_content_setting_values.geolocation": 1})

# SET CAPABILITY
desired_cap = chrome_options.to_capabilities()
desired_cap.update({
  'browser_version': '75.0',
  'os': 'Windows',
  'os_version': '10'
})

driver = webdriver.Remote(command_executor='http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub', desired_capabilities=desired_cap)
driver.get("https://the-internet.herokuapp.com/geolocation")
driver.find_element_by_xpath("//*[@id='content']/div/button").click()
sleep(10)
driver.quit()
require 'rubygems'
require 'selenium-webdriver'

# SET CHROME OPTIONS
# 0 - Default, 1 - Allow, 2 - Block
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"prefs" => { "profile.default_content_setting_values.geolocation" => 1 } })

# SET CAPABILITY
caps['browser'] = 'Chrome'
caps['browser_version'] = '75.0'
caps['os'] = 'Windows'
caps['os_version'] = '10'
driver = Selenium::WebDriver.for(:remote,
  :url => "http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
  :desired_capabilities => caps)
driver.navigate.to "https://the-internet.herokuapp.com/geolocation"
driver.find_element(:xpath, "//*[@id='content']/div/button").click;
sleep 2
driver.quit

A pop-up like the following will open in the mobile device where the test is running:

Mobile Device Location Access Popup

The snippet below lets you automate an Allow or Block interaction when the remote Chrome browser asks an Android device for location.

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;

import java.net.URL;
import java.util.Set;

public class AllowGeoLocationPopup {
  public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
  public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

  public static void main(String[] args) throws Exception {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("browser", "Chrome");
    caps.setCapability("browserName", "android");
    caps.setCapability("device", "Samsung Galaxy S10");
    caps.setCapability("realMobile", "true");
    caps.setCapability("os_version", "9.0");
    AppiumDriver driver = new AndroidDriver(new URL(URL), caps);
    driver.get("https://the-internet.herokuapp.com/geolocation");
    driver.findElement(By.xpath("//*[@id='content']/div/button")).click();
    Thread.sleep(5000);
    // 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");
    driver.findElement(By.xpath(".//android.widget.Button[@text='Allow']")).click();
    Thread.sleep(5000);
    driver.quit();
  }
}
let webdriver = require('wd');

let capabilities = {
  'os_version': '9.0',
  'device': 'Samsung Galaxy S10',
  'real_mobile': 'true',
  'browserstack.user': 'YOUR_USERNAME',
  'browserstack.key': 'YOUR_ACCESS_KEY',
}

let driver = webdriver.promiseRemote("http://hub-cloud.browserstack.com/wd/hub");
driver.init(capabilities, function() {
  driver.get('https://the-internet.herokuapp.com/geolocation')
    .then(function() {
      (async () => {
        let element = await driver.element("xpath", "//*[@id='content']/div/button");
        await element.click();

        // To accept/block the popup, you need to switch the context to “NATIVE_APP“ and click on the Allow/Block button.
        await driver.context('NATIVE_APP');
        let element2 = await driver.element("xpath", ".//android.widget.Button[@text='Allow']")
        await element2.click();
        await driver.quit();
      })();
    });
});
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System.Threading;
using System.Collections.Generic;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Safari;
using OpenQA.Selenium.Appium.iOS;

namespace SampleCSharp
{
    class Program
    {
        public static void Main(string[] args)
        {
            AppiumOptions capability = new AppiumOptions();

            capability.AddAdditionalCapability("device", "Samsung Galaxy S8");
            capability.AddAdditionalCapability("os_version", "7.0");
            capability.AddAdditionalCapability("real_mobile", "true");
            capability.AddAdditionalCapability("browserstack.debug", "true");
            capability.AddAdditionalCapability("browserstack.user", "YOUR_USERNAME");
            capability.AddAdditionalCapability("browserstack.key", "YOUR_ACCESS_KEY");
            AndroidDriver<IWebElement> driver = new AndroidDriver<IWebElement>(new Uri("http://hub-cloud.browserstack.com/wd/hub"), capability);
            driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/geolocation");
            Thread.Sleep(5000);
            IWebElement ll = driver.FindElement(By.XPath("//*[@id='content']/div/button"));
            ll.Click();
            Thread.Sleep(5000);
            // 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='Allow']"));
            ll.Click();
            driver.Quit();
        }
    }
}
import time
from appium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_cap = {
  'device': 'Samsung Galaxy S10',
  'realMobile': 'true',
  'os_version': '9.0'
}
driver = webdriver.Remote(command_executor='http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub.browserstack.com/wd/hub', desired_capabilities=desired_cap)
driver.get("https://the-internet.herokuapp.com/geolocation")
driver.find_element_by_xpath("//*[@id='content']/div/button").click()
time.sleep(2)
# To accept/block the popup, you need to switch the context to “NATIVE_APP“ and click on the Allow/Block button.
driver.switch_to.context('NATIVE_APP')
driver.find_element_by_xpath(".//android.widget.Button[@text='Allow']").click()
time.sleep(2)
driver.quit()
require 'rubygems'
require 'appium_lib'

caps = {}
caps['device'] = 'Samsung Galaxy S10'
caps['platformName'] = 'Android'
caps['realMobile'] = 'true'
caps['os_version'] = '9.0'
appium_driver = Appium::Driver.new({
  'caps' => caps,
  'appium_lib' => {
      :server_url => "http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub"
  }}, true)
driver = appium_driver.start_driver
driver.navigate.to "https://the-internet.herokuapp.com/geolocation"
driver.find_element(:xpath, "//*[@id='content']/div/button").click;
sleep(2)
# To accept/block the popup, you need to switch the context to “NATIVE_APP“ and click on the Allow/Block button.
driver.set_context('NATIVE_APP')
driver.find_element(:xpath, ".//android.widget.Button[@text='Allow']").click;
sleep(2)
driver.quit
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.IOSElement;
import java.net.URL;
import java.util.Set;

public class AllowGeoLocationPopup {
  public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
  public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

  public static void main(String[] args) throws Exception {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("os_version", "14");
    caps.setCapability("device", "iPhone 12");
    caps.setCapability("realMobile", "true");    

    IOSDriver<IOSElement> driver = new IOSDriver<IOSElement>(new URL("https://"+AUTOMATE_USERNAME+":"+AUTOMATE_KEY+"@hub-cloud.browserstack.com/wd/hub"), caps);
    driver.get("https://the-internet.herokuapp.com/geolocation");
    driver.findElement(By.xpath("//*[@id='content']/div/button")).click();
    Thread.sleep(5000);
    // 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");
    driver.findElement(By.name("Allow")).click();
    Thread.sleep(5000);
    driver.quit();
  }
}
let webdriver = require('wd');

let capabilities = {
  'os_version': '14',
  'device': 'iPhone 12',
  'real_mobile': 'true',
  'browserstack.user': 'YOUR_USERNAME',
  'browserstack.key': 'YOUR_ACCESS_KEY',
}

let driver = webdriver.promiseRemote("http://hub-cloud.browserstack.com/wd/hub");
driver.init(capabilities, function() {
  driver.get('https://the-internet.herokuapp.com/geolocation')
    .then(function() {
      (async () => {
        let element = await driver.element("xpath", "//*[@id='content']/div/button");
        await element.click();

        // To accept/block the popup, you need to switch the context to “NATIVE_APP“ and click on the Allow/Block button.
        await driver.context('NATIVE_APP');
        let element2 = await driver.element("name", "Allow")
        await element2.click();
        await driver.quit();
      })();
    });
});
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System.Threading;
using System.Collections.Generic;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Safari;
using OpenQA.Selenium.Appium.iOS;

namespace SampleCSharp
{
    class Program
    {
        public static void Main(string[] args)
        {
            AppiumOptions capability = new AppiumOptions();

            capability.AddAdditionalCapability("device", "iPhone 12");
            capability.AddAdditionalCapability("os_version", "14");
            capability.AddAdditionalCapability("real_mobile", "true");
            capability.AddAdditionalCapability("browserstack.debug", "true");
            capability.AddAdditionalCapability("browserstack.user", "YOUR_USERNAME");
            capability.AddAdditionalCapability("browserstack.key", "YOUR_ACCESS_KEY");
            IOSDriver<IWebElement>  driver = new IOSDriver<IWebElement>(
              new Uri("https://u:p@hub-cloud.browserstack.com/wd/hub/"), capability
            );
            driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/geolocation");
            Thread.Sleep(5000);
            IWebElement ll = driver.FindElement(By.XPath("//*[@id='content']/div/button"));
            ll.Click();
            Thread.Sleep(5000);
            // 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.Name("Allow"));
            ll.Click();
            driver.Quit();
        }
    }
}
import time
from appium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_cap = {
  "os_version" : "14",
  "device" : "iPhone 12",
  "real_mobile" : "true",
}
driver = webdriver.Remote(command_executor='http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub.browserstack.com/wd/hub', desired_capabilities=desired_cap)
driver.get("https://the-internet.herokuapp.com/geolocation")
driver.find_element_by_xpath("//*[@id='content']/div/button").click()
time.sleep(2)
# To accept/block the popup, you need to switch the context to “NATIVE_APP“ and click on the Allow/Block button.
driver.switch_to.context('NATIVE_APP')
driver.find_element_by_name('Allow').click()
time.sleep(2)
driver.quit()
require 'rubygems'
require 'appium_lib'

caps = {}
caps['device'] = 'iPhone 12'
caps['platformName'] = 'iOS'
caps['realMobile'] = 'true'
caps['os_version'] = '14'

appium_driver = Appium::Driver.new({
  'caps' => caps,
  'appium_lib' => {
      :server_url => "http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub"
  }}, true)
driver = appium_driver.start_driver
driver.navigate.to "https://the-internet.herokuapp.com/geolocation"
driver.find_element(:xpath, "//*[@id='content']/div/button").click;
sleep(2)
# To accept/block the popup, you need to switch the context to “NATIVE_APP“ and click on the Allow/Block button.
driver.set_context('NATIVE_APP')
driver.find_element(:name, "Allow").click;
sleep(2)
driver.quit

Handle “Use your camera” and “Use your microphone” pop-up

You can test what happens when your web app is allowed to (or blocked from) accessing camera or microphone, both while testing in Desktop or in Mobile devices:

The following pop-ups open in the desktop when trying to access camera and microphone respectively:

use camera android Microphone Permission popup

The snippet below lets you automate an Allow or Block interaction when your web app requests access to camera or microphone.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;

public class AllowCameraPopupChrome {
  public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
  public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

  public static void main(String[] args) throws Exception {

    //Configure ChromeOptions to pass fake media stream
    ChromeOptions options = new ChromeOptions();
    options.addArguments("use-fake-device-for-media-stream");
    options.addArguments("use-fake-ui-for-media-stream");

    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("browser", "Chrome");
    caps.setCapability("browser_version", "75.0");
    caps.setCapability("os", "Windows");
    caps.setCapability("os_version", "10");
    caps.setCapability(ChromeOptions.CAPABILITY, options);
    WebDriver driver = new RemoteWebDriver(new URL(URL), caps);

    //WebCam Test
    driver.get("https://webcamtests.com/check");
    Thread.sleep(5000);
    driver.findElement(By.id("webcam-launcher")).click();
    Thread.sleep(2000);

    //Mic Test
    driver.get("https://www.vidyard.com/mic-test/");
    Thread.sleep(2000);
    driver.findElement(By.xpath("//a[@id='start-test']")).click();
    Thread.sleep(2000);

    driver.quit();
  }
}
var webdriver = require('selenium-webdriver');
var chrome = require("selenium-webdriver/chrome");

var capabilities = {
  'browserName': 'Chrome',
  'browser_version': '75.0',
  'os': 'Windows',
  'os_version': '10',
  'browserstack.user': 'YOUR_USERNAME',
  'browserstack.key': 'YOUR_ACCESS_KEY',

  // Configure ChromeOptions to pass fake media stream
  'goog:chromeOptions': {
    'args': ["--use-fake-device-for-media-stream", "--use-fake-ui-for-media-stream"]
  }
}

var driver = new webdriver.Builder()
  .usingServer('http://hub-usw.browserstack.com/wd/hub')
  .withCapabilities(capabilities)
  .build();

// Webcam Test
driver.get('https://webcamtests.com/check')
  .then(function() {
    setTimeout(function() {
      driver.findElement(webdriver.By.id("webcam-launcher")).click()
        .then(function() {
          setTimeout(function() {
            // Mic Test
            driver.get('https://www.vidyard.com/mic-test/')
              .then(function() {
                driver.findElement(webdriver.By.xpath("//a[@id='start-test']")).click()
                  .then(function() {
                    setTimeout(function() {
                        driver.quit();
                    }, 10000);
                });
              });
          }, 5000);
        });
    }, 2000);
  });
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System.Threading;
using OpenQA.Selenium.Chrome;

namespace SampleCSharp
{
    public class AllowCameraMicPopupChrome
    {
        static void Main(string[] args)
        {
            IWebDriver driver;

            //Configure ChromeOptions to pass fake media stream
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.AddArgument("--use-fake-device-for-media-stream");
            chromeOptions.AddArgument("--use-fake-ui-for-media-stream");

            chromeOptions.AddAdditionalCapability("browser", "Chrome", true);
            chromeOptions.AddAdditionalCapability("browser_version", "70.0", true);
            chromeOptions.AddAdditionalCapability("os", "Windows", true);
            chromeOptions.AddAdditionalCapability("os_version", "10", true);
            chromeOptions.AddAdditionalCapability("browserstack.user", "YOUR_USERNAME", true);
            chromeOptions.AddAdditionalCapability("browserstack.key", "YOUR_ACCESS_KEY", true);

            driver = new RemoteWebDriver(
              new Uri("http://hub-cloud.browserstack.com/wd/hub/"), chromeOptions);

            // Webcam Test           
            driver.Navigate().GoToUrl("https://webcamtests.com/check");
            Thread.Sleep(5000);
            driver.FindElement(By.Id("webcam-launcher")).Click();
            Thread.Sleep(5000);

            // Mic Test
            driver.Navigate().GoToUrl("https://www.vidyard.com/mic-test/");
            Thread.Sleep(2000);
            driver.FindElement(By.XPath("//a[@id='start-test']")).Click();
            Thread.Sleep(5000);

            driver.Quit();
        }
    }
}
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
import time

desired_cap = {
  'browser_version': '75.0',
  'os': 'Windows',
  'os_version': '10',

  #Configure ChromeOptions to pass fake media stream
  'chromeOptions': {
    'args': ["--use-fake-device-for-media-stream", "--use-fake-ui-for-media-stream"]
  }
}
driver = webdriver.Remote(command_executor = 'http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub.browserstack.com/wd/hub', desired_capabilities = desired_cap)

# WebCam Test
driver.get("https://webcamtests.com/check")
time.sleep(5)
driver.find_element_by_id("webcam-launcher").click()
time.sleep(5)

# Mic Test
driver.get("https://www.vidyard.com/mic-test/")
time.sleep(5)
driver.find_element_by_xpath("//a[@id='start-test']").click()
time.sleep(10)
driver.quit()
require 'rubygems'
require 'selenium-webdriver'

#Configure ChromeOptions to pass fake media stream
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args" => ['--use-fake-device-for-media-stream', '--use-fake-ui-for-media-stream'] })
caps['browser'] = 'Chrome'
caps['browser_version'] = '75.0'
caps['os'] = 'Windows'
caps['os_version'] = '10'
driver = Selenium::WebDriver.for(:remote, :url => "http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub", :desired_capabilities => caps)

# WebCam Test
driver.navigate.to "https://webcamtests.com/check"
sleep(5)
driver.find_element(:id, "webcam-launcher").click;
sleep(5)

#Mic Test
driver.navigate.to "https://www.vidyard.com/mic-test/"
sleep(5)
driver.find_element(:xpath, "//a[@id='start-test']").click;
sleep(10)
driver.quit

The following pop-ups open in the mobile device when trying to access camera and microphone respectively:

Android Use Your Camera Permission Popup Microphone Permission popup

The snippet below lets you automate an Allow or Block interaction on Android devices when the remote Chrome browser asks for access to camera or microphone.

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;

import java.net.URL;

public class AllowCameraPopupChromeMobile {
  public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
  public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

  public static void main(String[] args) throws Exception {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("browser", "Chrome");
    caps.setCapability("device", "Samsung Galaxy S10");
    caps.setCapability("realMobile", "true");
    caps.setCapability("os_version", "9.0");
    AppiumDriver driver = new AndroidDriver(new URL(URL), caps);

    //WebCam Test
    driver.get("https://webcamtests.com/check");
    Thread.sleep(2000);
    driver.findElement(By.id("webcam-launcher")).click();
    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");
    driver.findElement(By.xpath(".//android.widget.Button[@text='Allow']")).click();
    driver.findElement(By.xpath(".//android.widget.Button[@text='Allow']")).click();
    Thread.sleep(5000);

    driver.context("CHROMIUM");

    //Mic Test
    driver.get("https://www.vidyard.com/mic-test/");
    Thread.sleep(2000);
    driver.findElement(By.xpath("//a[@id='start-test']")).click();
    Thread.sleep(2000);
    driver.context("NATIVE_APP");
    driver.findElement(By.xpath(".//android.widget.Button[@text='Allow']")).click();
    driver.findElement(By.xpath(".//android.widget.Button[@text='Allow']")).click();
    Thread.sleep(2000);

    driver.quit();
  }
}
let webdriver = require('wd');

let capabilities = {
  'os_version': '9.0',
  'device': 'Samsung Galaxy S10',
  'real_mobile': 'true',
  'browserstack.user': 'YOUR_USERNAME',
  'browserstack.key': 'YOUR_ACCESS_KEY'
}

const sleep = require('util').promisify(setTimeout)
let driver = webdriver.promiseRemote("http://hub-cloud.browserstack.com/wd/hub");
driver.init(capabilities, function() {
  (async () => {
    // Camera Test
    await driver.get('https://webcamtests.com/check');
    await sleep(3000);
    let element = await driver.element("id", "webcam-launcher");
    await element.click();

    // To accept/block the popup, you need to switch the context to “NATIVE_APP“ and click on the Allow/Block button.
    await driver.context('NATIVE_APP');
    let element2 = await driver.element("xpath", ".//android.widget.Button[@text='Allow']")
    await element2.click();
    let element3 = await driver.element("xpath", ".//android.widget.Button[@text='Allow']")
    await element3.click();
    await sleep(5000);

    await driver.context('CHROMIUM');

    // Mic Test
    await driver.get('https://www.vidyard.com/mic-test/');
    await sleep(3000);
    let element4 = await driver.element("xpath", "//a[@id='start-test']");
    await element4.click();
    await driver.context('NATIVE_APP');
    let element5 = await driver.element("xpath", ".//android.widget.Button[@text='Allow']")
    await element5.click();
    let element6 = await driver.element("xpath", ".//android.widget.Button[@text='Allow']")
    await element6.click();
    await sleep(3000);
    await driver.quit();
  })();
});
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System.Threading;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium;

namespace SampleCSharp
{
    class Program
    {
        static void Main(string[] args)
        {

            AppiumOptions capability = new AppiumOptions();

            capability.AddAdditionalCapability("device", "Samsung Galaxy S10");
            capability.AddAdditionalCapability("os_version", "9.0");
            capability.AddAdditionalCapability("real_mobile", "true");
            capability.AddAdditionalCapability("browserstack.debug", "true");
            capability.AddAdditionalCapability("browserstack.user", "YOUR_USERNAME");
            capability.AddAdditionalCapability("browserstack.key", "YOUR_ACCESS_KEY");
            AndroidDriver<IWebElement> driver = new AndroidDriver<IWebElement>(new Uri("http://hub-cloud.browserstack.com/wd/hub"), capability);

            // Camera Test
            driver.Navigate().GoToUrl("https://webcamtests.com/check");
            Thread.Sleep(2000);
             driver.FindElement(By.Id("webcam-launcher")).Click();
            // 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");
            driver.FindElement(By.XPath(".//android.widget.Button[@text='Allow']")).Click();
            driver.FindElement(By.XPath(".//android.widget.Button[@text='Allow']")).Click();

            Thread.Sleep(5000);

            driver.Context = ("CHROMIUM");

            // Mic Test
            driver.Navigate().GoToUrl("https://www.vidyard.com/mic-test/");
            Thread.Sleep(2000);
            driver.FindElement(By.XPath("//a[@id='start-test']")).Click();
            driver.Context = ("NATIVE_APP");
            driver.FindElement(By.XPath(".//android.widget.Button[@text='Allow']")).Click();
            driver.FindElement(By.XPath(".//android.widget.Button[@text='Allow']")).Click();
            Thread.Sleep(2000);
            driver.Quit();
        }
    }
}
import time
from appium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_cap = {
  'device': 'Samsung Galaxy S10',
  'realMobile': 'true',
  'os_version': '9.0'
}
driver = webdriver.Remote(command_executor='http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub.browserstack.com/wd/hub', desired_capabilities=desired_cap)

# Camera Test
driver.get("https://webcamtests.com/check")
time.sleep(2)
driver.find_element_by_id("webcam-launcher").click()
# To accept/block the popup, you need to switch the context to NATIVE_APP and click on the Allow/Block button.
driver.switch_to.context('NATIVE_APP')
driver.find_element_by_xpath(".//android.widget.Button[@text='Allow']").click()
driver.find_element_by_xpath(".//android.widget.Button[@text='Allow']").click()
time.sleep(5)

driver.switch_to.context('CHROMIUM')

# Mic Test
driver.get("https://www.vidyard.com/mic-test/")
time.sleep(2)
driver.find_element_by_xpath("//a[@id='start-test']").click()
driver.switch_to.context('NATIVE_APP')
driver.find_element_by_xpath(".//android.widget.Button[@text='Allow']").click()
driver.find_element_by_xpath(".//android.widget.Button[@text='Allow']").click()
time.sleep(2)
driver.quit()
require 'rubygems'
require 'appium_lib'

caps = {}
caps['device'] = 'Samsung Galaxy S10'
caps['platformName'] = 'Android'
caps['realMobile'] = 'true'
caps['os_version'] = '9.0'
appium_driver = Appium::Driver.new({'caps' => caps,'appium_lib' => { :server_url => "http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub" }}, true)
driver = appium_driver.start_driver

# WebCam Test
driver.navigate.to "https://webcamtests.com/check"
sleep(2)
driver.find_element(:id, "webcam-launcher").click;
# To accept/block the popup, you need to switch the context to “NATIVE_APP“ and click on the Allow/Block button.
driver.set_context('NATIVE_APP')
driver.find_element(:xpath, ".//android.widget.Button[@text='Allow']").click;
driver.find_element(:xpath, ".//android.widget.Button[@text='Allow']").click;
sleep(5)

driver.set_context('CHROMIUM')

# Mic Test
driver.navigate.to "https://www.vidyard.com/mic-test/"
sleep(2)
driver.find_element(:xpath, "//a[@id='start-test']").click;
driver.set_context('NATIVE_APP')
driver.find_element(:xpath, ".//android.widget.Button[@text='Allow']").click;
driver.find_element(:xpath, ".//android.widget.Button[@text='Allow']").click;
sleep(2)
driver.quit
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.IOSElement;
import java.net.URL;
import java.util.Set;

public class camera {
  public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
  public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

  public static void main(String[] args) throws Exception {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("device", "iPhone 12");
    caps.setCapability("realMobile", "true");
    caps.setCapability("os_version", "14.0");
    caps.setCapability("device", "iPhone 12");
    caps.setCapability("nativeWebTap", "true");
    IOSDriver<IOSElement> driver = new IOSDriver<IOSElement>(new URL("https://"+AUTOMATE_USERNAME+":"+AUTOMATE_KEY+"@hub-cloud.browserstack.com/wd/hub"), caps);

  //WebCam Test
    driver.get("https://webcammictest.com/");
    Thread.sleep(2000);
    driver.findElement(By.xpath("//*[@id=\"gatsby-focus-wrapper\"]/div[1]/button")).click();
    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");
    WebElement ele = driver.findElement(By.id("Allow"));
    ele.click();
    Thread.sleep(5000);
    Set<String> contextNames = driver.getContextHandles();
    driver.context(contextNames.toArray()[1].toString());

    //Mic Test
    driver.get("https://www.vidyard.com/mic-test/");
    Thread.sleep(2000);
    driver.findElement(By.xpath("//a[@id='start-test']")).click();
    Thread.sleep(2000);
    driver.context("NATIVE_APP");
    driver.findElement(By.name("Allow")).click();
    Thread.sleep(2000);
    driver.quit();
  }
}
let webdriver = require('wd');

let capabilities = {
  'os_version': '14',
  'device': 'iPhone12',
  'real_mobile': 'true',
  'browserstack.user': 'YOUR_USERNAME',
  'browserstack.key': 'YOUR_ACCESS_KEY'
}

const sleep = require('util').promisify(setTimeout)
let driver = webdriver.promiseRemote("http://hub-cloud.browserstack.com/wd/hub");
driver.init(capabilities, function() {
  (async () => {
    // Camera Test
    await driver.get('https://webcammictest.com/');
    await sleep(3000);
    let element2 = await driver.element("xpath", "//*[@id=\"gatsby-focus-wrapper\"]/div[1]/button")
    await element2.click();
    // To accept/block the popup, you need to switch the context to “NATIVE_APP“ and click on the Allow/Block button.
    await driver.context('NATIVE_APP');
    let element3 = await driver.element("name", "Allow")
    await element3.click();
    await sleep(5000);
    let contexts = await driver.contexts();
    await driver.context(contexts[1]);

    // Mic Test
    await driver.get('https://www.vidyard.com/mic-test/');
    await sleep(3000);
    let element4 = await driver.element("xpath", "//a[@id='start-test']");
    await element4.click();
    await sleep(3000);
    await driver.context('NATIVE_APP');
    let element5 = await driver.element("name", "Allow")
    await element5.click();
    await driver.quit();
  })();
});

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System.Threading;
using OpenQA.Selenium.Appium.iOS;
using OpenQA.Selenium.Appium;

namespace SampleCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            AppiumOptions capability = new AppiumOptions();

            capability.AddAdditionalCapability("device", "iPhone 12");
            capability.AddAdditionalCapability("os_version", "14");
            capability.AddAdditionalCapability("real_mobile", "true");
            capability.AddAdditionalCapability("browserstack.debug", "true");

            IOSDriver<IWebElement> driver = new IOSDriver<IWebElement>(new Uri("https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub/"), capability);

            driver.Navigate().GoToUrl("https://webcammictest.com/");
            Thread.Sleep(2000);
            driver.FindElement(By.XPath("//*[@id=\"gatsby-focus-wrapper\"]/div[1]/button")).Click();
            Thread.Sleep(2000);
            driver.Context = ("NATIVE_APP");
            IWebElement ele = driver.FindElement(By.Id("Allow"));
            ele.Click();
            driver.Context = driver.Contexts[1];
            Thread.Sleep(2000);

            driver.Navigate().GoToUrl("https://www.vidyard.com/mic-test/");
            IWebElement ll = driver.FindElement(By.XPath("//a[@id='start-test']"));
            ll.Click();
            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.Name("Allow"));
            ll.Click();
            Thread.Sleep(5000);
            driver.Quit();
        }
    }
}
import time
from appium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_cap = {
  'device': 'iPhone12',
  'realMobile': 'true',
  'os_version': '14'
}
driver = webdriver.Remote(command_executor='http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub.browserstack.com/wd/hub', desired_capabilities=desired_cap)
driver.get("https://webcammictest.com/")
time.sleep(5)
driver.find_element_by_xpath("//*[@id=\"gatsby-focus-wrapper\"]/div[1]/button").click()
driver.switch_to.context('NATIVE_APP')
driver.find_element_by_name("Allow").click()
driver.switch_to.context(driver.contexts[1])
driver.get("https://www.vidyard.com/mic-test/")
driver.find_element_by_xpath("//a[@id='start-test']").click()
driver.switch_to.context('NATIVE_APP')
driver.find_element_by_name("Allow").click()
driver.quit()
require 'rubygems'
require 'appium_lib'

caps = {}
caps['device'] = 'iPhone12'
caps['realMobile'] = 'true'
caps['os_version'] = '14.0'
caps['platformName'] = 'iOS'
appium_driver = Appium::Driver.new({'caps' => caps,'appium_lib' => { :server_url => "http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub" }}, true)
driver = appium_driver.start_driver

# WebCam Test
driver.navigate.to "https://webcammictest.com/"
sleep(2)
driver.find_element(:xpath, "//*[@id=\"gatsby-focus-wrapper\"]/div[1]/button").click;
driver.set_context('NATIVE_APP')
driver.find_element(:name, "Allow").click;
contexts = driver.available_contexts
driver.set_context contexts[1]
sleep(2)
# Mic Test
driver.navigate.to "https://www.vidyard.com/mic-test/"
sleep(2)
driver.find_element(:xpath, "//a[@id='start-test']").click;
driver.set_context('NATIVE_APP')
driver.find_element(:name, "Allow").click;
sleep(2)
driver.quit

Handle “Show notifications” pop-up

You can test what happens when your web app requests permission to show notifications both in desktop and in mobile devices:

The following pop-up appears in desktops asking to allow showing notifications:

popup for notification permission

The snippet below lets you automate an Allow or Block interaction when your web app requests permission to show notifications.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class AllowNotificationPopupDesktop {
  public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
  public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

  public static void main(String[] args) throws Exception {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("browser", "Chrome");
    caps.setCapability("browser_version", "75.0");
    caps.setCapability("os", "Windows");
    caps.setCapability("os_version", "10");

    // INIT CHROME OPTIONS
    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>();

    // SET CHROME OPTIONS
    // 0 - Default, 1 - Allow, 2 - Block
    contentSettings.put("notifications", 1);
    profile.put("managed_default_content_settings", contentSettings);
    prefs.put("profile", profile);
    options.setExperimentalOption("prefs", prefs);

    // SET CAPABILITY
    caps.setCapability(ChromeOptions.CAPABILITY, options);

    WebDriver driver = new RemoteWebDriver(new URL(URL), caps);

    driver.get("https://web-push-book.gauntface.com/demos/notification-examples/");
    driver.findElement(By.xpath("//body/main[1]/p[3]/input[1]")).click();
    Thread.sleep(2000);
    driver.quit();
  }
}
var webdriver = require('selenium-webdriver');
var chrome = require("selenium-webdriver/chrome");

var capabilities = {
  'browserName': 'Chrome',
  'browser_version': '75.0',
  'os': 'Windows',
  'os_version': '10',
  'browserstack.user': 'YOUR_USERNAME',
  'browserstack.key': 'YOUR_ACCESS_KEY',
  'goog:chromeOptions': {
    prefs: {
      // 0 - Default, 1 - Allow, 2 - Block
      'profile.managed_default_content_settings.notifications': 1
    }
  }
}

var driver = new webdriver.Builder()
  .usingServer('http://hub-usw.browserstack.com/wd/hub')
  .withCapabilities(capabilities)
  .build();

driver.get('https://web-push-book.gauntface.com/demos/notification-examples/')
  .then(function() {
    driver.findElement(webdriver.By.xpath("//body/main[1]/p[3]/input[1]")).click()
      .then(function() {
        driver.quit();
      });
  });
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System.Threading;
using System.Collections.Generic;
using OpenQA.Selenium.Chrome;

namespace SampleCSharp
{
    public class Program
    {
        static void Main(string[] args)
        {
            RemoteWebDriver driver;
            Dictionary<string, object> profile = new Dictionary<string, object>();
            // 0 - Default, 1 - Allow, 2 - Block
            profile.Add("profile.default_content_setting_values.notifications", 1);
            Dictionary<string, object> chromeOptions = new Dictionary<string, object>();
            chromeOptions.Add("prefs", profile);

            // SET CAPABILITY
            ChromeOptions capability = new ChromeOptions();
            capability.AddAdditionalCapability("browser", "Chrome", true);
            capability.AddAdditionalCapability("browser_version", "latest", true);
            capability.AddAdditionalCapability("os", "Windows", true);
            capability.AddAdditionalCapability("os_version", "10", true);
            capability.AddAdditionalCapability("browserstack.user", "YOUR_USERNAME", true);
            capability.AddAdditionalCapability("browserstack.key", "YOUR_ACCESS_KEY", true);
            capability.AddAdditionalCapability("chromeOptions", chromeOptions, true);

            driver = new RemoteWebDriver(new Uri("http://hub-cloud.browserstack.com/wd/hub/"), capability);
            driver.Navigate().GoToUrl("https://web-push-book.gauntface.com/demos/notification-examples/");
            driver.FindElement(By.XPath("//body/main[1]/p[3]/input[1]")).Click();
            Thread.Sleep(2000);
            driver.Quit();
        }
    }
}

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
import time

# 0 - Default, 1 - Allow, 2 - Block
chrome_options = Options()
chrome_options.add_experimental_option("prefs", { "profile.default_content_setting_values.notifications": 1})
desired_cap = chrome_options.to_capabilities()
desired_cap.update({
  'browser_version': '75.0',
  'os': 'Windows',
  'os_version': '10'
})
driver = webdriver.Remote(command_executor='http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub.browserstack.com/wd/hub', desired_capabilities=desired_cap)
driver.get("https://web-push-book.gauntface.com/demos/notification-examples/")
driver.find_element_by_xpath("//body/main[1]/p[3]/input[1]").click()
time.sleep(2)
driver.quit()
require 'rubygems'
require 'selenium-webdriver'

# 0 - Default, 1 - Allow, 2 - Block
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"prefs" => { "profile.default_content_setting_values.notifications" => 1 } })
caps['browser'] = 'Chrome'
caps['browser_version'] = '75.0'
caps['os'] = 'Windows'
caps['os_version'] = '10'
driver = Selenium::WebDriver.for(:remote,
  :url => "http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
  :desired_capabilities => caps)
driver.navigate.to "https://web-push-book.gauntface.com/demos/notification-examples/"
driver.find_element(:xpath, "//body/main[1]/p[3]/input[1]").click;
sleep(2)
driver.quit

The following pop-up appears in mobile devices asking to allow showing notifications:

Web Push Notification Request

The snippet below lets you automate an Allow or Block interaction on Android devices when the remote Chrome browser asks for permission to show notifications.

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;

import java.net.URL;
import java.util.Set;

public class AllowNotificationPopupMobile {
  public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
  public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

  public static void main(String[] args) throws Exception {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("browser", "Chrome");
    caps.setCapability("browserName", "android");
    caps.setCapability("device", "Samsung Galaxy S10");
    caps.setCapability("realMobile", "true");
    caps.setCapability("os_version", "9.0");
    AppiumDriver driver = new AndroidDriver(new URL(URL), caps);

    driver.get("https://web-push-book.gauntface.com/demos/notification-examples/");
    driver.findElement(By.xpath("//body/main[1]/p[3]/input[1]")).click();
    Thread.sleep(2000);

    Set contextHandles = driver.getContextHandles();
    // 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");
    driver.findElement(By.xpath(".//android.widget.Button[@text='Allow']")).click();
    Thread.sleep(2000);
    driver.quit();
  }
}
let webdriver = require('wd');

let capabilities = {
    'os_version': '9.0',
    'device': 'Samsung Galaxy S10',
    'real_mobile': 'true',
    'browserstack.user': 'YOUR_USERNAME',
    'browserstack.key': 'YOUR_ACCESS_KEY'
}

let driver = webdriver.promiseRemote("http://hub-cloud.browserstack.com/wd/hub");
driver.init(capabilities, function() {
  driver.get('https://web-push-book.gauntface.com/demos/notification-examples/')
    .then(function() {
      (async () => {
        let element = await driver.element("xpath", "//body/main[1]/p[3]/input[1]");
        await element.click();

        // To accept/block the popup, you need to switch the context to “NATIVE_APP“ and click on the Allow/Block button.
        await driver.context('NATIVE_APP');
        let element2 = await driver.element("xpath", ".//android.widget.Button[@text='Allow']")
        await element2.click();
        await driver.quit();
      })();
    });
});
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System.Threading;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium;

namespace SampleCSharp
{
    class AllowNotificationPopup
    {
        static void Main(string[] args)
        {
            AppiumOptions capability = new AppiumOptions();
            capability.AddAdditionalCapability("device", "Samsung Galaxy S10");
            capability.AddAdditionalCapability("os_version", "9.0");
            capability.AddAdditionalCapability("real_mobile", "true");
            capability.AddAdditionalCapability("browserstack.debug", "true");
            capability.AddAdditionalCapability("browserstack.user", "YOUR_USERNAME");
            capability.AddAdditionalCapability("browserstack.key", "YOUR_ACCESS_KEY");
            AndroidDriver<IWebElement> driver = new AndroidDriver<IWebElement>(new Uri("http://hub-cloud.browserstack.com/wd/hub"), capability);
            driver.Navigate().GoToUrl("https://web-push-book.gauntface.com/demos/notification-examples/");
            IWebElement ll = driver.FindElement(By.XPath("//body/main[1]/p[3]/input[1]"));
            ll.Click();
            // 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='Allow']"));
            ll.Click();
            Thread.Sleep(2000);
            driver.Quit();
        }
    }
}
import time
from appium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_cap = {
  'device': 'Samsung Galaxy S10',
  'realMobile': 'true',
  'os_version': '9.0'
}
driver = webdriver.Remote(command_executor='http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub.browserstack.com/wd/hub', desired_capabilities=desired_cap)
driver.get("https://web-push-book.gauntface.com/demos/notification-examples/")
driver.find_element_by_xpath("//body/main[1]/p[3]/input[1]").click()
# To accept/block the popup, you need to switch the context to NATIVE_APP and click on the Allow/Block button.
driver.switch_to.context('NATIVE_APP')
driver.find_element_by_xpath(".//android.widget.Button[@text='Allow']").click()
time.sleep(2)
driver.quit()
require 'rubygems'
require 'appium_lib'

caps = {}
caps['device'] = 'Samsung Galaxy S10'
caps['platformName'] = 'Android'
caps['realMobile'] = 'true'
caps['os_version'] = '9.0'
appium_driver = Appium::Driver.new({
  'caps' => caps,
  'appium_lib' => {
      :server_url => "http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub"
  }}, true)
driver = appium_driver.start_driver
driver.navigate.to "https://web-push-book.gauntface.com/demos/notification-examples/"
driver.find_element(:xpath, "//body/main[1]/p[3]/input[1]").click;
# To accept/block the popup, you need to switch the context to “NATIVE_APP“ and click on the Allow/Block button.
driver.set_context('NATIVE_APP')
driver.find_element(:xpath, ".//android.widget.Button[@text='Allow']").click;
sleep(2)
driver.quit

Note: Web Push Notifications does not work in iOS devices.

Handle “Storage access permission” pop-up on Android devices

You can test what happens when your web app is allowed to (or blocked from) accessing device storage on Android devices, in Automate.

storage_access_chrome

The snippet below lets you automate an Allow or Block interaction on Android devices when the remote Chrome browser asks for permission to access device storage.

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;

import java.net.URL;

public class AllowFileDownloadPopupChromeMobile {
  public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
  public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

  public static void main(String[] args) throws Exception {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("browser", "Chrome");
    caps.setCapability("browserName", "android");
    caps.setCapability("device", "Samsung Galaxy S10");
    caps.setCapability("realMobile", "true");
    caps.setCapability("os_version", "9.0");

    AppiumDriver driver = new AndroidDriver(new URL(URL), caps);
    driver.get("https://the-internet.herokuapp.com/download");
    driver.findElement(By.linkText("some-file.txt")).click();
    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");
    driver.findElement(By.xpath(".//android.widget.Button[@text='Continue']")).click();
    driver.findElement(By.xpath(".//android.widget.Button[@text='Allow']")).click();
    Thread.sleep(2000);
    driver.quit();
  }
}
let webdriver = require('wd');

let capabilities = {
  'os_version': '9.0',
  'device': 'Samsung Galaxy S10',
  'real_mobile': 'true',
  'browserstack.user' : 'YOUR_USERNAME',
  'browserstack.key' : 'YOUR_ACCESS_KEY'
}

let driver = webdriver.promiseRemote("http://hub-cloud.browserstack.com/wd/hub");
driver.init(capabilities, function() {
  driver.get('https://the-internet.herokuapp.com/download')
    .then(function() {
      (async () => {
        let element = await driver.element("xpath", "//*[text()='some-file.txt']");
        await element.click();

        // To accept/block the popup, you need to switch the context to “NATIVE_APP“ and click on the Allow/Block button.
        await driver.context('NATIVE_APP');
        let element2 = await driver.element("xpath", ".//android.widget.Button[@text='Continue']")
        await element2.click();
        let element3 = await driver.element("xpath", ".//android.widget.Button[@text='Allow']")
        await element3.click();
        await driver.quit();
      })();
    });
});
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System.Threading;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium;

namespace SampleCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            AppiumOptions capability = new AppiumOptions();

            capability.AddAdditionalCapability("device", "Samsung Galaxy S10");
            capability.AddAdditionalCapability("os_version", "9.0");
            capability.AddAdditionalCapability("real_mobile", "true");
            capability.AddAdditionalCapability("browserstack.debug", "true");
            capability.AddAdditionalCapability("browserstack.user", "YOUR_USERNAME");
            capability.AddAdditionalCapability("browserstack.key", "YOUR_ACCESS_KEY");
            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");

            IWebElement ll = driver.FindElement(By.XPath("//*[text()='some-file.txt']"));
            ll.Click();
            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();
            ll = driver.FindElement(By.XPath(".//android.widget.Button[@text='Allow']"));
            ll.Click();
            Thread.Sleep(2000);
            driver.Quit();
        }
    }
}

import time
from appium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_cap = {
  'device': 'Samsung Galaxy S10',
  'realMobile': 'true',
  'os_version': '9.0'
}
driver = webdriver.Remote(command_executor='http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub.browserstack.com/wd/hub', desired_capabilities=desired_cap)
driver.get("https://the-internet.herokuapp.com/download")
driver.find_element_by_xpath("//*[text()='some-file.txt']").click()
time.sleep(2)
# To accept/block the popup, you need to switch the context to “NATIVE_APP“ and click on the Allow/Block button.
driver.switch_to.context('NATIVE_APP')
driver.find_element_by_xpath(".//android.widget.Button[@text='Continue']").click()
driver.find_element_by_xpath(".//android.widget.Button[@text='Allow']").click()
time.sleep(2)
driver.quit()
require 'rubygems'
require 'appium_lib'

caps = {}
caps['device'] = 'Samsung Galaxy S10'
caps['realMobile'] = 'true'
caps['os_version'] = '9.0'
appium_driver = Appium::Driver.new({
  'caps' => caps,
  'appium_lib' => {
      :server_url => "http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub"
  }}, true)
driver = appium_driver.start_driver
driver.navigate.to "https://the-internet.herokuapp.com/download"
driver.find_element(:xpath, "//*[text()='some-file.txt']").click;
sleep(2)
# To accept/block the popup, you need to switch the context to “NATIVE_APP“ and click on the Allow/Block button.
driver.set_context('NATIVE_APP')
driver.find_element(:xpath, ".//android.widget.Button[@text='Continue']").click;
driver.find_element(:xpath, ".//android.widget.Button[@text='Allow']").click;
sleep(2)
driver.quit

Automatically grant all app permissions for Android apps

The autoGrantPermissions is an Appium capability. When enabled, your Android app gets all the permissions listed in the manifest file of the APK, upon install.

The sample below shows how to enable autoGrantPermissions capability for your test:

caps.setCapability("autoGrantPermissions", "true");
let capabilities = {
    'autoGrantPermissions': 'true'
}
capability.AddAdditionalCapability("autoGrantPermissions", "true");
desired_cap = {
  'autoGrantPermissions' = 'true',
}
caps['autoAcceptAlerts'] = 'true'

Allow / Deny all app permissions for iOS apps

autoAcceptAlerts and autoDismissAlerts are Appium capabilities. When enabled, the iOS device accepts (or rejects) all access requests, upon install. This includes location, contacts, photos, and more.

Note: These capabilities do not work on XCUITest scripts.

The sample below shows how to enable autoAcceptAlerts or autoDissmissAlerts capabilities for your test:

caps.setCapability("autoAcceptAlerts", "true");
//OR
caps.setCapability("autoDissmissAlerts", "true");
let capabilities = {
    'autoAcceptAlerts': 'true'
}
//OR
let capabilities = {
    'autoDissmissAlerts': 'true'
}
capability.AddAdditionalCapability("autoAcceptAlerts", "true");
//OR
capability.AddAdditionalCapability("autoDissmissAlerts", "true");
desired_cap = {
  'autoAcceptAlerts': 'true',
}
#OR
desired_cap = {
  'autoDissmissAlerts': 'true'
}
caps['autoAcceptAlerts'] = 'true'
#OR
caps['autoDissmissAlerts'] = 'true'

Allow / Deny any app permission pop-up on Android and iOS devices

For more selective test scenarios, you would want to accept some app permission pop-ups and deny the others. To automate these interactions, you can switch to “NATIVE_APP” context to accept or deny each pop-up on iOS and Android devices.

driver.context("NATIVE_APP");
//For Android
driver.findElement(By.xpath(".//android.widget.Button[@text='Allow']")).click();
//For iOS
driver.findElement(By.id("Allow")).click();
await driver.context('NATIVE_APP');
//For Android
let element = await driver.element("xpath", ".//android.widget.Button[@text='Continue']")
await element.click();
//For iOS
let element = await driver.element("id", "Allow")
await element.click();
driver.Context = ("NATIVE_APP");
//For Android
IWebElement ll =  driver.FindElement(By.XPath(".//android.widget.Button[@text='Continue']"));
ll.Click();
//For iOS
IWebElement ll =  driver.FindElement(By.Id("Allow"));
ll.Click();
driver.switch_to.context('NATIVE_APP')
#For Android
driver.find_element_by_xpath(".//android.widget.Button[@text='Allow']").click()
#For iOS
driver.find_element_by_id("Allow").click()
driver.set_context('NATIVE_APP')
#For Android
driver.find_element(:xpath, ".//android.widget.Button[@text='Allow']").click;
#For iOS
driver.find_element(:id, "Allow").click;
Warning: Disabling the “Save your password” message doesn’t work on the Edge browser.
There is a known issue in Edge browser which prevents you from either interacting or disabling the ‘Save Your Password’ pop-up. Refer to the GitHub issue link for more details.

We're sorry to hear that. Please share your feedback so we can do better







Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better







Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback!

Talk to an Expert
Talk to an Expert