0

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?

Django_Tester
  • 139
  • 1
  • 3
  • 16
  • You may have some luck with either 1) computing yourself whether the coordinates of the element are within the view window: `WebDriver.Window.getSize()` and `WebDriver.Window.getPosition()` should give you the frame of reference. Then, you can get the element location and see whether it's within the view with `WebElement.getLocation()`. Or, 2) try a MouseMoveAction onto the element. You should end up with a `MoveTargetOutOfBoundsException` if you go outside the window. – Eric Hughes Mar 04 '15 at 14:07
  • While #2 above might work, the Window indicates the outer periphery of the window, not the view. If your driver is a `JavascriptExecutor`, you may be able to use [this solution](http://stackoverflow.com/a/7557433/4406558) – Eric Hughes Mar 04 '15 at 14:19

0 Answers0