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 notifications with options Allow and Block'. I want to select Allow option. Can anyone know how to handle this kind of notifications using Selenium webdriver. please refer following snapshot for more details
问题:
回答1:
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");
回答2:
Steps to handle this type of requirements:
- Open Firefox Profile manager (Go to Start Menu, Type
firefox.exe -p
to launch profile manager) - Create a new Profile (Ex. name of Profile is
customfirefox
) - Navigate to
about:permissions
- 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.
回答3:
Please Follow below steps :
Step 1:
//Create a instance of ChromeOptions class
ChromeOptions options = new ChromeOptions();
Step 2:
//Add chrome switch to disable notification - "--disable-notifications"
options.addArguments("--disable-notifications");
Step 3:
//Set path for driver exe
System.setProperty("webdriver.chrome.driver","path/to/driver/exe");
Step 4 :
//Pass ChromeOptions instance to ChromeDriver Constructor
WebDriver driver =new ChromeDriver(options);
回答4:
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!!
回答5:
This is a Chrome browser alert
WebDriverWait wait = new WebDriverWait(driver, 2) \\ wait for alert to pop up;
Alert alert = wait.until(ExpectedConditions.alertIsPresent()) \\ Check for alert;
alert.accept(); \\ accept it- In your case it allows notification
This works well in Chrome Note: If you use Incognito mode- you should not get such notification alerts.
回答6:
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.