How to handle browser level notification using Selenium Webdriver

前端 未结 6 2132
Happy的楠姐
Happy的楠姐 2020-12-17 22:03

I am Automating some test cases using Selenium Webdriver and core Java,in chrome browser for one test case on clicking button I am getting browser level notification \'Show

相关标签:
6条回答
  • 2020-12-17 22:24

    You can use below code to allow chrome to send notifications:

    ChromeOptions options=new ChromeOptions();
    Map<String, Object> prefs=new HashMap<String,Object>();
    prefs.put("profile.default_content_setting_values.notifications", 1);
    //1-Allow, 2-Block, 0-default
    options.setExperimentalOption("prefs",prefs);
    ChromeDriver driver=new ChromeDriver(options);
    

    It works perfectly for me. Let me know if it doesn't work for you. Cheers!!

    0 讨论(0)
  • 2020-12-17 22:35

    update: Please use incognito mode to avoid such browser-level notifications and cookies issue.

    0 讨论(0)
  • 2020-12-17 22:41

    Please Follow below steps :

    A) USING JAVA :

    For Old Chrome Version (<50):

    //Create a instance of ChromeOptions class
    ChromeOptions options = new ChromeOptions();
    
    //Add chrome switch to disable notification - "**--disable-notifications**"
    options.addArguments("--disable-notifications");
    
    //Set path for driver exe 
    System.setProperty("webdriver.chrome.driver","path/to/driver/exe");
    
    //Pass ChromeOptions instance to ChromeDriver Constructor
    WebDriver driver =new ChromeDriver(options);
    

    For New Chrome Version (>50):

    //Create a map to store  preferences 
    Map<String, Object> prefs = new HashMap<String, Object>();
    
    //add key and value to map as follow to switch off browser notification
    //Pass the argument 1 to allow and 2 to block
    prefs.put("profile.default_content_setting_values.notifications", 2);
    
    //Create an instance of ChromeOptions 
    ChromeOptions options = new ChromeOptions();
    
    // set ExperimentalOption - prefs 
    options.setExperimentalOption("prefs", prefs);
    
    //Now Pass ChromeOptions instance to ChromeDriver Constructor to initialize chrome driver which will switch off this browser notification on the chrome browser
    WebDriver driver = new ChromeDriver(options);
    

    For Firefox :

        WebDriver driver ;
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("permissions.default.desktop-notification", 1);
        DesiredCapabilities capabilities=DesiredCapabilities.firefox();
        capabilities.setCapability(FirefoxDriver.PROFILE, profile);
        driver = new FirefoxDriver(capabilities);
        driver.get("http://google.com");
    

    B) USING PYTHON :

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    option = Options()
    
    option.add_argument("--disable-infobars")
    option.add_argument("start-maximized")
    option.add_argument("--disable-extensions")
    
    # Pass the argument 1 to allow and 2 to block
    option.add_experimental_option("prefs", { 
        "profile.default_content_setting_values.notifications": 1 
    })
    
    driver = webdriver.Chrome(chrome_options=option, executable_path='path-of- 
    driver\chromedriver.exe')
    driver.get('https://www.facebook.com')
    

    C) USING C#:

    ChromeOptions options = new ChromeOptions();
    options.AddArguments("--disable-notifications"); // to disable notification
    IWebDriver driver = new ChromeDriver(options);
    
    0 讨论(0)
  • 2020-12-17 22:47

    As per my understanding,this pop-ups not javascripts one.So selenium won't handle them. Same kinda,authentication popup also there , which we automate via a hack like http://username:password@www.test.com , here giving user name and password in url itself. In your case the way Praveen mentioned you have to create browser profile (FF or chrome ).In chrome, profile can be create via desired cabilities options or chrome options function.

    0 讨论(0)
  • 2020-12-17 22:48

    Steps to handle this type of requirements:

    1. Open Firefox Profile manager (Go to Start Menu, Type firefox.exe -p to launch profile manager)
    2. Create a new Profile (Ex. name of Profile is customfirefox)
    3. Navigate to about:permissions
    4. Do the required configurations and Save the Profile

    Now use the newly created Profile to run your test by invoking the Firefox

    ProfilesIni ffProfiles = new ProfilesIni();
    FirefoxProfile profile = ffProfiles.getProfile("customfirefox");
    WebDriver driver = new FirefoxDriver(profile);
    

    This will always use the firefox with the custom configurations saved on the profile.

    Hope this works for you..!!!

    Good luck.

    0 讨论(0)
  • 2020-12-17 22:49
    WebDriver driver ;
            FirefoxProfile profile = new FirefoxProfile();
            profile.setPreference("permissions.default.desktop-notification", 1);
            DesiredCapabilities capabilities=DesiredCapabilities.firefox();
            capabilities.setCapability(FirefoxDriver.PROFILE, profile);
            driver = new FirefoxDriver(capabilities);
            driver.get("your Web site"); 
    
    0 讨论(0)
提交回复
热议问题