I've read many of other stackoverflow questions, WebDriver docs and more and I still do not have the answer to my question. What is the best approach to verify an element is displayed on the screen to an end user? Not what is visible in the DOM, but what the user can see is in focus.
I've seen .isDisplayed()
- is what is visible in the DOM, .moveToElement()
- moves the mouse, .switchTo().activeElement()
visibilityOf()
all mentioned, but none are really true to what I want to test against.
I have several elements on a page (only two are in view on screen). I want to assert that the page does not jump down to the bottom of the page upon loading, and is, in fact, focused at the top of the page. Then I will have a line in my code that jumps to the element positioned on the bottom of the page (essentially scrolling into view on the screen), and verify this is in focus.
I've attempted various approaches:
WebElement moduleThree = driver.findElement(By.xpath("xpathExpression"));
moduleThree.equals(driver.switchTo().activeElement());
WebElement moduleTwo = driver.findElement(By.xpath("xpathExpression"));
moduleTwo.isDisplayed();
driver.switchTo().activeElement().findElement(By.xpath("xpathExpression"));
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("Document.getElementById('Id').focus();");
I'm attempting to fail the test when running the code and all I get back is a pass each time. Wuhoo.. great..! Well, not so great as its not what I'm attempting to verify.
Does anyone know how to verify an element is visibly in focus on screen, or is this even possible in WebDriver?
EDIT
I have since used the getLocation() method to find the coordinates of my web elements. ModuleTwo is at the bottom of the page out of view. ModuleThree is in view when the page loads.
int XaxisModuleTwo = moduleTwo.getLocation().getX();
int YaxisModuleTwo = moduleTwo.getLocation().getY();
How can I verify that this element is, at first, out of view?