Java webdriver页面工厂中@FindAll和@FindBys注解的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25914156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:29:03  来源:igfitidea点击:

difference between @FindAll and @FindBys annotations in webdriver page factory

javaselenium-webdriver

提问by Vikas

Please explain the difference between @FindAll and @FindBys annotations in webdriver page factory concept.

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

采纳答案by san1deep2set3hi

We can use these annotations in those cases when we have more than a single criteria to to identify one or more WebElement objects.

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

@FindBys :When the required WebElement objects need to match all of the given criteria use @FindBys annotation

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

@FindAll :When required WebElement objects need to match at least one of the given criteria use @FindAll annotation

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

Usage:

用法:

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

Here List elementsWithBothclass1ANDclass2 will contain any WebElement which satisfies both criteria.

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

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

Here List elementsWithEither_class1ORclass2 will contain all those WebElement that satisfies any one of the criteria.

这里的 List elementsWithEither_class1ORclass2 将包含所有满足任一条件的 WebElement。

回答by Zach

Look at the JavaDocs:

看看JavaDocs

Annotation Type FindBys

注释类型 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")})

Annotation Type FindAll

注释类型 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")})

回答by Vignesh Paramasivam

@FindAllcan contain multiple @FindByand will return all the elements which matches any @FindByin a single list.

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

Example:

例子:

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

Whereas,

然而,

@FindByswill return the elements depending upon how @FindByspecified inside it.

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

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

Where allElementsInListcontains all the elements having className="two"inside id="one"

whereallElementsInList包含className="two"里面的所有元素id="one"

回答by BuBu

to put it in simple words, @FindBys have AND conditional relationship among the @FindBy whereas @FindAll has OR conditional relationship.

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