webdriver页面工厂中@FindAll和@FindBys注释之间的区别

时间:2014-09-18 13:32:06

标签: java selenium-webdriver

请解释webdriver页面工厂概念中@FindAll和@FindBys注释之间的区别。

4 个答案:

答案 0 :(得分:17)

当我们有多个标准来识别一个或多个WebElement对象时,我们可以在这些情况下使用这些注释。

@FindBys:当所需的WebElement对象需要匹配所有给定条件时,请使用@FindBys注释

@FindAll:当需要的WebElement对象需要匹配至少一个给定条件时,使用@FindAll注释

用法:

@FindBys( {
   @FindBy(className = "class1")
   @FindBy(className = "class2")
} )
private List<WebElement> elementsWithBoth_class1ANDclass2;

这里列出的elementsWithBothclass1ANDclass2将包含满足两个条件的任何WebElement。

@FindAll({
   @FindBy(className = "class1")
   @FindBy(className = "class2")
})
private List<WebElement> elementsWithEither_class1ORclass2  

Here List elementsWithEither_class1ORclass2将包含满足任何一个条件的所有WebElement。

答案 1 :(得分:15)

@FindAll可以包含多个@FindBy,并会在单个列表中返回与任何@FindBy匹配的所有元素。

示例:

@FindAll({
@FindBy(id = "one"),
@FindBy(id = "two")
})
public List<WebElement> allElementsInList;

然而,

@FindBys将根据其中指定的@FindBy的方式返回元素。

 @FindBys({
    @FindBy(id = "one"),
    @FindBy(className = "two")
    })
    public List<WebElement> allElementsInList;

其中allElementsInList包含className="two"

id="one"的所有元素

答案 2 :(得分:7)

用简单的话说,@ FindBys在@FindBy之间有AND条件关系,而@FindAll有OR条件关系。

答案 3 :(得分:6)

查看JavaDocs

注释类型FindBys

@Retention(value=RUNTIME)
@Target(value={FIELD,TYPE})
public @interface FindBys

Used to mark a field on a Page Object to indicate that lookup should use a series of @FindBy tags in a chain as described in ByChained Eg:

 @FindBys({@FindBy(id = "foo"),
           @FindBy(className = "bar")})

注释类型FindAll

@Retention(value=RUNTIME)
@Target(value={FIELD,TYPE})
public @interface FindAll

Used to mark a field on a Page Object to indicate that lookup should use a series of @FindBy tags It will then search for all elements that match any of the FindBy criteria. Note that elements are not guaranteed to be in document order. Eg:

 @FindAll({@FindBy(how = How.ID, using = "foo"),
           @FindBy(className = "bar")})