Selenium Element Location

Go To StackoverFlow.com

2

Is there an easy way to find a child element from another element (both of these items have been located using a PageFactory)? We have a set of containers which hold many modules, and I'd like to ensure that they are displaying in their proper locations.

The API only seems to have the following method:

webElement.findElement(s).(By by);

Is there an easy way to do the following:

webElement.findElement(s)(WebElement webElement);

or even better:

webElement.contains(WebElement webElement);
2012-04-04 20:17
by Scott
I can't really figure out what are you trying to do. Could you please give an example? From what I udnerstood, the element.findElement(s)(By by) should be good enough even for your needs. What am I getting wrong - Petr Janeček 2012-04-04 20:45
I would prefer not to utilize reflection again to pull the FindBy annotation off of the fields. I have two elements which have already been initialized with a custom PageFactory. We have many different modules which can be created and added to various pages. I don't want to modify each of these tests whenever the pages are changed in the CMS, as such I'm self-locating these modules and am now wanting to verify that they exist in the proper locations on the screen (i.e. I want to ensure that Module A is in Container A or B, but not C) - Scott 2012-04-04 20:56


2

I finally got what you need. My best solution would be something in the way of:

public static void assertContains(WebElement outerElem, WebElement innerElem) {
    // get borders of outer element
    Point outerLoc = outerElem.getLocation();
    Dimension outerDim = outerElem.getSize();
    int outerLeftX = outerLoc.getX();
    int outerRightX = outerLeftX + outerDim.getWidth();
    int outerTopY = outerLoc.getY();
    int outerBottomY = outerTopY + outerDim.getHeight();

    // get borders of inner element
    Point innerLoc = innerElem.getLocation();
    Dimension innerDim = innerElem.getSize();
    int innerLeftX = innerLoc.getX();
    int innerRightX = innerLeftX + innerDim.getWidth();
    int innerTopY = innerLoc.getY();
    int innerBottomY = innerTopY + innerDim.getHeight();

    // assures the inner borders don't cross the outer borders
    final String errorMsg = "ughh, some error message";
    final boolean contains = (outerLeftX <= innerLeftX)
            && (innerRightX <= outerRightX)
            && (outerTopY <= innerTopY)
            && (innerBottomY <= outerBottomY);
    assertTrue(errorMsg, contains);
}

...works only if none of those containers overlap. If they do, I'd try some dark and wild magic with innerElem.getTag() and getText() and test whether the outer text contains the inner element. One way of doing it:

public static void assertContains(WebElement outer, WebElement inner) {
    // e.g. //div[text()='some text in inner element']
    final String findInner = ".//" + inner.getTagName() + "[text()='" + inner.getText() + "']";
    try {
        outerElem.findElement(By.xpath(findInner));
    } catch (NoSuchElementException ignored) {
        fail("Some horrible message! We are all doomed!");
    }
    // passed
}

...or something similar. It is possible that normalize-space() or contains() (or both) will be needed due to tags in tags in tags in...

And, of course, this is still guessing. The first method may have false positives and negatives (but I like it more given my needs), the second should only have false positives. You can combine them or invent something yourself (e.g. using Reflection as you have already stated).

Or file a bug.

2012-04-04 21:35
by Petr Janeček
While I'm not necessarily in love with this method, it does offer the benefit in that we should be able to determine if browsers are rendering the HTML in a similar fashion to one another, however that's a double-edged sword as well, as the HTML may in fact be a child of the element, but not within it's boundaries if we get a hosed result from the driver due to rendering issues - Scott 2012-04-05 12:58
Yeah, I know it's not the thing we actually would like to have =/. Also, I edited the answer to be more useful for future readers (as you seemed pretty much capable yourself), by the way - Petr Janeček 2012-04-05 18:06
Looks good, I'm more than likely just going to work on a custom locator, as I think it will be a bit cleaner. I'm also going to post this on the selenium google code repository as a feature request - Scott 2012-04-05 18:15
Or that. Funny, I haven't thought about that at all - Petr Janeček 2012-04-05 19:35
In case you were curious, I passed in the parent element to the custom ElementLocatorFactory we created and catch the exceptions if not found in that particular WebElement (and filed issue 3682) - Scott 2012-04-19 19:50
Ads