How to click Allow on Show Notifications popup using Selenium Webdriver - selenium

I'm trying to login to Facebook. After a successful login, I get a browser popup:
How with the webdriver can I click Allow and proceed forward?

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

import unittest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import time
class SendMsgSkype(unittest.TestCase):
#classmethod
def setUpClass(cls):
options = Options()
options.add_argument("--disable-notifications")
cls.driver = webdriver.Chrome("./chromedriver.exe", chrome_options=options)
cls.driver.implicitly_wait(5)
cls.driver.maximize_window()
cls.driver.get("https://web.skype.com/ru/")
It works for me.
More details here: http://nullege.com/codes/show/src#t#a#TardyParty-HEAD#oxbiz.py/21/selenium.webdriver.Chrome

This not an alert box, so you can't handle it using Alert, this is a chrome browser notification, To Switch off this browser notification you need to create chrome preference map with chrome option as below :
//Create prefs map to store all preferences
Map<String, Object> prefs = new HashMap<String, Object>();
//Put this into prefs map to switch off browser notification
prefs.put("profile.default_content_setting_values.notifications", 2);
//Create chrome options to set this prefs
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
//Now initialize chrome driver with chrome options which will switch off this browser notification on the chrome browser
WebDriver driver = new ChromeDriver(options);
//Now do your further steps
Hope it helps..:)

The one and only working solution I've come across so far is this:
from selenium.webdriver.chrome.options import Options
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)

no answer has been accepted yet, this following code works for me
ruby, rspec, capybara
Capybara.register_driver :selenium_chrome do |app|
prefs = {"profile.managed_default_content_settings.notifications" => 2,}
caps = Selenium::WebDriver::Remote::Capabilities.chrome(chrome_options: { prefs: prefs })
Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: caps)
end
Capybara.javascript_driver = :selenium_chrome

try {
// Check the presence of alert
Alert alert = driver.SwitchTo().Alert();
// if present consume the alert
alert.Accept();
} catch (NoAlertPresentException ex) {
//code to do if not exist.
}

Facebook authentication window displays an overlay that covers the continue as [username] button.
This makes the continue button un-clickable. To circumvent that problem, you can hide those layers programmatically using JavaScript (not recommended) using this code (don't do this).
// DO NOT USE THIS CODE.
function forceClickSetup(targetSelector) {
return browser.selectorExecute("div",
function(divs, targetSelector) {
var button = document.querySelector(targetSelector);
for(var i = 0; i < divs.length; i++) {
if(!divs[i].contains(button)) {
divs[i].remove();
}
}
return i;
}, targetSelector);
}
Or instead, you can dismiss the notifications dialog, after which facebook will uncover the continue button. But before wildly hitting Escape at the browser, first make sure that the continue button has been shown.
// USE THIS CODE.
browser.waitForVisible("[name=__CONFIRM__]");
browser.keys("Escape"); // Dismiss "notifications" dialog box.
var confirmButtonSelector = "[name=__CONFIRM__]";
This solution is really Matthijs' (see comments above)

if you play with Ruby and Capybara try this code
Capybara.register_driver :chrome_no_image_popup_maximize do |app|
# 2: disable, other than 2 means enable it
preferences = {
"profile.managed_default_content_settings.notifications" => 2,
"profile.managed_default_content_settings.images" => 2,
"profile.managed_default_content_settings.popups" => 2
}
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
'chromeOptions' => {
'prefs' => preferences,
}
)
args = [ "--start-maximized" ]
Capybara::Selenium::Driver.new(app, {:browser => :chrome, :desired_capabilities => caps, :args => args})
end
Capybara.default_driver = :chrome_no_image_popup_maximize
Capybara.javascript_driver = :chrome_no_image_popup_maximize

Related

How to test Electron app using selenium and java

Hi Im having an issue with testing an electron app. Up until last week our product was ran on chrome. But now the product has been changed to an electron desktop app and when launched the window isnt picked up.
The flow is basically I open the product on chrome and it appears as a pop up. Previously this was just a chrome pop up but now its an electron app. And now i cnat seem to switch to this window. Im wondering is it possible to switch between the two or do i need a different driver and just test he electron app by itself?
My driver factory is shown here
public class DriverFactory {
private static WebDriver driver;
public static WebDriver startDriver() {
String projectLocation = System.getProperty("user.dir");
// add in elements for logging into the mobile application also - Android and
// iOS.
if (OSValidator.isMac()) {
System.setProperty("webdriver.chrome.driver", projectLocation + "/chromedriver_mac");
} else if (OSValidator.isWindows()) {
System.setProperty("webdriver.chrome.driver", projectLocation + "/chromedriver.exe");
} else {
System.setProperty("webdriver.chrome.driver", projectLocation + "/chromedriver_linux");
}
if (System.getProperty("app.env") != null) { // If coming from Jenkins/Maven goal..
// This is for logging results. Added when investigating crashes on chrome driver. Can be disabled when not needed. 26/03/2020
System.setProperty("webdriver.chrome.verboseLogging", "true");
}
unknown-error-devtoolsactiveport-file-doesnt-exist-while-t
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
options.addArguments("--window-size=1920x1080");
options.addArguments("--disable-cache");
//options.addArguments("--headless");
options.addArguments("--disable-application-cache");
options.addArguments("--disk-cache-size=0");
options.addArguments("--disable-gpu"); // applicable to windows os only
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("--dns-prefetch-disable");
//options.addArguments("--no-sandbox"); // Bypass OS security model
options.setPageLoadStrategy(PageLoadStrategy.NORMAL);
driver = new ChromeDriver(options);
//--------------------
return driver;
}
}
It is described here.
https://applitools.com/blog/automating-electron-applications-using-selenium/
You just need to set appropriate options and use same code for the chrome and electron.
#Before
public void setup() {
ChromeOptions opt = new ChromeOptions();
opt.setBinary("/Users/yanir/Downloads/Electron API Demos.app/Contents/MacOS/Electron API Demos");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("chromeOptions", opt);
capabilities.setBrowserName("chrome");
driver = new ChromeDriver(capabilities);
if (driver.findElements(By.id("button-about")).size() > 0)
driver.findElement(By.id("button-about")).click();
}

Unable to create Headless Chrome Instance with ChromeDriver and NodeJS

I am using NodeJS Selenium, Mocha and Chai to run test automation. I have spent several hours trying to get Chrome to run "headless" I use the function below to create a driver instance. When Chrome opens it is neither headless not maximized. Tested on both Windows with Chrome 76 and OSX with Chrome 74.
export async function getDriver() {
let options = new chrome.Options();
options.addArguments("--window-size=1024,768");
options.addArguments("--disable-gpu");
options.addArguments("--disable-extensions");
options.addArguments("--start-maximized");
options.addArguments("--headless");
var driver = new Builder()
.forBrowser('chrome')
.setChromeOptions(options)
.build();
return driver;
}
do you try
const options = new chromeDriver.Options();
options.setChromeBinaryPath(CHROME_BIN_PATH);
options.addArguments( 'headless', 'disable-gpu');
or just
let chromeOptions = {
'args': ['--headless', '--disable-gpu'] };
chromeCapabilities.set('chromeOptions', chromeOptions);
const driver = new webdriver.Builder().withCapabilities(chromeCapabilities).build()

Unable to handle download file in Chrome using selenium webdriver

In the AUT when user clicks on the Excel button then a file should be downloaded.
In the past in other web applications I have handled it using the chrome options as:
But in my current application I see that when user clicks on Excel button then a new tab is open which presents the file explorer pop up. The question here is how to handle this. Even if I switch window handles still I will have the pop up which I don't want. How can I handle this situation.
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", download_dir);
chromeOptions.setExperimentalOption("prefs", chromePrefs);
This is the (python) code that I've used in the past to download files
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("prefs", {
"download.default_directory": r"C:\Users\xxx\downloads\Test",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
(obviously this is in python, I can take a stab at making a version for Java or you can try if you like)
I developed a library which is dealing more clearly in such case. You can generate a ChromeOptions object with given download folder and use a one line method call to download a file and verify succession:
private SeleniumDownloadKPI seleniumDownloadKPI;
#BeforeEach
void setUpTest() {
seleniumDownloadKPI =
new SeleniumDownloadKPI("/tmp/downloads");
ChromeOptions chromeOptions =
seleniumDownloadKPI.generateDownloadFolderCapability();
driver = new ChromeDriver(chromeOptions);
}
#Test
void downloadAttachTest() throws InterruptedException {
adamInternetPage.navigateToPage(driver);
seleniumDownloadKPI.fileDownloadKPI(
adamInternetPage.getFileDownloadLink(), "SpeedTest_16MB.dat");
waitBeforeClosingBrowser();
}

Selenium Disable “Disable Developer Mode Extensions” Popup While adding extension

I want to disable the "Disable Developer Mode Extensions" popup while keeping an extension as mentioned in this post keep "Disable developer mode" closed while adding extension
There was no answer and I'm trying to do the same thing. Is there a chrome option like this available for Selenium?
import pywinauto
window_title = "Disable Developer Mode Extensions"
app = pywinauto.Application().connect(name_re=window_title)
win_ext = app.window(name=window_title)
win_ext.close()
Here is the Answer to your Question:
While working with Selenium 3.4.0, chromedriver v2.30, Google Chrome 59.0 through Java bindings, you can use the ChromeOption class to disable the "Disable Developer Mode Extensions" popup as follows:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Q44959944_dev_extn {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.navigate().to("https://google.com");
}
}
Let me know if this Answers your Question.
The solution provided by DebanjanB doesn't work with the latest version of Chrome and Chrome Driver.
To get this to work you need to specify the exclude switches and useAutomationExtension in the prefs flag.
System.setProperty("webdriver.chrome.driver", Constant.BROWSER_CHROME_PATH);
Map prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.geolocation", 1); // 1:allow 2:block
prefs.put("useAutomationExtension", false);
prefs.put("excludeSwitches", Collections.singletonList("enable-automation"));
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("-incognito");
chromeOptions.addArguments("--disable-gpu"); // applicable to windows os only
chromeOptions.setExperimentalOption("prefs", prefs);
chromeOptions.addArguments("--no-sandbox");
wDriver = new ChromeDriver(chromeOptions);
((LocationContext)wDriver).setLocation(new Location(37.774929, -122.419416, 0));
wDrivers.put("chrome", wDriver);
log.info("New Chrome Browser Instance Created.");

How do I enable geolocation support in chromedriver?

I need to test a JS geolocation functionality with Selenium and I am using chromedriver to run the test on the latest Chrome.
The problem is now that Chrome prompts me to enable Geolocation during the test and that I don't know how to click that little bar on runtime, so I am desperately looking for a way to start the chromedriver and chrome with some option or trigger to enable this by default. All I could find here was however how I can disable geolocation altogether.
How can I solve this issue?
In the known issues section of the chromedriver wiki they said that you Cannot specify a custom profile
This is why it seems to me that #Sotomajor answer about using profile with Chrome as you would do with firefox will not work.
In one of my integration tests I faced the same issue. But because I was not bothering about the real geolocation values, all I had to do is to mock window.navigator.gelocation
In you java test code put this workaround to avoid Chrome geoloc permission info bar.
chromeDriver.executeScript("window.navigator.geolocation.getCurrentPosition = function(success) {
var position = {
"coords": {
"latitude": "555",
"longitude": "999"
}
};
success(position);
}");
latitude (555) and longitude (999) values here are just test value
Approach which worked for me in Firefox was to visit that site manually first, give those permissions and afterwards copy firefox profile somewhere outside and create selenium firefox instance with that profile.
So:
cp -r ~/Library/Application\ Support/Firefox/Profiles/tp3khne7.default /tmp/ff.profile
Creating FF instance:
FirefoxProfile firefoxProfile = new FirefoxProfile(new File("/tmp/ff.profile"));
FirefoxDriver driver = new FirefoxDriver(firefoxProfile);
I'm pretty sure that something similar should be applicable to Chrome. Although api of profile loading is a bit different. You can check it here: http://code.google.com/p/selenium/wiki/ChromeDriver
Here is how I did it with capybara for cucumber tests
Capybara.register_driver :selenium2 do |app|
profile = Selenium::WebDriver::Chrome::Profile.new
profile['geolocation.default_content_setting'] = 1
config = { :browser => :chrome, :profile => profile }
Capybara::Selenium::Driver.new(app, config)
end
And there is link to other usefull profile settings: pref_names.cc
Take a look at "Tweaking profile preferences" in RubyBindings
As for your initial question:
You should start Firefox manually once - and select the profile you use for Selenium.
Type about:permissions in the address line; find the name of your host - and select share location : "allow".
That's all. Now your Selenium test cases will not see that dreaded browser dialog which is not in the DOM.
I took your method but it can not working. I did not find "BaseUrls.Root.AbsoluteUri" in Chrome's Preferences config. And use a script for test
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("proxy-server=http://127.0.0.1:1087")
prefs = {
'profile.default_content_setting_values':
{
'notifications': 1,
'geolocation': 1
},
'devtools.preferences': {
'emulation.geolocationOverride': "\"11.111698#-122.222954:\"",
},
'profile.content_settings.exceptions.geolocation':{
'BaseUrls.Root.AbsoluteUri': {
'last_modified': '13160237885099795',
'setting': '1'
}
},
'profile.geolocation.default_content_setting': 1
}
chromeOptions.add_experimental_option('prefs', prefs)
chromedriver_path = os.path.join(BASE_PATH, 'utils/driver/chromedriver')
log_path = os.path.join(BASE_PATH, 'utils/driver/test.log')
self.driver = webdriver.Chrome(executable_path=chromedriver_path, chrome_options=chromeOptions, service_log_path=log_path)
We just found a different approach that allows us to enable geolocation on chrome (currently 65.0.3325.181 (Build officiel) (64 bits)) without mocking the native javascript function.
The idea is to authorize the current site (represented by BaseUrls.Root.AbsoluteUri) to access to geolocation information.
public static void UseChromeDriver(string lang = null)
{
var options = new ChromeOptions();
options.AddArguments(
"--disable-plugins",
"--no-experiments",
"--disk-cache-dir=null");
var geolocationPref = new JObject(
new JProperty(
BaseUrls.Root.AbsoluteUri,
new JObject(
new JProperty("last_modified", "13160237885099795"),
new JProperty("setting", "1")
)
)
);
options.AddUserProfilePreference(
"content_settings.exceptions.geolocation",
geolocationPref);
WebDriver = UseDriver<ChromeDriver>(options);
}
private static TWebDriver UseDriver<TWebDriver>(DriverOptions aDriverOptions)
where TWebDriver : RemoteWebDriver
{
Guard.RequiresNotNull(aDriverOptions, nameof(UITestsContext), nameof(aDriverOptions));
var webDriver = (TWebDriver)Activator.CreateInstance(typeof(TWebDriver), aDriverOptions);
Guard.EnsuresNotNull(webDriver, nameof(UITestsContext), nameof(WebDriver));
webDriver.Manage().Window.Maximize();
webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
webDriver.NavigateToHome();
return webDriver;
}
ChromeOptions options = new ChromeOptions();
HashMap<String, Integer> contentSettings = new HashMap<String, Integer>();
HashMap<String, Object> profile = new HashMap<String, Object>();
HashMap<String, Object> prefs = new HashMap<String, Object>();
contentSettings.put("geolocation", 1);
contentSettings.put("notifications", 2);
profile.put("managed_default_content_settings", contentSettings);
prefs.put("profile", profile);
options.setExperimentalOption("prefs", prefs);
The simplest to set geoLocation is to just naviaget on that url and click on allow location by selenium. Here is the code for refrence
driver.navigate().to("chrome://settings/content");
driver.switchTo().frame("settings");
WebElement location= driver.findElement(By.xpath("//*[#name='location' and #value='allow']"));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
((JavascriptExecutor) driver).executeScript("arguments[0].click();", location);
WebElement done= driver.findElement(By.xpath(""));
driver.findElement(By.xpath("//*[#id='content-settings-overlay-confirm']")).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.navigate().to("url");
Cypress:
I encountered with same problem but mocking was not worked for me.
My case:
Application use geo location for the login purpose. So with mock it not worked for me.
Solution:
I used below package and followed the steps and its works for me.
https://www.npmjs.com/package/cypress-visit-with-custom-geolocation
Working sample example:
https://github.com/gaikwadamolraj/cypress-visit-with-custom-geolocation