对于低估,我使用了this
另外,@FindBys有AND条件,而@FindAll有OR条件,我试过下面的代码,
@FindBys(
{
@FindBy(name="username"),
@FindBy(name="password")
}
)
List<WebElement> totWeAnd;
@FindAll(
{
@FindBy(name="username"),
@FindBy(name="password")
}
)
List<WebElement> totWeOr;
正如预期的那样,totWeAnd返回了0个WebElements,而totWeOr返回了2个WebElements。 但是使用 FinBy(xpath =“”)可以实现上述相同的事情,
@FindBy(xpath="//input[@name='username' and @name='password']")
List<WebElement> totWE_And;
@FindBy(xpath="//input[@name='username' or @name='password']")
List<WebElement> totWE_OR;
那么@ FindBys,@ FindAll的用途是什么?我们什么时候应该使用它?
答案 0 :(得分:0)
文章中的逻辑不正确。每个FindBy mentioned in the [FindBys][1] or [FindAll][1] annotation
指的是与标准匹配的特定元素,而不是“&#39}”。和&#39; AND&#39;标准的组合。
@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类。请参阅元素变量之前的注释。
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。查询页面的次数越多,性能也越好。