Is it possible change browser language in firefox using selenium? - selenium

I want to launch firefox browser in a given language in selenium automation. Can I do that? If so, please share some sample code implementation.
Thanks in advance.

Set the language code in fire fox profile preference before launch the the driver as shown below.
FirefoxProfile ffprofile = new FirefoxProfile();
//For japenese language
ffprofile.setPreference("intl.accept_languages","ja");
driver = new FirefoxDriver(ffprofile);
driver.get("https://www.google.com");
For language codes : https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes

Related

Changing Keyboard Language - Selenium

I'm trying to change the keyboard language from english to russion because my team decided to prevent user type on a textbox before he changes the language to russian. I'm trying to test this feature using katalon and Selenium but I'm not able to change the keyboard inputs language to russian.
When instantiating a web driver instance, you can set the preferred language option. For Chrome, it looks like this:
ChromeOptions options = new ChromeOptions();
options.addArguments(“–lang=ru”);
ChromeDriver driver = new ChromeDriver(options);

RemoteWebDriver - changing window size

I'm writing selenium tests. I have problem with one script because selenium can't find a element. I think that is a problem with RemoteWebDriver because when I'm watching screens I see that screen is incomplete, without right side with that button. If I'm using local driver I have line driver.manage().window().maximize() and that works. So my question is, is that possible to maximize size of RemoteWebDriver window? Maybe there is a alternative to get full screen in that Driver? I'm using Jenkins with selenium.
I have had this problem in chrome many times, and the workaround for me is to use javascript's scrollIntoView to move the viewport to that element.
In php / phpunit-selenium:
$this->execute([
'script' => 'var elm = document.getElementById("id");elm.scrollIntoView(true);',
'args' => $args
]);
// Continue to access element
You should be able to extract that bit within 'script' and execute it as raw javascript from within whatever language you use for your selenium tests.
for chrome we can use
DesiredCapabilities cap = new DesiredCapabilities();
ChromeOptions options = new ChromeOptions();
cap.setCapability(ChromeOptions.CAPABILITY, options);
options.addArguments("--start-maximized");
driver = new ChromeDriver(cap);
firefox doesnt need any driver.window().manage().maximize() in the latest geckodriver version

Multiple Browser WebDriver Selenium

I am working on a Selenium test project where I need to launch two browsers at initial setup .
Then I need to do switching between these browsers.
So I will have [Window1] [Window2]
I would like to run test through [Window1] and then switch to [Window2] to check result of actions done in [Window1]
Any idea on how to do it?
I tried driver.switchTo().window() but no luck.
Any help would be greatly appreciated. Thanks
driver.switchTo().window() will work only if new window is opened by any action in existing window. If you are using different drivers to open different windows then it wont work.
In such case you need to choose appropriate instance of driver to control the new window.
Suppose you have instance of webdriver
// Window 1
WebDriver chrome = new ChromeDriver()
// Window 2
WebDriver firefox = new FirefoxDriver()
Now use chrome whenever you want to interact with Window 1 and use firefox to interact with Window 2.
Just use two driver instancess:
WebDriver driver1 = new ChromeDriver()
WebDriver driver2 = new FirefoxDriver()
You can make them both same flavour if you want.
You need to pass the parameter as window name or you can get all the window handles and then switch to the particular window handle.
You could use:
driver.switchTo().window("windowName");
or:
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}

Selenium - Block popup using firefox driver

I want to block popup in selenium using firefox driver.
has anyone done anything like this? i have had tried by loading extension but no success.
Can anyone help here?
Thanks!
Regards,
Kalpesh Patel
You need to use your browser profile and block popup there. Pass your profile as a parameter while creating instance of your browser. check the code below:
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("Your-profile-name");
profile.setPreference( "intl.accept_languages", "no,en-us,en" );
FirefoxDriver driver = new FirefoxDriver(profile);
driver.get("http://www.example.com/");
Note: I consider that you already know the concept of profile in browser.

How could I start a Selenium browser(like Firefox) minimized?

How could I start a Selenium browser (like Firefox) minimized? I want the command browser.start(mySettings) to start a browser minimized.
I have an alternate solution that may meet your needs. You can set the position of the WebDriver to be outside of your view. That way, it'll be out of sight while it runs. (It may start at a visible location, but it will only be there for an instant.)
FirefoxDriver driver = new FirefoxDriver();
driver.manage().window().setPosition(new Point(-2000, 0));
Your question does not say that why you want to run your test cases in minimized browser but unfortunately selenium do not provide any built-in function for the same.
Normally when we want to run test cases with maximized browser we use driver.manage().window().maximize();
No doubt there are several ways to minimize your window through code by using Java key event by using keyboard shortcuts for minimimzing window or by using JavaScriptExecuter but that too depend on which OS and language you are working.
One more thing you can try is HtmlUnitDriver.By using this you cant even see the browser, so that may also serve your purpose if you have a case of not opening the browser while execution of test cases.
Dimension windowMinSize = new Dimension(100,100);
driver.manage().window().setSize(windowMinSize);
For C# you can minimize window easily, also with a built in way.
See: https://www.selenium.dev/documentation/en/webdriver/browser_manipulation
Screenshot:
Without knowing your motive for minimizing the browser and assuming that you are using the WebDriver drivers (Selenium v2) and don't want a UI to pop up, one could use the lightweight browser HtmlUnitDriver.
After you define the driver
driver = webdriver.Chrome(r"FULL PATH TO YOUR CHROMEDRIVER")
Do
driver.minimize_window()
And then call the site
driver.get(r'SITE YOU WANT TO SELECT')
It will minimize.
When Using Python to Move the FireFox Browser off screen:
driver = webdriver.FireFox()
driver.set_window_position(-2000,0)
driver.set_window_position(2000,2000)
or
(x,y) values more than monitor resolution
Later if u want to see the window again
driver.maximize_window()
The workarounds mentioned in the post did not work for NodeWebKit browser, so as a workaround i had to use native C# code as mentioned below:
public static void MinimiseNWKBrowser(IWebDriver d)
{
var body = UICommon.GetElement(By.TagName("body"), d);
body.Click();
string alt = "%";
string space = " ";
string down = "{DOWN}";
string enter = "{ENTER}";
SendKeys.SendWait(alt + space);
for(var i = 1; i <= 5; i++)
{
SendKeys.SendWait(down);
}
SendKeys.SendWait(enter);
}
So this workaround basically uses "ALT+SPACE" to bring up the browser action menu to select "MINIMIZE" from the options and presses "ENTER"
In php we can use JavaScript command to minimize the browser window.
$this->selenium->getEval("Minimize();");
and similar command for java :
browser.getEval("Minimize();");
The best way to minimize your browser is to use shortcut using Robot class.
Shortcut: Alt+Space+N (for Windows)
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_N);
By using above code you can minimize your browser.
Selenium doesn't have a built-in method to minimize the Browsing Context. Minimizing the browser while the Test Execution is In Progress would be against the best practices as Selenium may loose the focus over the Browsing Context and an exception may raise during the Test Execution. You can find a relevant detailed discussion in How to execute tests with selenium webdriver while browser is minimized
However, to mimic the functionality of minimizing the Browsing Context you can use the following steps:
Set the dimension of the Browsing Context to [0, 0]
Set the position of the top left corner of the Browsing Context just below the Viewport
Code Block (Java):
driver.navigate().to("https://www.google.com/");
Point p = driver.manage().window().getPosition();
Dimension d = driver.manage().window().getSize();
driver.manage().window().setSize(new Dimension(0,0));
driver.manage().window().setPosition(new Point((d.getHeight()-p.getX()), (d.getWidth()-p.getY())));
You can use:
driver.manage().window().maximize();
For example code snippet with chrome driver:
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
driver = new ChromeDriver();
baseUrl = "chrome://newtab/";
driver.manage().window().maximize().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
for Firefox use just "firefordriver.exe"
driver.manage().window().minimize();
This should help minimize the window. You can also use "maximize" in place of "minimize" to maximize the window.