Friday, September 4, 2020

How to handle Camera, Mic & Show notification permission request Pop-ups | Selenium Automation

How to handle Camera, Mic & Show notification permission request Pop-ups | Selenium Automation

 

In this post will learn how to handle camera permission pop-up, mic permission pop up & show notification popup in your browser using selenium.

 


How to handle camera Permission pop-up & Mic permission pop up :


When browser try to take picture or use camera for video call, it will ask you to give permission. These pop-ups are different than user message pop-up displays regularly and we cannot handle these by switching. We need to provide do settings in browser profile to handle it.

 

Let’s see some examples for camera & Mic permission request Pop-ups:


Camera permission request popup



How to handle Mic permission request popup using selenium


The below full code, lets you automate to allow mic and camera permission request:

 

package practise;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;

 

public class CameraMicrophonePermission {

    

     public static WebDriver driver;

 

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

         

           ChromeOptions option = new ChromeOptions();

           option.addArguments("use-fake-device-for-media-stream"); // this argument for accepting permissions

           option.addArguments("use-fake-ui-for-media-stream");    // this argument for accepting permissions

         driver = new ChromeDriver(option);

        

        //WebCam Permission Test

         driver.get("https://webcamtests.com/check");

         Thread.sleep(5000);

         driver.findElement(By.id("webcam-launcher")).click();

         Thread.sleep(2000);

 

         //Mic permission test

         driver.get("https://www.vidyard.com/mic-test/");

         Thread.sleep(2000);

         driver.findElement(By.xpath("(//a[@id='start-test'])[1]")).click();

         Thread.sleep(2000);

 

         driver.quit();

 

     }

 

}


 

Handling web Show Notification popup:

 

Many times we come across handling permission request for showing notifications on browser during our automation scripts:


Show notification permission request popup


 

Using “managed_default_content_settings” we can handle it:

 

We need to set “notifications” as 1 to “Allow

                 set “notifications” as 1 to “Block

                 set “notifications” as 1 to “Default

 

       

  // SET CHROME OPTIONS

  // 0 - Default, 1 - Allow, 2 - Block

  contentSettings.put("notifications", 1);

  profile.put("managed_default_content_settings", contentSettings);

 

 

The below full code, lets you automate an ‘Allow’ or ‘Block’ interaction when your web app requests permission to show notifications:

 


package practise;

 

import java.util.HashMap;

import java.util.Map;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;

 

public class WebNotificationExample

{

    

     public static WebDriver driver;

 

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

         

 

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

         driver = new ChromeDriver(options);

    

         driver.get("https://web-push-book.gauntface.com/demos/notification-examples/");

         driver.findElement(By.xpath("//input[@class='c-toggle js-example-toggle']")).click();

         Thread.sleep(5000);

         driver.quit();

 

     }

 

}


Do let me know if any issues/feedback in the comment section. 

 

 

No comments:

Post a Comment

If any suggestions or issue, please provide