Skip to main content
Experience faster, smarter testing with BrowserStack AI Agents. See what your workflow’s been missing. Explore now!
No Result Found
Get your setup working faster. Join our Discord for optimisation tips from elite testers. Join our DiscordJoin our Discord

Handle pop-ups, permissions, and notifications

Learn how to manage pop-ups, permissions, and notifications in different browsers when running your Selenium tests on BrowserStack Automate.

When testing any web application, you might encounter certain advertisement pop-ups, notifications, or permission pop-ups for camera or microphones. This guide provides code samples and solutions to handle such pop-ups when testing your application.

Some of the most common pop-ups seen in both desktop and mobile devices are as follows -

Type of pop-up Description Reference Β  Β 
Web pop-ups These are pop-ups that appear as a separate browser window. Web pop-ups Β  Β 
JS Alerts These alerts are native browser pop-ups and are classified as an alert, a confirm alert, or a prompt alert. Check out Selenium documentation to learn more about these alerts. JS alerts Β  Β 
Permission pop-ups These alerts are raised by some applications that might need permission to access native device features, such as camera or microphone. Permission pop-ups Β  Β 
Location pop-ups These alerts are raised by applications that want to show their customers region-specific offers, currency, or other information. Location pop-ups Β  Β 
Notification pop-ups These alerts are raised by applications for permission to push notifications to the foreground of the user’s screen. Notification pop-ups Β  Β 

In this guide, you will also learn how to:

Web pop-ups

The web pop-ups appear as a separate window when accessing a website as shown in the following image. Apart from closing the pop-up, sometimes, these pop-ups require you to click a button as acceptance optionally. These workflows are mostly related to advertising, subscription, opt-in, etc. You can handle such pop-ups, as explained in this section.

Browser Location Access Popup

The default pop-up behavior in different browsers on desktop devices is as follows:

Browser Pop-ups enabled by default Β  Β  Β 
Chrome Yes Β  Β  Β 
Safari No Β  Β  Β 
Edge Yes Β  Β  Β 
Firefox Yes Β  Β  Β 

You can handle most pop-ups that appear in mobile devices using NATIVE_APP context. Check out how to handle pop-ups in native context to learn about managing web pop-ups in mobile devices.

Enable all pop-ups in Safari or IE for desktop devices

If you are using BrowserStack SDK, you can set the capabilities covered in this document within the configuration file. To test if the pop-ups appear in Safari or Edge, you can enable pop-ups as follows:

To enable the pop-ups in Edge, set the enablePopups capability to true.

browserstack.yml
Copy icon Copy

Disable all pop-ups in Chrome, Safari, and Edge for desktop devices

Use the sample code snippets to disable pop-ups in the following browsers.

To disable the pop-ups in Chrome, set the enablePopups capability to false.

browserstack.yml
Copy icon Copy

Know your location pop-up

Use the sample code snippets to disable the know your location pop-up in the following devices.

If your web application requests access to your location, the following pop-up appears on 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 requests the 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", "128.0");
    caps.setCapability("os", "Windows");
    caps.setCapability("os_version", "11");

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

Camera and microphone pop-ups

Use the sample code snippets to disable the camera and microphone pop-ups on the following devices.

When a web application requires access to the camera and microphone, the following pop-ups appear on the desktop:

use camera android Microphone Permission popup

The snippet below lets you automate an Allow or Block interaction when your web app requests access to a 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", "128.0");
    caps.setCapability("os", "Windows");
    caps.setCapability("os_version", "11");
    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();
  }
}

Show notifications pop-ups

Use the sample code snippets to disable the notification pop-up in the following devices.

When your web application requests permission to show notifications, the following pop-up appears on desktop devices:

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", "128.0");
    caps.setCapability("os", "Windows");
    caps.setCapability("os_version", "11");

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

JS alerts

Use the sample code snippets to learn how to disable the most common JS alerts on mobile or desktop devices for any browser. The following image shows the types of JS alerts handled by the sample code snippet. JS Alerts

const { ok } = require('assert');
const webdriver = require('selenium-webdriver'); 

// Input capabilities
const capabilities = {
    'browserName': 'Chrome',
    'os': 'Windows',
    'os_version': '11',
    'browserstack.debug': 'true',
    'build': 'JS alerts pop-ups in Browsers ',
    'name': 'JS alerts popups disabled in Chrome',
}
async function runTestWithCaps () {
  let driver = new webdriver.Builder().usingServer('https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub').withCapabilities(capabilities).build();
  await driver.get("https://the-internet.herokuapp.com/javascript_alerts");
  //   Simple JS alert to confirm.
  const element = await driver.wait(webdriver.until.elementLocated(webdriver.By.css('#content > div > ul > li:nth-child(2) > button')), 5 * 1000);
  await element.click()
  await driver.wait(webdriver.until.alertIsPresent());
  const alertWindow = await driver.switchTo().alert();
  await alertWindow.accept();

  //   Simple JS alert to accept ok.
//   const element = await driver.wait(webdriver.until.elementLocated(webdriver.By.css('#content > div > ul > li:nth-child(1) > button')), 5 * 1000);
//   await element.click()
//   await driver.wait(webdriver.until.alertIsPresent());
//   const alertWindow = await driver.switchTo().alert();
//   await alertWindow.accept();

  //   Simple JS alert to enter data and accept ok.
//   const element = await driver.wait(webdriver.until.elementLocated(webdriver.By.css('#content > div > ul > li:nth-child(3) > button')), 5 * 1000);
//   await element.click()
//   await driver.wait(webdriver.until.alertIsPresent());
//   const alertWindow = await driver.switchTo().alert();
//   await alertWindow.sendKeys('Selenium');
//   await alertWindow.accept();

  await driver.quit();
}
runTestWithCaps(); 

Handle switching tabs in iOS devices

When testing applications that require clicking an element that opens in a new tab, you need to set the safariAllowPopups and autoAcceptAlerts capabilities to true, and then click the required element.

const webdriver = require('selenium-webdriver')

const desiredCaps = {
  'os_version': '14',
  'browserName': 'iPhone',
  'device': 'iPhone 15',
  'real_mobile': 'true',
  'browserstack.debug': 'true',
  'build': 'JS alerts pop-ups in Browsers',
  'name': 'JS alerts popups disabled in Chrome',
  'browserstack.appium_version': '1.18.0',
  'autoAcceptAlerts': 'true',
  'safariAllowPopups' : 'true'
}

const runSampleTest = async () => {
  let driver
  try {
    driver = new webdriver.Builder().
      usingServer('https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub').
      withCapabilities(desiredCaps).
      build()

    await driver.get('https://demoqa.com/browser-windows')
    const elem = await driver.findElement(webdriver.By.xpath('//*[@id="windowButton"]'))
    await elem.click()

  } catch (e) {
    console.log(e)
  } finally {
    if(driver) {
      await driver.quit()
    }
  }
}

runSampleTest()

Handle any app permission pop-up using Native context

When you test any web application on iOS or Android devices, the permission pop-ups appear in the form of Allow/Block, Continue/Deny, etc options. To handle such pop-ups, irrespective of the options presented, you can switch to NATIVE_APP context and use locators (by xpath, id, or css) to either allow or block the pop-ups.

When handling permission pop-ups in iOS devices, ensure that the safariAllowPopups capability is set to true before you add any logic to click an element to the script.

driver.context("NATIVE_APP");
//For Android
driver.findElement(By.xpath(".//android.widget.Button[@text='Allow']")).click();
//For iOS
caps.setCapability("safariAllowPopups", true)
driver.findElement(By.id("Allow")).click();
//Change context back to the main page.
driver.context(β€œWEBVIEW”);

Disabling the β€œSave your password” message doesn’t work on the Edge browser.

Edge browser has a known issue that prevents you from either interacting or disabling the Save Your Password pop-up. Check out the GitHub issue link for more details.

Need some help?

If you encounter any pop-up that is not listed on this page, get in touch with Support with your session ID so we can assist you further.

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

Test web and mobile, from manual to automation, on real devices in one unified platform.

Contact Us

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
Download Copy Check Circle