Multiple Browser WebDriver Selenium - 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);
}

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);

Can WebDriver replace ChromeDriver in order to make Selenium tests work in all browsers

I'm automating my tests using TestNG and Java:
Can WebDriver replace ChromeDriver in order to make our tests work in all browsers such as Chrome, Firefox, Mozilla, Safari, Opera ... ?
How should we configure the browser so as to use the same code for all browsers?
Essentially, you just new to instantiate a different class derived from RemoteWebDriver depending on the browser you are testing.
e.g.
void GetWebDriver(String browserName) {
if (CHROME.equals(browserName))
return new ChromeDriver(capability);
else if (FIREFOX.equals(browserName))
return new FirefoxDriver(capability);
else if (EDGE.equals(browserName))
return new EdgeDriver(capability);
else if (INTERNET_EXPLORER.equals(browserName))
return new InternetExplorerDriver(capability);
else if (OPERA.equals(browserName))
return new OperaDriver(capability);
else if (SAFARI.equals(browserName))
return new SafariDriver(capability);
}
I suggest you look into this githob project: https://github.com/sebarmeli/Selenium2-Java-QuickStart-Archetype
Specifically, the WebDriverFactory.java file.
The easiest way to run your code in different browsers to use Selenium Grid and RemoteWebDriver. You can find the doc on the following link:
https://github.com/SeleniumHQ/selenium/wiki/Grid2

Issue with WebDriver test case

I have written a test case using WebDriver, I closed the browser in one method and again I am opening the browser not able to invoke
driver.close();i closed the browser through above command for again opening a browser i driver.get(url)
but i am getting error
'Error communicating with the remote browser It may have died'
If you really want to close the browser before navigating to the new URL, then do:
driver.quit();
driver = new FirefoxDriver();
driver.get(url);
However, why would you want to close the browser before navigating to the new URL in the first place?
I can think of either one of two reasons:
You want to go to the next URL with your browser's history cleaned.
If this is indeed the case, then deleteAllCookies will do the job.
You cannot go to the next URL because some popup alert is preventing you doing it.
If this is indeed the case, then neither close not quit will do the job.
Well you killed the browser with your driver.close();
In order to use the driver you must create a new one using something like driver = new FirefoxDriver(capabilities);
Moving to answer from comment.
I think I got your issue. Though you have called the close() and it only should close the current window, you should use this if you want to shift between multiple windows.
For your case just don't close the driver it will use the same window to open the url.
You have to use following way if you want to close the current browser and open
a new one:
webDriver.Close();
//Goto the Target website
WebDriver.Navigate().GoToUrl("url");
And you can use following way if you want to close the browser and kill the
web driver:
webDriver.Close();
webDriver.Quit();

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.