I次の操作を行うための簡単な方法があります
webElement.findElement(s).(By by);
:
は
APIは、以下の方法を持っているようですついにあなたが必要なものを手に入れました。私の最善の解決策は、次のようなものです。
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()
で暗くて野生の魔法を試し、外側のテキストに内側の要素が含まれているかどうかをテストします。それを行う1つの方法:
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()(またはその両方)が必要です。最初の方法は偽陽性と陰性があるかもしれませんが(私はそれが私の必要性を考えればより好きです)、2番目の方法は偽陽性のみでなければなりません。それらを組み合わせたり、自分で作成したりすることができます(既に述べたようにReflection
を使用してください)。
またはfile a bugです。
あなたは何をしようとしているのか分かりません。あなたは例を挙げていただけますか?私が誇らしげに思ったことから、 'element.findElement(By By)'はあなたのニーズにあっても十分なはずです。私は何が間違っていますか? –
フィールドのFindBy注釈を取り除くためにリフレクションを再度使用しないことをお勧めします。私はカスタムPageFactoryで既に初期化されている2つの要素を持っています。私たちはさまざまなページを作成して追加することができる、さまざまなモジュールを用意しています。私はこれらのモジュールを自分自身で見つけて、画面上の適切な場所に存在することを確認したいので、CMSでページが変更されるたびにこれらのテストをそれぞれ変更する必要はありません(つまり、モジュールAがコンテナAまたはBにあることを保証するが、Cではない)。 – Scott