This test opens a webpage, finds an element by its ID, prints its position and size, and checks if the element has a valid size.
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;
public class ElementDimensionsLocationTest {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
try {
driver.get("https://example.com");
WebElement element = driver.findElement(By.id("exampleElement"));
Point location = element.getLocation();
Dimension size = element.getSize();
System.out.println("Element location: X=" + location.getX() + ", Y=" + location.getY());
System.out.println("Element size: Width=" + size.getWidth() + ", Height=" + size.getHeight());
// Simple assertion
if (size.getWidth() > 0 && size.getHeight() > 0) {
System.out.println("Test Passed: Element has valid size.");
} else {
System.out.println("Test Failed: Element size is invalid.");
}
} finally {
driver.quit();
}
}
}