Is there any way to minimize chrome window in Selenium? - java

Something like driver.manage().window().maximize(); but for minimize the window.
Thanks!

Selenium doesn't have minimize() option, atleast not for Java, however you can use setPosition do do it
driver.manage().window().setPosition(new Point(0, 0));
However the better way is to run it as headless browser
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
WebDriver driver = new ChromeDriver(options);
This way you can use maximized browser while it's running in the background.

Selenium's java client have no built-in method for minimizing the browser. Ideally, you shouldn't minimize the browser while the Test Execution is In Progress as Selenium would loose the focus over the Browsing Context and an exception will be raised at any point of time which will halt 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 solution:
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 set the position of the WebDriver outside of your view. That way, it'll be out of sight while it runs.
FirefoxDriver driver = new FirefoxDriver();
driver.manage().window().setPosition(new Point(-2000, 0));
OR
Dimension windowMinSize = new Dimension(100,100);
driver.manage().window().setSize(windowMinSize);

Use below code to completely minimize it.
driver.manage().window().setPosition(new Point(-2000, 0))

Related

Chrome Browser window is not maximize in Selenium-grid node

Windows are not maximize issue occurs only in grid node.
we tried the different methods to get it resolved as shown below, but nothing has worked
Dimension dimension = new Dimension(1920,1080)
driver.manage().window().setSize(dimension);
driver.manage().window().maximize()
options.addArguments("--window-size=1920,1080");
options.addArguments("--start-maximized");
NOTE: No issue observed when we run the scripts locally , or if i logged into that node when the scripts is running, but when I close that node, and start the script, then window size would remain small and couldn't find some webElement
Maximize the browser with Java :
driver.manage().window().maximize();
Another way to do with Java:
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenResolution = new Dimension((int)
toolkit.getScreenSize().getWidth(), (int)
toolkit.getScreenSize().getHeight());
driver.manage().window().setSize(screenResolution);

how to maximise window size in htmlunit driver?

I am running selenium test using HtmlUnitDriver but at some point test keep failing. Is there any way we can maximize the window size in HtmlUnitDriver?
To maximize the window just use HtmlUnitWindow maximize()
HtmlUnitDriver htmlDriver = new HtmlUnitDriver();
htmlDriver.HtmlUnitWindow().maximize()

Selenium 2 + phantom js: unable to perform actions on a mouseover

I'm trying to perform an action on a button it's never done.
final Actions action = new Actions(mDriver);
final WebElement myCart = mDriver.findElement(By.cssSelector("path to my span"]"));
final WebElement myButton = mDriver.findElement(By.cssSelector("path to my button"));
action.moveToElement(myCart).build().perform();
action.moveToElement(myButton).click().build().perform();
This code works perfectly with firefox but not with phantom JS
I found some issue here How to handle Mouseover in Selenium 2 API or How to perform mouseover function in Selenium WebDriver using Java? but nothing work with phantom.
Is there any known workaround for this ?
Thanks!
I had similar issues when I used GhostDriver and PhantomJS around a year ago (FYI article). Actually I had problems with IE_Driver and Chrome_Driver too, mostly related with visibility of elements outside the screen_frame (page must be scrolled down).
One of most serious issues was the upload_window and handle it through already-mentioned. I wasn't able to achieve it doh. But my workaround was to switch/cast the driver on these problematic places and after they complete/handle the operation - switch it back to GhostDriver. Even by doing so, the execution speed was impressive.
Hope this helps - even late given.
Update:
find IWebElement to process
set WebDriver from GhostDriver to FirefoxDriver
process the IWebElement item with current WebDriver as FirefoxDriver
verify expected result from processing the IWebElement item
set back WebDriver from FirefoxDriver to GhostDriver
continue workflow
As far as I remember my Test framework implementation - a BaseTest class takes care of initialization of used WebDriver and ISelenium objects. So for your more specific case, you can try this:
// Create a new instance of the Ghost driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new GhostDriver();
//do stuff until new driver is needed
driver = new FirefoxDriver();
//do stuff with new driver
//'cast' back after required operations have been completed and verified
driver = new GhostDriver();

How to resize current browser window in Selenium WebDriver with Java?

I need to resize the browser as 300x400 during executing the Selenium automated tests. How can I resize the browser window in Selenium WebDriver (aka, Selenium 2) with Java?
[Note: It needs to resize the browser window for testing Responsive Web Design (RWD)]
You can get a reference to the current window with driver.manage().window(). And the window has a setSize() method, so you could try
Dimension dimension = new Dimension(800, 600);
driver.manage().window().setSize(dimension)
For Responsive Design it's better if you don't use fixed data, in this case screen resolution - users need most often (cause it's much less prone to errors) :
_driver.manage().window().maximize();
or
_driver.manage().window().fullScreen();
If nothing works (to make tab in full screen only) for you, go for it:
driver.maximize_window()
where
driver = webdriver.Chrome(executable_path="D:\softwares\chromedriver.exe")
or could be any other webdriver locations.
We can also use Chrome capability to resize the window.
ChromeOptions options = new ChromeOptions();
options.addArguments("window-size=800,480");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, options);

Selenium web driver | java | unable to switch between firefox browser windows

Using Selenium Webdriver 2. java.
I would like to switch back in forth between two firefox browser windows. When I do I get: org.openqa.selenium.NoSuchWindoException: Unable to loacate window"{accb1cc2-74c9-3b4e-8f71-c0b184a037c4}"; duration or timeout:
Here is the java:
driver = new FirefoxDriver();
driver.get("http://mail.google.com");
String firstWindowHandle = driver.getWindowHandle();
System.out.println("handle of first window ="+firstWindowHandle);
Thread.sleep(1000);
driver = new FirefoxDriver();
driver.get("http://www.google.com");
// Get names of currently open windows
String secondWindowHandle = driver.getWindowHandle();
System.out.println("handle of first window ="+secondWindowHandle);
Thread.sleep(1000);
// It fails right here!
driver.switchTo().window(firstWindowHandle );
driver.get("http://www.lifehacker.com");
It prints the following to the console:
- handle of first window = {accb1cc2-74c9-3b4e-8f71-c0b184a037c4}
- handle of the second window = {f5256619-a36e-a441-9979-937da0abacd1}
All help is appreciated.
Unfortunately, you cannot switch between windows the way you are currently trying to do it - WebDriver lost the first window as soon as you instantiated a new instance.
You could try opening the second window via javascript and then switching back and forth from it:
window.open('http://www.bing.com','Bing','modal=yes,alwaysRaised=yes')
This is a bit of a hack, and could have the following problems:
Popup blockers may prevent the action
The browser must have javascript enabled
Future browser versions may break the hack
Complaining and murmuring from peers (and perhaps rightly so) because even though it might work, it's still a hack ;)
Some final thoughts:
Is there any particular reason it has to be the same driver instance?
If not, just switch between two driver instances:
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://mail.google.com");
FirefoxDriver driver2 = new FirefoxDriver();
driver2.get("http://www.google.com");
Swtiching between 2 active Windows:
FirefoxDriver wd=new FirefoxDriver();
wd.get("https://irctc.co.in/");
wd.manage().timeouts().implicitlyWait(5000,TimeUnit.SECONDS);
WebElement wb=wd.findElement(By.linkText("Cabs"));
wb.click(); //Now 2 Windows are open
wd.manage().timeouts().implicitlyWait(5000,TimeUnit.SECONDS); //Wait for the complete page to load
Set<String> sid=wd.getWindowHandles(); //getWindowHandles() method returns the ids of all active Windows and its return type will be a Collection Set.
Iterator<String> it=sid.iterator(); //Using iterator we can fetch the values from Set.
String parentId=it.next();
System.out.println(parentId);
String childId=it.next();
System.out.println(childId);
wd.switchTo().window(childId); //swtiching control to child Window
wd.close(); //control returns to the parent Window.

Categories

Resources