Setting download directory for alternate profile in chrome selenium - java

I use the suggested methods for setting profile and folder path for downloads shown on the docs
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory", "C:\\testDownloads");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:\\seleniumChromeProfile");
options.setExperimentalOption("prefs", prefs);
ChromeDriver driver= new ChromeDriver(options);
And it works. With the Preferences file open in notepad++ I can see that when the browser starts, the file gets edited with the correct default_directory path.
Now I have a new use case where I would like to use a new profile. In chrome you can switch between profiles on the browser. I was confused at first because the user-data-dir argument was already used, but later I realized each "user data directory" can contain one or more profiles. This profile can be set using
options.addArguments("profile-directory=PROFILE 2");
With this additional argument, selenium now loads with a new profile called PROFILE 2 (instead of Default), which is what I want. If the profile doesn't exist, it will be created automatically.
However, one issue I've noticed is the download directory isn't set properly for the new profile; it defaults to windows download folder instead. I decided to look at the Preferences file for the Default profile, and noticed that it was getting the changes instead.
My conclusion here is
The preferences are being set first
The profile is then changed
Which is a bit weird to me. I'm not using the latest version of webdriver but I wanted to know if anyone has encountered this issue before, and whether this behavior (setting preferences after changing profile) works properly in later versions.
I thought maybe the order that I set the options might make a difference, but everything goes into the ChromeOptions object and is passed into ChromeDriver during instantiation so I'm not sure if order is something I have control over.

Related

I'm trying to remove the option of where to save a file to download from chrome, and I can't get it with this configuration

I have used different options, which do work for me with headlessmode enabled, but not inactive.
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("download.default_directory", driverFactory.getDownloadPath());
prefs.put("download.prompt_for_download", false);
prefs.put("download.default_directory", driverFactory.getDownloadPath());
And when setting the download directory by default, it ignores the previous configurations, and the popup keeps popping up, thus failing me the tests that have some implication in downloading some type of file.

Selenium ChromeDriver use custom profile folder - permission error

so I'm trying to create a custom webdriver (as in (down)load the normal driver but use custom profile settings) to get it to open any tab with a popped-out DevTools window (set to "Console").
I figured I could do that by setting up such a preference file by just interacting with my own installed Chrome version and copying my preferences to another folder, then let the Selenium WebDriver load its preferences from that folder.
But when I tried starting the WebDriver using this:
options.addArguments("--profile-directory=\"Default\"");
options.addArguments("--user-data-dir=./temp-selenium");
... it always shows an error (in german, roughly translated to):
Google Chrome can't read or write in the following directory: ./temp-selenium
Removing the second line completely just starts a Chome window with a user profile selection prompt. Setting up a file path to something like AppData doesn't work because then I get the following error:
Could not remove old devtools port file. Perhaps the given user-data-dir at "myFilePath\User Data" is still attached to a running Chrome ...
By the way, why I'm doing this: I just want to SEE the console logs while testing stuff. Can't imagine it's actually THIS difficult to get to it. My tests are way too fast to manually open the console in time and I need to find a bug that happens every 100 tries. Opening the console 100 times is just way too tedious.
Anyone have an idea?

Force WebDriver to use an Exisitng Firefox Profile in Geckdriver 0.30.0 without Creating Temporary Copy?

By default, Geckodriver 0.30.0 copies existing FF profile to temporary files directory before launching a new instance of WebDriver.
The code responsible for this is written in Rust and is present in src/browser.rs of geckodriver source, in line 60 - 70 as discussed in this thread:
let mut profile = match options.profile {
Some(x) => x,
None => Profile::new()?,
};
I’m able to permanently force Geckodriver 0.30.0 to use a specific FF Profile directly (w/o first making a copy of it) by modifying the above code to:
let path = std::path::Path::new("path-to-profile");
let mut profile = Profile::new_from_path(path)?;
However, this would only allow me to use a single profile in the entire application as the path_to_profile is hard coded into above file.
I need to be able to launch hundreds of FF profiles throughout the lifecycle of my application and thus need to choose any existing profile at runtime using the following code structure:
File firefoxProfileFile= new File(path_to_profile_file);
FirefoxProfile firefoxProfile = new FirefoxProfile(firefoxProfileFile);
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(firefoxProfile);
Using the code above it would be possible to dynamically select any existing FF profile at runtime, without first copying profile to temporary folder - but only if I can somehow pass the path to profile as an argument to above code.
How can this be done?

Selenium: -browser.helperApps.neverAsk.openFile and savetodisk is not working

I have a critical issue over here.
Please find my scenario below:
login
click on a link
after the click, a new tab opens
I have switched the focus to the new opened tab with the following code
ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(newTab.get(1));
The issue is that when I try to click on an excel download link on the newly opened tab, the "Open with" popup is appearing and my automation fails. Even after adding the following preference
firefoxProfile.setPreference("browser.helperApps.neverAsk.openFile",
"text/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"text/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
Please, can you suggest a feasible solution as soon as possible?
Thank you
Please manually set these preferences in your firefox's(about:config) section, visit the application, click the link and see if the file gets downloaded without any prompt. This will help you to identify the issues with automation.
I tried setting these preferences in my firefox, but it still prompts download window. I can download without prompt only after checking "Do this automatically for files like this from now on" which updates mimeTypes.rdf file in the profile directory. So to make this work through automation, you may need to bundle a custom firefox profile that includes a mimeTypes.rdf with your TestSuite.
Here's the code to create FirefoxProfile from a given profile directory:
FirefoxProfile profile = new FirefoxProfile(new File("<PATH_TO_FIREFOX_PROFILE_DIRECTORY_THAT_WORKS_WHEN_TESTED_MANUALLY>");
WebDriver driver = new FirefoxDriver(profile);

Java - reference maven dependency - Selenium Chromedriver

I recently started up coding with Selenium and Java. I have a basic test set up and things seem to be working with Firefox. I would like test on Chrome as well. But when I define the Webdriver as ChromeDriver, I get an error saying I need to define it on the system path.
I used Maven to download all the dependencies, but now I don't know how to reference them properly.
My issue:
protected void setUpBeforeTestClass(){
// define path to ChromeDriver
// cause I get the error "The path to the driver executable must be set by the webdriver.chrome.driver system property"
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
// driver = new FirefoxDriver();
driver = new ChromeDriver();
String url = urls[0]; // pull in from array of urls
driver.get(url);
}
Maven downloads dependencies to:
C:\Users\{username}\.m2\repository\org\seleniumhq\selenium\ ...
And ChromeDriver is in that folder.
How can I reference this folder to pull in ChromeDriver without hard-coding the path? (I'm not looking to modify my system environment variables)
My goal is that I can just download my Java classes and Maven dependencies on any machine and run the tests.
You need to download the Chrome Driver Binary and put it somewhere on your computer. Somewhere like "C:/Selenium/chromedriver.exe". You can find it here. You can then access it by using something like:
System.setProperty("webdriver.chrome.driver", "C:/Selenium/chromedriver.exe"));
As per answers, I found that it is the binary that I was missing. Damn.
I found this:
https://github.com/bonigarcia/webdrivermanager
This helps out a lot in terms of managing the webdrivers I want to use. I don't have to download the webdrivers myself, this does it for me.
Download the binary from here:-
http://chromedriver.storage.googleapis.com/index.html?path=2.19/
Use below code:-
WebDriver driver=null;
System.setProperty("webdriver.chrome.driver","./src//lib//chromedriver");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
capabilities.setCapability("chrome.binary","./src//lib//chromedriver");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
Hope it help :)
Get back to me if still facing issue :)

Categories

Resources