文章中的逻辑不正确。每个FindBy mentioned in the [FindBys][1] or [FindAll][1] annotation 指的是与标准匹配的特定元素,而不是标准的“或”和“与”组合。
@FindBys - 返回与每个 FindBy 返回的元素序列匹配的元素链。
@FindAll - 返回与每个 FindBy 返回的每个元素匹配的所有元素。
最简单的方法是使用一些代码进行测试。检查链接的源代码,尤其是findElements() 方法。
下面是一个用于测试的简单 HTML。
<div id='level1elem1' name='LEVEL1 ELEMENT1'>
<div id='level2elem1' name='LEVEL2 ELEMENT1'>
</div>
<div id='level2elem2' name='LEVEL2 ELEMENT2'>
</div>
</div>
PageObject 类。引用元素变量前的 cmets。
public class FindPageObject {
private WebDriver driver;
//This will match an element (id of level2elem1) inside a parent element (id of level1elem1).
//Refer to test - testFindBys.
//Return a POSITIVE match.
@FindBys({
@FindBy(id="level1elem1"),
@FindBy(id="level2elem1")
})
private WebElement singleFindBys;
//This NEEDS TO MATCH an element (id of level1elem1) inside a parent element (id of level2elem1).
//Refer to test - testFindBysRev.
//Return a NoSuchElementException as element heirarchy is not present.
@FindBys({
@FindBy(id="level2elem1"),
@FindBy(id="level1elem1")
})
private WebElement singleFindBysRev;
//This will match the first element that matches any of the 3 criterion.
//Refer to test - testFindByAll.
//Return a POSITIVE match of top most element (id of level1elem1).
@FindAll({
@FindBy(id="level1elem1"),
@FindBy(id="level2elem1"),
@FindBy(id="level2elem2")
})
private WebElement singleFindAllUI;
//This will match the first element that matches any of the 3 criterion.
//In this case the inner elemnts FindBy are placed first.
//Refer to test - testFindByAllRev.
//Return a POSITIVE match of top most element (id of level2elem2).
@FindAll({
@FindBy(id="level2elem2"),
@FindBy(id="level2elem1"),
@FindBy(id="level1elem1")
})
private WebElement singleFindAllUIRev;
//This will match all the elements for each criterion.
//Refer to test - testFindByAllMultiple.
//Return a POSITIVE match of a list of all the elements of size 3.
@FindAll({
@FindBy(id="level1elem1"),
@FindBy(id="level2elem1"),
@FindBy(id="level2elem2"),
})
private List<WebElement> multipleFindAll;
//This will match all the elements for each criterion.
//Element(id of level1elem1 & name LEVEL1 ELEMENT1) will be returned twice as it matches the first and last findby.
//Refer to test - testFindByAllMultipleDuplicate.
//Return a POSITIVE match of a list of all the elements of size 4.
@FindAll({
@FindBy(id="level1elem1"),
@FindBy(id="level2elem1"),
@FindBy(id="level2elem2"),
@FindBy(name="LEVEL1 ELEMENT1"),
})
private List<WebElement> multipleFindAllDuplicate;
//This will match all the elements that satisfy the xpath.
//Though it has 4 matchers returns only 3 elements.
//Refer to test - testXpathAlls.
//Return a POSITIVE match of a list of all the elements of size 3.
@FindBy(xpath="//div[@id='level1elem1' or @id='level2elem2' or @id='level2elem1' or @name='LEVEL1 ELEMENT1']")
private List<WebElement> xpathAlls;
public FindPageObject(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public String getSingleFindBys() {
return singleFindBys.getAttribute("name");
}
public String getSingleFindBysRev() {
return singleFindBysRev.getAttribute("name");
}
public String getSingleFindAll() {
return singleFindAllUI.getAttribute("name");
}
public String getSingleFindAllRev() {
return singleFindAllUIRev.getAttribute("name");
}
public int getSizeMultiple() {
return multipleFindAll.size();
}
public int getSizeMultipleDuplicate() {
return multipleFindAllDuplicate.size();
}
public int getSizeMultipleXpaths() {
return xpathAlls.size();
}
}
junit 测试类。
public class FindTest {
private static WebDriver driver;
@Test
public void testFindBys() {
FindPageObject fpo = new FindPageObject(driver);
assertEquals("LEVEL2 ELEMENT1", fpo.getSingleFindBys());
}
@Test
public void testFindBysRev() {
FindPageObject fpo = new FindPageObject(driver);
thrown.expect(NoSuchElementException.class);
fpo.getSingleFindBysRev();
}
@Test
public void testFindByAll(){
FindPageObject fpo = new FindPageObject(driver);
assertEquals("LEVEL1 ELEMENT1", fpo.getSingleFindAll());
}
@Test
public void testFindByAllRev(){
FindPageObject fpo = new FindPageObject(driver);
assertEquals("LEVEL2 ELEMENT2", fpo.getSingleFindAllRev());
}
@Test
public void testFindByAllMultiple(){
FindPageObject fpo = new FindPageObject(driver);
assertEquals(3, fpo.getSizeMultiple());
}
@Test
public void testFindByAllMultipleDuplicate(){
FindPageObject fpo = new FindPageObject(driver);
assertEquals(4, fpo.getSizeMultipleDuplicate());
}
@Test
public void testXpathAlls(){
FindPageObject fpo = new FindPageObject(driver);
assertEquals(3, fpo.getSizeMultipleXpaths());
}
@Rule
public ExpectedException thrown = ExpectedException.none();
@BeforeClass
public static void setUp() {
System.setProperty("webdriver.chrome.driver",
"Path to chromedriver");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
driver.get("Local path to html");
}
@AfterClass
public static void tear() {
driver.quit();
}
}
关于用法,从未使用过,因为创建 css 或 xpath 更容易。查询页面的次数越多,性能也越好。