How to enable verbose logging in ChromeDriver? (GebConfig) - google-chrome

I have to enable verbose logging in Chrome / ChromeDriver to see why my geb tests are failing. Does anybody know how I can do that. Heres my GebConfig:
String chromeDriverDownloadFullPath = "https://chromedriver.storage.googleapis.com/${chromeDriverVersion}/${chromeDriverZipFileName}"
File chromeDriverLocalFile = downloadDriver(
currentPlatformName,
chromeDriverDownloadFullPath,
chromeDriverExecFileName,
'zip',
"chrome",
chromeDriverVersion)
System.setProperty('webdriver.chrome.driver', chromeDriverLocalFile.absolutePath)
Locale locale = getLocale()
ChromeOptions options = new ChromeOptions();
options.addArguments("--lang=" + locale.country);
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--no-sandbox");
options.addArguments("--user-data-dir=/data");
DesiredCapabilities jsCapabilities = DesiredCapabilities.chrome()
Map<String, Object> prefs = new HashMap<>()
prefs.put("intl.accept_languages", locale.toLanguageTag())
options.setExperimentalOption("prefs", prefs)
jsCapabilities.setCapability(ChromeOptions.CAPABILITY, options)
ChromeDriver chromeDriver = new ChromeDriver(options)
chromeDriver.manage().window().setSize(getDimension())
return chromeDriver

To obtain verbose logs from ChromeDriver we can configure the logfile and the type_of_logging as follows:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
System.setProperty("webdriver.chrome.logfile", "C:\\Utility\\BrowserDrivers\\chromedriver.log");
System.setProperty("webdriver.chrome.verboseLogging", "true");
Update :
I can see from your code you have provided :
System.setProperty('webdriver.chrome.driver', chromeDriverLocalFile.absolutePath)
In a similar fashion try to provide :
System.setProperty('webdriver.chrome.logfile', chromeDriverLocalFile.absolutePath);
System.setProperty('webdriver.chrome.verboseLogging', boolean);

From the documentation of ChromeDriver - WebDriver for Chrome
System.setProperty("webdriver.chrome.logfile", "Your path");
System.setProperty("webdriver.chrome.verboseLogging", "true");

Related

ChromeOptions not getting applied

I am trying to open the chrome browser with devtools open in maximized window.
Following code does not work, driver opens the URL in a smaller window without devtools.
System.setProperty("webdriver.chrome.driver", "<path to chrome.exe>");
ChromeOptions options = new ChromeOptions();
<String> chromeoptions = new ArrayList<String>();
chromeoptions.add("start-maximised");
chromeoptions.add("auto-open-devtools-for-tabs");
options.addArguments(chromeoptions);
WebDriver driver=new ChromeDriver(options);
driver.get("http://www.google.com");
I have also used Capabilities with no result.
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.binary", "<path to chrome exe>");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver=new ChromeDriver(capabilities);
I am using Chrome 75, selenium jar version 3.4.0
Any suggestion?
As per your code, there have some spelling mistake like("start-maximised") instead of ("--start-maximized")
kindly use the below code:
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.addArguments("--auto-open-devtools-for-tabs");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
Update: I changed selenium version to 3.0.0 and It started working.
Try using chrome options like this. it'll download the correct chrome driver version automatically and also chrome options work correctly.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument("--start-maximized")
driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
driver.get('https://www.facebook.com/');

ChromeDriver 2.29 Unable to set automatic downloads to Allow by default

After upgrading to ChromeDriver 2.29, the default value for 'Automatic Downloads 'localhost:9000' is set to 'Ask'. Whenever my tests clicks on a link which invokes a download, the Save As windows dialog opens up. Previously it would download silently to the default Downloads folder of Chrome.
How do I change this setting's default value to 'Allow' in chromedriver (not chrome)?
I've tried using the chrome.switches but they didn't work:
chrome.switches=--disable-extensions,--disable-infobars,--allow-insecure-localhost,--safebrowsing-disable-download-protection
The default setting in Chrome is 'Allow' for all sites.
'http://localhost:9000' has also been added to Exceptions list.
You can set default download location using capabilities. It will download the file to that folder, it won't give any pop up's
Just try with below line of code
DesiredCapabilities capabilities = new DesiredCapabilities();
String downloadPath = System.getProperty("user.dir")+ "\\Downloads";
HashMap<String, Object> chromePrefs = new HashMap<>();
chromePrefs.put("download.default_directory", downloadPath);
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("safebrowsing.enabled", "true");
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeOptionsMap = new HashMap<>();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--test-type");
capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY,chromeOptionsMap);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
Let me know if you face any issue

adding extension to chrome using chromeoptions doesnot work in selenium

I am a have a chrome extension developed, which I would like to have added to chrome.
When loaded the extension using the below code in selenium
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("prefs", prefs);
String CurrentDir = System.getProperty("user.dir");
String pluginDir = CurrentDir + "\\src\\main\\resources\\extensions\\newPlugin\\Chrome\\";
chromeOptions.addArguments("--load-extension=", pluginDir);
WebDriver driver = new ChromeDriver(chromeOptions);
Is there something I am missing? Please let me know

Disable chrome download multiple files confirmation

I developed a crawler with ruby watir-webdriver that downloads some files from a page. My problem is that when I click to download the second file, Chrome opens a bar in the top asking for confirmation that I am downloading multiple files from this website.
Once this is used by webdriver, I cannot confirm the download. Is there anyway to avoid this confirmation? I am thinking if is there any configuration to avoid it or if is there an extension to do this or even if I can click on the confirmation with webdriver.
thanks
I'm using Chrome 49 and none of the other solutions worked for me.
After some research I found a working solution:
ChromeDriver createChromeDriverWithDownloadFolder(String folder) {
Map<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", folder);
chromePrefs.put("profile.content_settings.exceptions.automatic_downloads.*.setting", 1 );
chromePrefs.put("download.prompt_for_download", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
return new ChromeDriver(cap);
}
It seems as if these settings are constantly changing. Therefore, here's how I found the right solution for my setup:
Open Chrome and go to chrome://version/ to find the path of your profile
In Default/Preferences is a json file called Preferences. Open it and search for automatic_downloads.
In my case the interesting part of the file looked like this:
..."profile": {
"avatar_bubble_tutorial_shown": 1,
"avatar_index": 0,
"content_settings": {
"clear_on_exit_migrated": true,
"exceptions": {
"app_banner": {},
"auto_select_certificate": {},
"automatic_downloads": {
"[.]localhost:63342,": {
"setting": 1
},...
From that I could derive that the right setting would be chromePrefs.put("profile.content_settings.exceptions.automatic_downloads.*.setting", 1 );
As of Chrome 56.0.2924.87, February 17, 2017, the only preference you need to set (however you set them for your webdriver) is:
'profile.default_content_setting_values.automatic_downloads': 1
Giving an updated answer because most answers here use outdated preferences or show other preferences that are unnecessary.
for new chrome (version 46 or newer) this options was changed
now your hash must looks like this:
prefs = {
'profile' => {
'default_content_settings' => {'multiple-automatic-downloads' => 1}, #for chrome version olde ~42
'default_content_setting_values' => {'automatic_downloads' => 1}, #for chrome newer 46
}
}
browser = Watir::Browser.new :chrome, options: {prefs: prefs, args: ['--test-type', '--disable-infobars'}
Here is the solution for Java - Selenium implementation
We faced hard time fixing this, as we wanted to add automation test for functionality which downloads set of PDFs on a single download link.
Map<String, Object> prefs = new HashMap<String, Object>();
//To Turns off multiple download warning
prefs.put("profile.default_content_settings.popups", 0);
prefs.put( "profile.content_settings.pattern_pairs.*.multiple-automatic-downloads", 1 );
//Turns off download prompt
prefs.put("download.prompt_for_download", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOptions("prefs", prefs);
driver = new ChromeDriver(options);
Hope this help to someone.
It seems that the solution is different for older and newer chromedriver versions and that is adding to the confusion.
chromedriver
profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = download_directory
b = Watir::Browser.new :chrome, :profile => profile
chromedriver2
prefs = {
'profile' => {
'default_content_settings' => {'multiple-automatic-downloads' => 1},
}
}
b = Watir::Browser.new :chrome, :prefs => prefs
Today most people are probably using chromedriver2 version and that is a solution that should work fine. It worked ok in my watir scripts as I am not getting the message: "This site is attempting to download multiple files. Do you want to allow this?" anymore.
Java solution:
cap = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<>();
Map<String, Object> content_setting = new HashMap <>();
content_setting.put("multiple-automatic-downloads",1);
prefs.put("download.prompt_for_download", "false");
prefs.put("profile.default_content_settings", content_setting);
options.setExperimentalOption("prefs", prefs);
cap.setCapability(ChromeOptions.CAPABILITY, options);
this is what worked for me:
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("profile.default_content_setting_values.automatic_downloads", 1);
chromePrefs.put("download.prompt_for_download", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
This bug/enhancement has been raised in the chromedriver page at the below URL:
http://code.google.com/p/chromedriver/issues/detail?id=130
Bug/Enhancement Status: Yet to be resolved.
I have tried to do it on page load client-side using markups.
<META HTTP-EQUIV="Content-Disposition" CONTENT="inline" />
It seems to work (it is working at this moment, in overriding).
But time will tell (might not have effect on future CHROME's, you know what I mean).
There are a list of available header fields published on a couple of sites which I find extremely helpful. Hope it will help you, as well.
https://www.w3.org/Protocols/HTTP/Issues/content-disposition.txt
https://www.iana.org/assignments/cont-disp/cont-disp.xhtml#cont-disp-2

Unable to Launch Google Chrome using default/custom profile in Selenium WebDriver that undergo http basic authentication

Following methods are not working properly.
Since GRID is used, capability is set as null here.
System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");
DesiredCapabilities capability=null;
Method 1:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--user-data-dir=C:/Users /username/AppData/Local/Google/Chrome/User Data/Default"));
driver = new ChromeDriver(capabilities);
Method 2:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
String chromeProfile = "C:/Users/username/AppData/Local/Google/Chrome/Application /chrome.exe";
ArrayList<String> switches = new ArrayList<String>();
switches.add("C:/Users/username/AppData/Local/Google/Chrome/User Data/Default" + chromeProfile);
capabilities.setCapability("chrome.switches", switches);
driver = new ChromeDriver(capabilities);
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
If you face such error:
org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited normally
Then try to create a new Chrome profile and execute tests.
Copy the folder, 'User Data'
Paste & Rename the folder on the same location. e.g., New User
Now, locate the directory, C:/Users/user_name/AppData/Local/Google/Chrome/New User
If you like to test the profile, then bookmark some of the sites & observe them on next run.
1 Set the chromedriver property in starting the node. My approach:
java -jar selenium-server-standalone-2.31.0.jar -role node -hub http://localhost:4444/grid/register -maxSession 15 -browser browserName="chrome",version=ANY,platform=WINDOWS,maxInstances=15 -Dwebdriver.chrome.driver=lib\chromedriver.exe
2 Inside the code my approach:
capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"), capabilities);
3 loading the HTTP basic auth page:
String username = "Pavel";
String password = "Omgtoosecrettotellyou";
driver.get("http://" + username + ":" + password + "#" +"your-site.com");