我终于得到了你需要的东西。我最好的解决方案是:
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);
}
...仅当这些容器都不重叠时才有效。如果他们这样做,我会尝试一些黑暗和狂野的魔法,innerElem.getTag()
并getText()
测试外部文本是否包含内部元素。一种方法:
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
}
...或类似的东西。可能需要normalize-space()或contains()(或两者),因为...
当然,这仍然是猜测。第一种方法可能有误报和否定(但鉴于我的需要,我更喜欢它),第二种方法应该只有误报。您可以将它们结合起来或自己发明一些东西(例如使用Reflection
您已经说过的)。
或者提交一个错误。