Selenium WebDriver Load both a Capability and Profile - java

Looking to load both DesiredCapabilities and a FirefoxProfile, can't figure it out and can't find an answer for it

There is the FirefoxDriver.PROFILE capability for RemoteWebDriver.
For example,
FirefoxProfile yourProfile = new FirefoxProfile("path_to_your_profile");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, yourProfile);

Create your firefox profile, i.e: "profileFF"
DesiredCapabilities cap = new DesiredCapabilities();
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("profileFF");
cap.setCapability(FirefoxDriver.PROFILE, myprofile );
OR
DesiredCapabilities cap = new DesiredCapabilities();
File firefoxProfileFolder = new File("/path/to/your/firefox/profile/profileFF");
FirefoxProfile myprofile = new FirefoxProfile(firefoxProfileFolder);
cap.setCapability(FirefoxDriver.PROFILE, myprofile );

Related

unable to launch FireFox custom profile with geckodriver in java

I'm trying to launch a Firefox profile with add-ons in it, with selenium v3.12 and gecko-driver v2.10 and Firefox version 60.0, how-ever it seems that the custom profile is not working. below is my code
static WebDriver driver;
ProfilesIni profile = new ProfilesIni();
myprofile = profile.getProfile("AutoProfile");
System.setProperty("webdriver.gecko.driver",
"E:\\Library\\geckodriver-v0.21.0-win32\\geckodriver.exe");
driver = new FirefoxDriver(myprofile);
the acutal error is on the line
driver = new FirefoxDriver(myprofile);
as
The constructor FirefoxDriver(FirefoxProfile) is undefined
You have to pass it through firefox options.
System.setProperty("webdriver.gecko.driver", "E:\\Library\\geckodriver-v0.21.0-win32\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("AutoProfile");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(myprofile);
WebDriver driver = new FirefoxDriver(firefoxOptions);
If the below solution causes a java heap error, you could try DesiredCapabilities, like this:
System.setProperty("webdriver.gecko.driver","E:\\Library\\geckodriver-v0.21.0-win32\\geckodriver.exe");
File file = new File(path_to_your_firefox_profile);
DesiredCapabilities dc = DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile(file);
dc.setCapability(FirefoxDriver.PROFILE, profile);
FirefoxDriver driver = new FirefoxDriver(dc);

How to Merge Chrome driver service with desired capabilities for headless using xvfb?

I want to merge ChromeDriverService with chromeOptions or with DesiredCapabilities for running browser in xvfb.
Below is part of code ChromeDriverService I previously used without selenium grid.
String NodeChromeIncognito = "http://localhost:5558/wd/hub"
ChromeDriverService chromeDriverService = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("driver_linux/chromedriver"))
.usingAnyFreePort()
.withEnvironment(ImmutableMap.of("DISPLAY", ":1")).build();
chromeDriverService.start();
// commented because using RemoteWebDriver
// driver = new ChromeDriver(chromeDriverService);
and below is part code of RemoteWebDriver which I will merge with ChromeDriverService.
DesiredCapabilities cap = null;
String NodeChromeIncognito = "http://localhost:5558/wd/hub";
String NodeChrome = "http://localhost:5557/wd/hub";
String NodeFirefox = "http://localhost:5556/wd/hub";
if (browserName.equals("chrome")) {
cap = DesiredCapabilities.chrome();
cap.setBrowserName("chrome");
driver = new RemoteWebDriver(new URL(NodeChrome), cap);
} else if (browserName.equals("firefox")) {
System.setProperty("webdriver.gecko.driver", "driver_linux/geckodriver");
cap = DesiredCapabilities.firefox();
cap.setCapability("marionette", true);
driver = new RemoteWebDriver(new URL(NodeFirefox), cap);
}else if (browserName.equals("chromeIncognito")) {
ChromeOptions option = new ChromeOptions();
option.addArguments("--incognito");
cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, option);
cap.setPlatform(Platform.WIN10);
cap.setBrowserName("chrome incognito");
driver = new RemoteWebDriver(new URL(NodeChromeIncognito), cap);
}
I know I could use addArguments("--headless") for chrome, but it does not work well with the my webApp. And also I used DesiredCapabilities.merge and error.
How to merge code/configuration ChromeDriverService with ChromeOptions or DesiredCapabilites ?
As you mentioned you want to merge ChromeDriverService with ChromeOptions or with DesiredCapabilities both can be achieved. But as of current Selenium Java Client releases the following Constructors are Deprecated :
ChromeDriver(Capabilities capabilities)
//and
ChromeDriver(ChromeDriverService service, Capabilities capabilities)
Hence we have to use either of the following options :
Option A : Use only ChromeOptions :
private static ChromeDriverService service;
private WebDriver driver;
//code block
service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("path/to/my/chromedriver.exe"))
.usingAnyFreePort()
.build();
chromeDriverService.start();
ChromeOptions option = new ChromeOptions();
option.addArguments("--incognito");
driver = new RemoteWebDriver(service.getUrl(), options);
Option B : Use DesiredCapabilities and then use merge() from MutableCapabilities to merge within ChromeOptions :
private static ChromeDriverService service;
private WebDriver driver;
//code block
service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("path/to/my/chromedriver.exe"))
.usingAnyFreePort()
.build();
chromeDriverService.start();
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("...", true);
ChromeOptions option = new ChromeOptions();
option.addArguments("--incognito");
option.merge(capabilities);
driver = new RemoteWebDriver(service.getUrl(), options);

WebDriver to disable SEC_ERROR_UNKNOWN_ISSUER in Firefox

WebDriver opens browser window, but I get SEC_ERROR_UNKNOWN_ISSUER.
I tried to add this site as an exception in browser, but when new browser window opened I get the same message again instead of the website.
FirefoxProfile profile = new FirefoxProfile();
FirefoxOptions options = new FirefoxOptions();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(true);
options.setLogLevel(Level.ALL);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options);
WebDriver driver = new FirefoxDriver(capabilities);
You need to set setAcceptInsecureCerts Capabilities as true
This simple code work for me :-
System.setProperty("webdriver.gecko.driver", "D:\\Workspace\\StackOverlow\\src\\lib\\geckodriver.exe");
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setAcceptInsecureCerts(true);
WebDriver driver = new FirefoxDriver(desiredCapabilities);
driver.get("https://self-signed.badssl.com/");
OR
System.setProperty("webdriver.gecko.driver", "D:\\Workspace\\StackOverlow\\src\\lib\\geckodriver.exe");
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("acceptInsecureCerts", true);
WebDriver driver = new FirefoxDriver(desiredCapabilities);
driver.get("https://self-signed.badssl.com/")
Change your gecko path in first line of code. Update gecko driver and firefox

How to override useragent in firefox browser on saucelabs

Saucelabs:-
https://saucelabs.com/
Am creating the firefox driver on saucelabs using the following:-
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("version", "5");
capabilities.setCapability("platform", Platform.XP);
// Create the connection to Sauce Labs to run the tests
this.driver = new RemoteWebDriver(
new URL("http://YOUR_USERNAME:YOUR_ACCESS_KEY#ondemand.saucelabs.com:80/wd/hub"),
capabilities);
}
I want to use the mobile user agent using firefox driver. How can i do it.
Have you tried creating a new profile and setting the user agent string on the profile?
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("general.useragent.override", "UA-STRING");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, profile);

How to set Proxy setting for Chrome in Selenium Java

I am able to set proxy settings for Firefox as below.
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setProxyType(ProxyType.MANUAL);
proxy.setHttpProxy(CONFIG.getProperty("hostname"));
proxy.setSslProxy(CONFIG.getProperty("hostname"));
proxy.setFtpProxy(CONFIG.getProperty("hostname"));
proxy.setSocksUsername(CONFIG.getProperty("username"));
proxy.setSocksPassword(CONFIG.getProperty("password"));
FirefoxProfile fp = new FirefoxProfile();
fp.setProxyPreferences(proxy);
driver = new FirefoxDriver(fp);
builder = new Actions(driver);
bckdbrowser = new WebDriverBackedSelenium(driver, ConfigReader.ENVIRONMENT_URL);
But I need to setup for Chrome as well.. Can any one assist me how to do ?
Thanks
Raj
You can try using the DesiredCapabilities class, like this:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--proxy-server=http://user:password#proxy.com:8080"));
WebDriver driver = new ChromeDriver(capabilities);
Try this code:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", ProxyType.AUTODETECT.ordinal());
WebDriver driver = new FirefoxDriver(profile);
here we have one more solution....it's worked for me

Categories

Resources