How to use chrome profile when using browserInDocker with WebDriverManager? - java

I'm using webdrivermanager with browserInDocker() for my test automation framework. So far it works great. But now I have trouble trying to use a chrome profile.
Usually, when I use selenium webdriver, I simply can set a ChromeOption and it works.
How can I use it with browserInDocker()?
I tried the following code:
public void setupBrowserInDockerWithProfile() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir=/home/seluser/.config/google-chrome/Default");
driver = WebDriverManager
.chromedriver()
.capabilities(options)
.browserInDocker()
.dockerVolumes("/var/lib/docker/volumes/chromeprofile/_data:/home/seluser/.config/google-chrome/Default")
.create();
driver.manage().window().maximize();
driver.get(APP_URL);
}
I tried it with and without a docker volume, but I'm not even sure if I use it right. I'm searching for days now, but could't find a solution yet.

The problem is the argument --user-data-dir=/home/seluser/.config/google-chrome/Default. As explained in the doc, WebDriverManager uses the Aerokube's Docker images. That path is available in the docker-images, but not in Aerokube's. You can drop that argument or change it to the following:
options.addArguments("--user-data-dir=/home/selenium/.config/google-chrome/Default");

Related

How can I set my Firefox browser in private mode in Selenium 2.53.1?

I'm on selenium 2.53.0 (I don't want to update to 3.0 because I need to work on Firefox and Selenium did not implement actions for FF).
My problem is: I try to open FF in private mode (I don't want to keep the cache because I ran a lot of FF instances)
For that, I use a FF profile and I have a certificate, so I accept it.
My code is :
FirefoxDriverManager.getInstance().setup();
capabilities.setBrowserName("firefox");
capabilities.setVersion("46");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffProfile = profile.getProfile("default");
ffProfile.setPreference("browser.private.browsing.autostart", true);
ffProfile.setPreference("browser.privatebrowsing.autostart", true);
//accept the certificate
ffProfile.setAcceptUntrustedCertificates(true);
ffProfile.setAssumeUntrustedCertificateIssuer(false);
capabilities.setCapability(FirefoxDriver.PROFILE, ffProfile);
WebDriver webDriver = new FirefoxDriver();
I tried "browser.private.browsing.autostart" and "browser.privatebrowsing.autostart" because when I did about:config in firefox, i found this two.
I did not received any error, firefox run my test but not in private. Do you have any idea?
I found this post but it haven't have answer.
FirefoxOptions opts = new FirefoxOptions();
opts.addArguments("-private");
FirefoxDrive f = new FirefoxDriver(opts);
working currently with FF v54 and selenium 3.4.0
On your code sample above you are trying twice to set the profile. Try removing one of the arguments? AS long as
ffProfile.setPreference("browser.privatebrowsing.autostart", true);
is not working for you, there is an alternative workaround solution; so you can work until you figure this out.
Find any element at the page you are visiting where you want the private window to be opened. For example:
Driver.FindElement(By.ByXpath(//div[#id="loginButton"])).sendKeys(Keys.chord(Keys.CONTROL, Keys.SHIFT, "P"));
And then send a Ctrl+Shift+p to it..Then you can use the new private window for your test. Perhaps not the best solution but, this will work 100%, just tested on my machine, please comment below if you have trouble getting this right.
Best of luck!

Disable native events on chrome?

Hi I want to disable native events from Chrome driver, I did not find the way to do it, I just found examples for Firefox:
dcBrowser = DesiredCapabilities.firefox();
FirefoxProfile prof2 = new FirefoxProfile();
prof2.setEnableNativeEvents(false);
dcBrowser.setCapability("firefox_profile", prof2);
I tried to do the same for Chrome
dcBrowser = DesiredCapabilities.chrome();
ChromeProfile prof2 = new ChromeProfile();
prof2.setEnableNativeEvents(false);
dcBrowser.setCapability("chrome_profile", prof2);
When I add the import:
import org.openqa.selenium.chrome.ChromeProfile;
I get the following error:
The import org.openqa.selenium.chrome.ChromeProfile cannot be resolved
I know the ChromeProfile class does exists, but since I didn't find any example of disabling chrome native events I don't know if what I did is correct or not or maybe I am missing something, any ideas? Thanks!
There is no equivalent. Chrome always has native events enabled.
See: https://github.com/SeleniumHQ/selenium/wiki/Advanced-User-Interactions#native-events-versus-synthetic-events
If you are using java, you can simply use
super.setNativeEvents(false);

While working with Selenium WebDriver and Java I am not able to use click() function of WebDriver (giving no exception)

I have set the following properties:-
Set environment variables JAVA_HOME,path,ANT_HOME,path (using JDK version --> jdk1.7.0_40)
In Eclipse set the system Library, added all needed jars and select all jars in Order and Export.
Install the Firefox Version 24 and used the Selenium jar(selenium-server-standalone-2.41.0).
Giving no error while running the Java application, also no exception while not able to click on particular element.
sendKeys(Keys.ENTER) is working fine, but instead I need to use click() method.
Also code is working fine on other systems, I think I am missing something.
click() method is not giving any valid response, it makes me feel like its only declared not defined. I have tried on different websites, on different elements and with different types(xpath, css selector, name, id) but nothing worked for me. I took focus on that element by webdriver and also by highlighting the element using javascript code, its showing that particular element is present. I have performed other operations like entering username after clicking on that element, its performing well. I have worked with different browsers, different webpages also maximize the window using selenium but no positive response.
Any help will appreciate.
Thanks in advance
Hi the snippet below worked fine for me
public static void main(String[]args){
WebDriver appDriver;
appDriver = new FirefoxDriver();
appDriver.get("https://web210.qa.drfirst.com/login.jsp");
WebElement loginButton = appDriver.findElement(By.className("btn"));
loginButton.click();
appDriver.switchTo().alert().accept();
appDriver.close();
}

Load Chrome Profile using Selenium WebDriver using java

I'm having some troubles getting Selenium loading a chrome profile.
String pathToChrome = "driver/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", pathToChrome);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
String chromeProfile = "C:\\Users\\Tiuz\\AppData\\Local\\Google\\Chrome\\User Data\\Default";
ArrayList<String> switches = new ArrayList<String>();
switches.add("--user-data-dir=" + chromeProfile);
capabilities.setCapability("chrome.switches", switches);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("http://www.google.com");
It starts great and works perfect, but I want to get the default profile loaded because I want to test it with some Extensions enabled and some preferences tested.
Does anybody has an idea why this code isn't working?
I've tested it with Selenium 2.29.1 and 2.28.0 with chromedriver 26.0.1383.0 on windows 7 x64.
This is an old question, but I was still having a problem getting it to work so I did some more research to understand what was happening. The answer from #PrashanthSams is correct, but I was incorrectly adding \Default to the end of the profile path
I found that Chrome appends \Default to the profile path specified in the user-data-dir. So if your profile path is specified as:
user-data-dir=C:\Users\user_name\AppData\Local\Google\Chrome\User Data\Default\
it will append \Default and you will end up at:
C:\Users\user_name\AppData\Local\Google\Chrome\User Data\Default\Default
which is not the same as the profile that you would get if you opened chrome under that user profile normally.
You can also verify your settings if you open a command prompt, navigate to the chrome executable directory, and run chrome with the options specified similar to this:
chrome.exe --user-data-dir="C:\Users\user_name\AppData\Local\Google\Chrome\User Data"
Finally, you can go to a new tab in Chrome and browse to chrome://version/ you will see the actual profile that is being used. It will be listed like:
Profile Path C:\Users\user_name\AppData\Local\Google\Chrome\User Data\Default
These combinations did trick for me :)
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);
You should try this
op.addArgument("--user-data-dir=C:/Users/username/AppData/Local/Google/Chrome/User Data/");
op.addArgument("--profile-directory=Profile 2");
Path where Chrome stores the profiles on Linux.
String chromeProfilePath = "/home/(user)/.config/google-chrome/Profile3/";
Creating ChromeOptions object, desabling the extensions and adding the profile I want to use by ".addArguments".
ChromeOptions chromeProfile = new ChromeOptions();
chromeProfile.addArguments("chrome.switches", "--disable-extensions");
chromeProfile.addArguments("user-data-dir=" + chromeProfilePath);
As said above by JasonG, after this point Google-Chrome will append \Default to the String you've provided.
There is a "/Default" folder inside "/Profile3" directory, so what I did was...
I copied the content of "/Profile3" to the "/Default" folder.
Set the Browser System Properties and Path as you usually do, call the constructor that receives a ChromeOption and it will work fine.
WebDriver driver = new ChromeDriver(chromeProfile);
I copied default profile to any other folder and then I do connect to this copy
ChromeOptions options = new ChromeOptions();
options.AddArgument("--user-data-dir=C:\\AnyFolder");
driver = new ChromeDriver(options);
So it uses default profile
I tried in windows and following code works for me:
String userProfile= "C:\\Users\\user_name\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\";
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir="+userProfile);
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com");
How to know whether it is working ?
One way to know is to run the program twice without killing previous instance of the chrome. If the profile is valid, you'll see the second instance "as a new tab" in the first browser window. If it is not working, you get the second instance "as a new browser window".
According to the ChromeDriver wiki, this is a known issue and is currently not possible.
https://code.google.com/p/selenium/wiki/ChromeDriver
None of these above solutions works for me. And after several hours tirelessly research and trying, I already found out this solution
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
//chrome://version/
chromeOptions.addArguments("--user-data-dir=/home/{username}/.config/google-chrome");
//load default profile
chromeOptions.addArguments("--profile-directory=Default");

Automating switching browsers when running Selenium-Java tests

I am currently working on a project using Java, Selenium and Testng. My overall goal is to test the functionality of a webpage over different web browsers. I have my Selenium code working and am able to run the test on Chrome and Firefox. However, I have to manually change the code to switch the browser. I do this by commenting out driver = new ChromeDriver();
I would like to edit my code so the test runs in Firefox and when that test is complete start the test in Chrome. Can someone please guide me in the right direction?
Here is a sample of what my code looks like:
WebDriver driver = null;
Selenium selenium = null;
#BeforeSuite
public void setup() throws Exception {
/// Chrome Driver ///
System.setProperty("webdriver.chrome.driver", "mac/chromedriver.exe");
//driver = new ChromeDriver();
/// Firefox Driver ///
driver = new FirefoxDriver();
}
#Test
public void testGoogle() throws Exception {
selenium = new WebDriverBackedSelenium(driver,"URL");
There could be quite a few ways of achieving this.
In the setup you can read a property and based on that you can instantiate the right driver.
String driverType = System.getProperty("driverType");
if ("firefox".equals(driverType))
driver = new FirefoxDriver().....
You can run the test twice, once with firefox property and then with chrome property.
Other option is to keep all the test in one class. Then extend this class by two classes, one for firefox setup and another for chrome setup. Then you can run both the subclass tests in a suite. They would run one after the another.

Categories

Resources