0
0
Selenium Javatesting~20 mins

Element dimensions and location in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Element Dimensions & Location Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of getSize() method?
Consider the following Selenium Java code snippet:

WebElement element = driver.findElement(By.id("sample"));
Dimension size = element.getSize();
System.out.println(size.getWidth() + "," + size.getHeight());

Assuming the element's width is 150 pixels and height is 75 pixels, what will be printed?
Selenium Java
WebElement element = driver.findElement(By.id("sample"));
Dimension size = element.getSize();
System.out.println(size.getWidth() + "," + size.getHeight());
A150,75
B75,150
CSize(width=150, height=75)
DWidth:150 Height:75
Attempts:
2 left
💡 Hint
getWidth() returns the width in pixels, getHeight() returns the height in pixels.
assertion
intermediate
2:00remaining
Which assertion correctly verifies element location?
You want to verify that a web element is located at x=200 and y=100 on the page. Which assertion correctly checks this using Selenium Java?
Selenium Java
WebElement element = driver.findElement(By.id("locationElement"));
Point location = element.getLocation();
AassertTrue(location.getX() == 100 && location.getY() == 200);
BassertEquals(200, location.getX()); assertEquals(100, location.getY());
CassertEquals(new Point(100, 200), location);
DassertNotNull(location);
Attempts:
2 left
💡 Hint
Check the x and y coordinates separately with expected values.
locator
advanced
2:00remaining
Which locator best ensures stable element location for dimension checks?
You need to locate a button whose position and size you want to test. The button has dynamic classes but a stable aria-label attribute 'Submit Form'. Which locator is best for stable element dimension and location testing?
ABy.className("btn-primary")
BBy.xpath("//button[contains(@class, 'btn')]")
CBy.cssSelector("button[aria-label='Submit Form']")
DBy.id("submitBtn")
Attempts:
2 left
💡 Hint
Choose locator that is stable and unique for the element.
🔧 Debug
advanced
2:00remaining
Why does getLocation() return (0,0) unexpectedly?
You run this code:

WebElement element = driver.findElement(By.id("hiddenElement"));
Point location = element.getLocation();
System.out.println(location.getX() + "," + location.getY());

The output is '0,0' but you expect different coordinates. What is the most likely reason?
AThe driver has a bug and always returns (0,0) for getLocation().
BThe element's id is incorrect, so getLocation() returns (0,0).
CThe element is not visible or is hidden, so location defaults to (0,0).
DThe element is inside an iframe and getLocation() returns (0,0) without switching frames.
Attempts:
2 left
💡 Hint
Check if the element is inside an iframe or frame.
framework
expert
3:00remaining
How to design a reusable test method for verifying element size and location?
You want to create a reusable Java method in your Selenium test framework that verifies an element's width, height, x, and y coordinates against expected values. Which method signature and assertion approach is best?
A
public void verifyElementDimensions(WebElement element, int expectedWidth, int expectedHeight, int expectedX, int expectedY) {
  Dimension size = element.getSize();
  Point location = element.getLocation();
  assertEquals(expectedWidth, size.getWidth());
  assertEquals(expectedHeight, size.getHeight());
  assertEquals(expectedX, location.getX());
  assertEquals(expectedY, location.getY());
}
B
public boolean verifyElementDimensions(WebElement element, int width, int height, int x, int y) {
  return element.getSize().getWidth() == width && element.getSize().getHeight() == height && element.getLocation().getX() == x && element.getLocation().getY() == y;
}
C
public void verifyElementDimensions(WebElement element, Dimension expectedSize, Point expectedLocation) {
  assertTrue(element.getSize().equals(expectedSize) && element.getLocation().equals(expectedLocation));
}
D
public void verifyElementDimensions(WebElement element, int width, int height) {
  Dimension size = element.getSize();
  assertEquals(width, size.getWidth());
  assertEquals(height, size.getHeight());
}
Attempts:
2 left
💡 Hint
Include all four parameters and assert each separately for clear failure messages.