Element dimensions and location in Selenium Java - Build an Automation Script
import org.openqa.selenium.By; import org.openqa.selenium.Dimension; import org.openqa.selenium.Point; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; import java.time.Duration; public class ElementDimensionsLocationTest { private WebDriver driver; private WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void testElementDimensionsAndLocation() { driver.get("https://example.com/login"); // Wait for username input to be visible WebElement usernameInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))); WebElement passwordInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password"))); WebElement loginButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("loginBtn"))); // Get dimensions of username input Dimension usernameSize = usernameInput.getSize(); int width = usernameSize.getWidth(); int height = usernameSize.getHeight(); // Get location of login button Point loginLocation = loginButton.getLocation(); int x = loginLocation.getX(); int y = loginLocation.getY(); // Assertions assertEquals(250, width, "Username input width should be 250 pixels"); assertEquals(40, height, "Username input height should be 40 pixels"); assertTrue(x > 100, "Login button x coordinate should be greater than 100"); assertTrue(y > 200, "Login button y coordinate should be greater than 200"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
This test opens the login page URL using ChromeDriver.
It waits explicitly for the username input, password input, and login button to be visible to avoid timing issues.
Then it gets the size (width and height) of the username input field using getSize() and the location (x and y coordinates) of the login button using getLocation().
Assertions check that the username input width is exactly 250 pixels and height is 40 pixels, matching the expected design.
It also asserts that the login button's x coordinate is greater than 100 and y coordinate is greater than 200, ensuring it is positioned correctly on the page.
The test uses JUnit 5 assertions for clear pass/fail results.
Finally, the @AfterEach method closes the browser to clean up.
Now add data-driven testing with 3 different login page URLs to verify element dimensions and location on each.
Practice
Solution
Step 1: Understand the purpose of
This method returns the coordinates (x, y) of the element on the page.getLocation()Step 2: Compare with other methods
getSize()returns width and height,getText()returns visible text, andgetAttribute()returns attribute values.Final Answer:
getLocation() -> Option DQuick Check:
Position = getLocation() [OK]
- Confusing getLocation() with getSize()
- Using getText() to find position
- Expecting getAttribute() to return coordinates
element?Solution
Step 1: Identify method to get size
getSize()returns a Dimension object with width and height.Step 2: Get width from Dimension
UsegetWidth()on the Dimension object to get the width.Final Answer:
int width = element.getSize().getWidth(); -> Option CQuick Check:
Width = getSize().getWidth() [OK]
- Using getLocation() to get width
- Mixing width and height methods
- Calling getHeight() when width is needed
Point location = element.getLocation();
Dimension size = element.getSize();
System.out.println("X: " + location.getX() + ", Y: " + location.getY());
System.out.println("Width: " + size.getWidth() + ", Height: " + size.getHeight());Solution
Step 1: Understand getLocation() output
It returns a Point with x=50 and y=100 as given.Step 2: Understand getSize() output
It returns Dimension with width=200 and height=150 as given.Final Answer:
X: 50, Y: 100 Width: 200, Height: 150 -> Option AQuick Check:
Location and size match output [OK]
- Swapping x and y values
- Swapping width and height values
- Assuming default (0,0) location
element:
int height = element.getLocation().getHeight();
Solution
Step 1: Check getLocation() return type
It returns a Point object which has getX() and getY(), but no getHeight().Step 2: Correct method to get height
Height is obtained fromelement.getSize().getHeight(), not from getLocation().Final Answer:
getLocation() does not have getHeight() method -> Option AQuick Check:
Height = getSize().getHeight() [OK]
- Calling getHeight() on Point object
- Confusing getWidth() and getHeight()
- Assuming getLocation() returns size info
button element?Solution
Step 1: Check button's top-left corner inside viewport
Ensure X and Y coordinates are >= 0.Step 2: Check button's bottom-right corner inside viewport
Sum of X + width must be <= viewport width (1024), and Y + height <= viewport height (768).Final Answer:
button.getLocation().getX() >= 0 && button.getLocation().getY() >= 0 && button.getLocation().getX() + button.getSize().getWidth() <= 1024 && button.getLocation().getY() + button.getSize().getHeight() <= 768 -> Option BQuick Check:
Check corners inside viewport using location + size [OK]
- Checking only size without location
- Using OR instead of AND in conditions
- Ignoring viewport boundaries
Ready to prove it?
Pass the test and this pattern moves unknown
