Sometimes you need to set window position and size or get window size and position In selenium software test. Selenium webdriver software testing tool has many useful methods using which we can play with web browser window for different purpose. One of them Is window().maximize(); which we use In each and every
webdriver software test to maximizing the window. Same way, webdriver has window size and position related Independent methods.
What Is window size and position?
Before learning how to set/get size and position, We need to understand It.
- Window size means height and width of window.
- Window position means distance of window from left side(X Coordinates) of screen and top side(Y Coordinates) of screen.
Bellow given Image describes you more about window size and position.
window().setSize() to set window size
We can use window().setSize() method to set the size of window In selenium webdriver software test. We can set window width to 300 and height to 500 dimensions using bellow given syntax In selenium webdriver.
driver.manage().window().setSize(new Dimension(300,500));
window().getSize() to get window size
We can use window().getSize() method to get size of window.
Bellow given syntax will return window height using getSize().getHeight().
driver.manage().window().getSize().getHeight()
Bellow given syntax will return window width using getSize().getWidth().
driver.manage().window().getSize().getWidth()
window().setPosition() to set position of window
We can set window position to 50 points from left side and 200 points from top side using bellow given syntax.
driver.manage().window().setPosition(new Point(50,200));
window().getPosition() to get position of window
Use window().getPosition().getX() to get window position from left side as bellow.
driver.manage().window().getPosition().getX()
To get window position from top side of screen, use window().getPosition().getY() In your software test as bellow.
driver.manage().window().getPosition().getY()
Full webdriver test to explore all above methods of webdriver window Is as bellow.
package Testing_Pack;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class WindowSizePosition {
WebDriver driver;
@BeforeTest
public void setup() throws Exception {
driver = new FirefoxDriver();
driver.manage().window().maximize();
}
@Test(priority=1)
public void setGetWinSize(){
//WebDriver setSize method used to set window size width = 300 and height = 500.
driver.manage().window().setSize(new Dimension(300,500));
//WebDriver getSize method used to get window width and height.
System.out.println("Window height Is -> "+driver.manage().window().getSize().getHeight());
System.out.println("Window width Is -> "+driver.manage().window().getSize().getWidth());
}
@Test(priority=2)
public void setGetWinPosition(){
//WebDriver setPosition method used to set window position x coordinate = 50 and y coordinate = 100.
driver.manage().window().setPosition(new Point(50,200));
//WebDriver getPosition method used to get window position x,y coordinates.
System.out.println("Window position X coordinates Is -> "+driver.manage().window().getPosition().getX());
System.out.println("Window position Y coordinates Is -> "+driver.manage().window().getPosition().getY());
}
}
This way you can re-size/set position of your webdriver window during test execution.
It is very clear to understand even if they don't know abcd in selenium..
ReplyDeleteEven if "they"?
DeleteThank you for saving my life :D I have been banging my head all day! Tried to change browser size through Xvfb, didn't work. This did.
ReplyDeletequite useful information. thanks.
ReplyDelete