How can i set browser language in Selenide using Java? - java

Hello guys please how can i do it?When i run not headless mode browser is in english and everything works fine, but when i run headless mode language is changed to my native language.
I am using this for headless mode.
Configuration.headless = true;

Selenide Configuration class contains
public static MutableCapabilities browserCapabilities which used within the driver startup if provided.
For Chrome:
ChromeOptions options = new ChromeOptions()
.setHeadless(true)
.addArguments("--lang=en_US");
Configuration.browserCapabilities = options;
But note --lang argument might be ignored on Linux.
For Firefox:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("intl.accept_languages", "en-US");
FirefoxOptions options = new FirefoxOptions()
.setHeadless(true);
.setProfile(profile);
Configuration.browserCapabilities = options;

Related

Selenium Launches Firefox While Silently Ignoring Profile Preference

There are currently two recommended ways to launch the latest version of Selenium using a specific profile. One is through the Desired Capabilities object and the other us by using Firefox Options.
Desired Capabilities:
public static WebDriver launchFirefoxProfileByFFCapabilities()
{
WebDriver driver = null;
System.setProperty("webdriver.gecko.driver", "PATH");
ProfilesIni profile = new ProfilesIni();
String profileName = "Profile_1";
FirefoxProfile firefoxProfileByProfilesIni = profile.getProfile(profileName);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, firefoxProfileByProfilesIni);
FirefoxOptions opt = new FirefoxOptions();
opt.merge(dc);
driver = new FirefoxDriver(opt);
return driver;
}
Firefox Options:
public static WebDriver launchFirefoxProfileByFFOptions()
{
WebDriver driver = null;
System.setProperty("webdriver.gecko.driver", "PATH");
ProfilesIni profile = new ProfilesIni();
String profileName = "Profile_1";
FirefoxProfile firefoxProfileByProfilesIni = profile.getProfile(profileName);
FirefoxOptions opt = new FirefoxOptions();
opt.setProfile(firefoxProfileByProfilesIni);
driver = new FirefoxDriver(opt);
return driver;
}
I noticed a few interesting discrepancies between the two approaches and I'm trying to understand what is the reason for these discrepancies and how to prevent them.
1) With Firefox Options when the specified Profile Name does not exist (as it appears in the Firefox - Choose User Profile dialog) throws WebDriverException: Unexpected value for profile: null. However with Desired Capabilities no such exception is thrown and Selenium silently ignores the profile preference (which is not ideal)
2) If the Profile Name exists (as it appears in the Firefox - Choose User Profile dialog) but the path in profiles.ini file is incorrect even Firefox Options will launch silently ignore the profile preference and launch WebDriver not connected to any profile
What is the cause of these discrepancies? How can they be prevented?
Thanks
It seems that Capabilities and FirefoxProfile/FirefoxOptions handle the profileIni in a different way.
Could you please modify and add the stack-traces so we can further help you?

How to Disable JavaScript in WebDriver when automating in Selenium?

I am currently trying to write a test that tests the non-JS version of my website. I use Selenium with Java. I have tried this:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("javascript.enabled", false);
driver = new FirefoxDriver(profile);
driver.get();
However this isn't working. It just loads the page with JavaScript Enabled.
As a workaround I did, this, for the requirement.
It will manually set the javascript.enabled property to false by the following script.
WebDriver driver = new FirefoxDriver();
driver.get("about:config");
Actions act = new Actions(driver);
act.sendKeys(Keys.RETURN).sendKeys("javascript.enabled").perform();
act.sendKeys(Keys.TAB).sendKeys(Keys.RETURN).perform();

ChromeDriver Disable Javascript in Java

https://code.google.com/p/selenium/issues/detail?id=3175
Doesn't work.
So then I tried this,
ChromeOptions opts = new ChromeOptions();
opts.addArguments("--disable-javascript");
driver = new ChromeDriver(opts);
But then driver.get(website);
javascript is enabled again. When it was on data; it was disabled.
Also I tried,
DesiredCaptabilities caps = DesiredCaptabilties.chrome();
caps.setJAvaScriptEnabled(fale);
driver = new ChromeDriver(caps);
driver.get(Website);
Nothing is working. Any advice?
javascriptEnabled just works on HTMLUnitDriver.
And ChromeDriver should have JavaScript enabled to work properly in the first place, so you canĀ“t disable JavaScript if you use ChromeDriver2.
static public void DisableJS () {
driver.get("chrome://settings");
driver.switchTo().frame("settings");
driver.findElement(By.id("advanced-settings-expander")).click();
driver.findElement(By.id("privacyContentSettingsButton")).click();
//here do not allow js
driver.findElement(By.xpath("//*[#id='content-settings-page']/div[2]/section[3]/div/div[2]/label/input")).click();
driver.findElement(By.id("content-settings-overlay-confirm")).click();
}

Modify firefox profile template

I want to modify a firefox template from which webdriver will create temporary profile by
new FirefoxDriver();
I am linux user (ubuntu). Anybody knows where this template is located?
We don't modify the actual file in our solution.
Granted, this is C#, but it should be very similar:
FirefoxProfile profile = new FirefoxProfile() {AcceptUntrustedCertificates = true};
profile.SetPreference("webdriver_assume_untrusted_issuer", false);
Driver = new FirefoxDriver(profile);
Driver.Manage().Window.Maximize();

Selenium 2 chrome driver preferences java equivalent to RubyBindings

I've been looking for a way to set the driver preferences for chrome driver using java for the past two days with no luck.
I have however found a solution in ruby VIA RubyBindings and would like to know if there is a java equivalent line I can use for this.
The ruby code is the following:
profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = "/path/to/dir"
driver = Selenium::WebDriver.for :chrome, :profile => profile
While searching I found that chrome does not have a profiler I could use like the FirefoxProfile class, so I started using the DesireCapabilities class instead. After further investigation into this problem I found that I could set the "switches" and "prefs" VIA capabilities.setCapabilitiy and ended up with the following:
Map<String, String> prefs = new Hashtable<String, String>();
prefs.put("download.prompt_for_download", "false");
prefs.put("download.default_directory", "/path/to/dir");
prefs.put("download.extensions_to_open", "pdf");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.prefs", prefs);
dr = new ChromeDriver(capabilities);
However I was not able to get this working, the default download directory was never changed to the specified directory once started. I am unsure if there is a problem with how I am trying to set this capability or if the problem lies elsewhere.
In the end I eventually used the solution proposed here:
http://dkage.wordpress.com/2012/03/10/mid-air-trick-make-selenium-download-files/
but I would like to know if it is possible to do this more cleanly but just setting the preferences directly instead of using the UI
Any help is appreciated, Thanks!
Update:
Surprisingly after updating Selenium 2 to version 2.24.1 (and to windows chrome 22), the code above with the Maps work as expected, the only problem now is that they deprecated the the use of the constructor ChromeDriver(DesiredCapabilities capabilities), and instead recommend I use the ChromeOptions class, which I cannot get working for the above scenario.
Below is the wiki page explaining the use of both ChromeOptions and DesiredCapabilities:
http://code.google.com/p/chromedriver/wiki/CapabilitiesAndSwitches
The Ruby bindings actually expands that to:
{
"download": {
"prompt_for_download": false,
"default_directory": "/path/to/dir"
}
}
Try building your Java prefs object like that and see if it works. The string vs boolean false could also be an issue.
Try this (Forgive my java which is quite rusty, but hopefully you get the idea)
Dictionary download = new Dictionary();
download["default_directory"] = "/path/to/dir";
Dictionary prefs = new Dictionary();
prefs["browser"] = download;
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.prefs", prefs);
WebDriver driver = new ChromeDriver(capabilities);
Update: I just browsed the code and it seems that what I suggested above probably won't work. The ruby chrome profile class creates zip file with chrome profile file structure in it to support chrome preference. I couldn't find such facility code in java. There is a Firefox profile in java that does the simliar thing for firefox, but obviously that won't work for chrome. So in short, this feature is not supported yet in java.
Newer versions (I tested Chrome 44.0.2403.125, Selenium 2.47.1, and ChromeDriver 2.17.340128) work with the following:
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory", "/path/to/directory");
options.setExperimentalOption("prefs", prefs);
ChromeDriver chromeDriver = new ChromeDriver(options);

Categories