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

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.

Related

Minimizing the browser window in selenium testing [duplicate]

After maximizing the window by driver.manage().window().maximize();, how do I minimize the browser window in Selenium WebDriver with Java?
Selenium's Java client doesn't have a built-in method to minimize the browsing context.
However, as the default/common practice is to open the browser in maximized mode, while Test Execution is in progress minimizing the browser would be against the best practices as Selenium may lose the focus over the browsing context and an exception may raise during the test execution. However, Selenium's Python client does have a minimize_window() method which eventually pushes the Chrome browsing context effectively to the background.
Sample code
Python:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.google.co.in')
driver.minimize_window()
Java:
driver.navigate().to("https://www.google.com/");
Point p = driver.manage().window().getPosition();
Dimension d = driver.manage().window().getSize();
driver.manage().window().setPosition(new Point((d.getHeight()-p.getX()), (d.getWidth()-p.getY())));
There seems to be a minimize function now:
From the documentation on:
webdriver.manage().window()
this.minimize() → Promise<undefined>
Minimizes the current window. The exact behavior of this command is specific to individual window managers, but typicallly involves hiding the window in the system tray.
Parameters
None.
Returns
Promise<undefined>
A promise that will be resolved when the command has completed.
So the code should be:
webdriver.manage().window().minimize()
At least in JavaScript.
Unfortunately, Selenium does not provide any built-in function for minimizing the browser window. There is only the function for maximizing the window. But there is some workaround for doing this.
driver.manage().window().setPosition(new Point(-2000, 0));
Use the following code to the minimize browser window. It worked for me and I am using Selenium 3.5:
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);
I'm using Selenium WebDriver (Java) 3.4.0 and have not found the minimize function either. Then I'm using the following code.
driver.manage().window().maximize();
driver.manage().window().setPosition(new Point(0, -2000));
We need to import the following for the Point function.
import org.openqa.selenium.Point;
For C# , tested using Selinium 3.14 ver ( Package1 , Package2) and it work
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Minimize();
In Selenium version 4, there is a method called minimize() added in Window interface (An inner interface of WebDriver) with Java.
Hence now, if you have Selenium version 4, then you can use driver.manage().window().minimize().
This works for me in Python.
The window will open and after loading it will minimize.
driver.get(url)
driver.minimize_window()
Try this. It should work.
Dimension n = new Dimension(360, 592);
driver.manage().window().setSize(n);

How to minimize the browser window in Selenium WebDriver 3

After maximizing the window by driver.manage().window().maximize();, how do I minimize the browser window in Selenium WebDriver with Java?
Selenium's Java client doesn't have a built-in method to minimize the browsing context.
However, as the default/common practice is to open the browser in maximized mode, while Test Execution is in progress minimizing the browser would be against the best practices as Selenium may lose the focus over the browsing context and an exception may raise during the test execution. However, Selenium's Python client does have a minimize_window() method which eventually pushes the Chrome browsing context effectively to the background.
Sample code
Python:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.google.co.in')
driver.minimize_window()
Java:
driver.navigate().to("https://www.google.com/");
Point p = driver.manage().window().getPosition();
Dimension d = driver.manage().window().getSize();
driver.manage().window().setPosition(new Point((d.getHeight()-p.getX()), (d.getWidth()-p.getY())));
There seems to be a minimize function now:
From the documentation on:
webdriver.manage().window()
this.minimize() → Promise<undefined>
Minimizes the current window. The exact behavior of this command is specific to individual window managers, but typicallly involves hiding the window in the system tray.
Parameters
None.
Returns
Promise<undefined>
A promise that will be resolved when the command has completed.
So the code should be:
webdriver.manage().window().minimize()
At least in JavaScript.
Unfortunately, Selenium does not provide any built-in function for minimizing the browser window. There is only the function for maximizing the window. But there is some workaround for doing this.
driver.manage().window().setPosition(new Point(-2000, 0));
Use the following code to the minimize browser window. It worked for me and I am using Selenium 3.5:
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);
I'm using Selenium WebDriver (Java) 3.4.0 and have not found the minimize function either. Then I'm using the following code.
driver.manage().window().maximize();
driver.manage().window().setPosition(new Point(0, -2000));
We need to import the following for the Point function.
import org.openqa.selenium.Point;
For C# , tested using Selinium 3.14 ver ( Package1 , Package2) and it work
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Minimize();
In Selenium version 4, there is a method called minimize() added in Window interface (An inner interface of WebDriver) with Java.
Hence now, if you have Selenium version 4, then you can use driver.manage().window().minimize().
This works for me in Python.
The window will open and after loading it will minimize.
driver.get(url)
driver.minimize_window()
Try this. It should work.
Dimension n = new Dimension(360, 592);
driver.manage().window().setSize(n);

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: Can't SendKeys() to an item that was below the visible window but was made visible by Click()

I have this problem with a text field that is visible at the time of the SendKeys. I'm using IEDriverServer.exe and C#.
Here's how I can reproduce the problem:
The text field in question is visible in the window but you have to scroll down to see it. To scroll down I click on the element using code like this:
var element = driver.FindElement(By.Xpath("…"));
element.Click();
This scrolls the window down and makes the text field visible.
But when I try to send text to now-visible window:
element.SendKeys("blah");
I get the exception:
When_applicant_enters_application.Should_be_instantly_approved_on_external threw exception: OpenQA.Selenium.ElementNotVisibleException: Element is not displayed
How can I fix or workaround this problem?
Selenium version: 2.32.1
OS: Windows 7
Browser: IE
Browser version: 9.0.15
I've written code demonstrating the problem and submitted it to the Selenium tech support volunteers.
The full discussion is at http://code.google.com/p/selenium/issues/detail?id=5620
but the take-home is:
// Doesn't work
// driver = new InternetExplorerDriver();
// driver.Navigate().GoToUrl(#"D:\CGY\selenium\Bug5620\Bug5620\Bug5620.htm");
// Works
// driver = new FirefoxDriver();
// driver.Navigate().GoToUrl(#"D:\CGY\selenium\Bug5620\Bug5620\Bug5620.htm");
// Works
driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl(#"http://localhost:8080/Bug5620/"); // Hosted on Tomcat
so there may be a problem that possibly involves IE, IE security settings, Visual Studio local servers and/or the IE Driver. This may not even be a code problem, but something that needs to be documented, since other people are apparently running into the problem.
I don't know where the problem is exactly but I do have a work-around at this point, which is to use the Firefox Driver.
Thanks for your help, Jim. If you find out a better way of dealing with the problem, please add an answer here for the other folks.

Selenium Webdriver hover not working

Selenium Webdriver 2.31.0
with Scala 2.9
Anyone know how to do a mouse hover in Firefox? I'm basically trying to hover over an element to display a tooltip.
This code fails to move the mouse over the element specified.
val webElement = webDriver.findElement(By.cssSelector(myElement.queryString))
val builder = new Actions(webDriver)
val hover = builder.moveToElement(webElement).build()
hover.perform()
I have also tried mouse events without success (as described here WebDriver mouseOver is not working properly with selenium grid)
This is somewhat anecdotal since I don't have an exact technical explanation, but I've experienced this in the past and have remedied by upgrading Selenium.
The first thing I check is to make sure my selenium is up to date. This includes dependencies, standalone-server and browser drivers (though, in this case, not applicable as Firefox is included with Selenium).
Another possible (and more probable) cause, more directly related to Firefox, is Firefox itself. It's been my experience that a Firefox update can, from time to time, break some selenium functions, particularly hovers. I've found that either upgrading selenium, or if no update has been released, downgrading Firefox will solve the problem.
I wish I had more detailed information to give you, but I'm still learning the finer details of this situation myself. If nothing else, I hope this points you in the right direction.
Since you havn't said you got any errors,
After build().perform(), provide a wait method say, Thread.sleep() for certain amount of time, since there are posibilities where mousehover performed in fraction of seconds and it may not be possible to see the tooltip.
Makesure the locator is correct (because you may point to someother locator which doesn't show up any tooltip)
Makesure you firefox supports the mousehover functionality
The code might resemble as same as your's, but give it a try(JAVA),
Actions builder = new Actions(driver);
WebElement we = driver.findElement(locator);
Actions perf= builder.moveToElement(we).build();
perf.perform();
Thread.sleep(1000);
You can look out the link for your ref : #firefox issue
As your issue is in Firefox, you may need to enable Native Events with webdriver, specifically
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver driver = new FirefoxDriver(profile);
I've had to do this to get drag and drop working in Firefox on Unix, although it worked with the same code on a Windows box.