Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's not nice, but it can be done to some extent.</p> <p>For <strong>positioning</strong>, you can use WebElement's <a href="http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#getLocation%28%29" rel="nofollow"><code>getLocation()</code></a> (Java doc, but the same method exists in all Selenium bindings). Note that most browsers render slightly differently, so do not expect things to be pixel-perfect when working with older IE. Also, things might be positioned slightly differently when e.g. the first font defined in CSS was not found and an alternative was used. Don't rely heavily on this method. But if you'll be able to make your tests sane and your environment stable, it will work.</p> <p>For <strong>aligning</strong>, I wrote a simple Java method for <code>WebDriver</code> that asserts that an element is <em>visually inside</em> of another element.</p> <p>There should be no false negatives, but there could be some false positives in the case when the inner element is visually inside, but its (invisible) actual borders "peek out". I haven't bumped into this problem, however, in my real experience, since nice websites behave nicely and don't need such hacks :). Still, it's kinda hackish and Selenium wasn't designed for this type of work, so it might be harder to implement more complex checks.</p> <pre class="lang-java prettyprint-override"><code>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 &lt;= innerLeftX) &amp;&amp; (innerRightX &lt;= outerRightX) &amp;&amp; (outerTopY &lt;= innerTopY) &amp;&amp; (innerBottomY &lt;= outerBottomY); assertTrue(errorMsg, contains); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload