msch msch - 1 year ago 97
Node.js Question

deactivate browser notifications in chrome driver for selenium

I have a simple user login, which shows a notification ( or an alert (if notifications were disabled), if the login fails.

Furthermore I want to test this login with selenium in NodeJS, but without the notifications (only alerts). So I tried to deactivate the notification feature in chrome using the chrome experimental options. Nevertheless it does show the notification. Perhaps this feature isn't supported atm.

I've tried the following in NodeJS:

var options = new chrome.Options();
options.addArguments("profile.default_content_setting_values.notifications", "2");
var driver = new webdriver.Builder().withCapabilities(options.toCapabilities()).build();

The following code works in Java.

Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.notifications", 2);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);

Answer Source

I had to set the userPreferences.

var options = new chrome.Options();
options.setUserPreferences({'profile.default_content_setting_values.notifications': 2});
var driver = new webdriver.Builder().withCapabilities(options.toCapabilities()).build();