Challenge - 5 Problems
Element Dimensions & Location Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of getSize() method?
Consider the following Selenium Java code snippet:
Assuming the element's width is 150 pixels and height is 75 pixels, what will be printed?
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());
Attempts:
2 left
💡 Hint
getWidth() returns the width in pixels, getHeight() returns the height in pixels.
✗ Incorrect
The getSize() method returns a Dimension object. Calling getWidth() and getHeight() returns integer values for width and height respectively. The code concatenates them with a comma, so the output is '150,75'.
❓ assertion
intermediate2: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();Attempts:
2 left
💡 Hint
Check the x and y coordinates separately with expected values.
✗ Incorrect
The element's x coordinate should be 200 and y coordinate 100. Option B correctly asserts these values separately. Option B swaps x and y. Option B swaps x and y in Point. Option B only checks non-null, not coordinates.
❓ locator
advanced2: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?
Attempts:
2 left
💡 Hint
Choose locator that is stable and unique for the element.
✗ Incorrect
Option C uses a stable attribute 'aria-label' which is less likely to change and uniquely identifies the button. Option C and C rely on classes which are dynamic. Option C assumes an id which may not exist.
🔧 Debug
advanced2:00remaining
Why does getLocation() return (0,0) unexpectedly?
You run this code:
The output is '0,0' but you expect different coordinates. What is the most likely reason?
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?
Attempts:
2 left
💡 Hint
Check if the element is inside an iframe or frame.
✗ Incorrect
If the element is inside an iframe, Selenium must switch to that iframe before accessing element properties. Without switching, getLocation() returns (0,0). Option D is incorrect because hidden elements still have location. Option D would cause NoSuchElementException. Option D is false.
❓ framework
expert3: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?
Attempts:
2 left
💡 Hint
Include all four parameters and assert each separately for clear failure messages.
✗ Incorrect
Option A provides a clear method with separate assertions for width, height, x, and y, making failures easy to diagnose. Option A returns boolean but does not assert, losing test reporting benefits. Option A uses equals on Dimension and Point which may be less clear and harder to debug. Option A ignores location coordinates.