硒@FindBy与driver.findElement() [英] Selenium @FindBy vs driver.findElement()
问题描述
为什么我应该使用@FindBy
vs driver.findElement()
?
Why should I use @FindBy
vs driver.findElement()
?
@FindBy
迫使我将所有变量都移到类级别(当大多数变量只需要在方法级别时).似乎可以买到的唯一东西是我可以调用PageFactory.initElements()
,它可以为我处理延迟初始化.
@FindBy
forces me to move all my variables to a class level (when most of them only need to be at the method level). The only thing it seems to buy me is I can call PageFactory.initElements()
, which handles lazy initialization for me.
我想念什么?
推荐答案
粗略地说,@FindBy
只是查找元素的另一种方式(如您所说,通常的方式"是driver.findElement()
).
Roughly speaking, @FindBy
is just an alternate way of finding elements (the "usual way" being driver.findElement()
as you said).
不过,该注释的最大优势不是它本身.最好用于支持 PageObject模式 .
The big advantage of that annotation is not itself, though. It is better used to support the PageObject pattern.
简而言之, PageObject 模式告诉您为要使用/测试的系统的每个页面创建一个类.
In a few words, the PageObject pattern tells you to create a class for each page of the system you are trying to use/test.
因此,与其拥有(通常的driver.findElement()
代码):
So, instead of having (the usual driver.findElement()
code):
public class TestClass {
public void testSearch() {
WebDriver driver = new HtmlUnitDriver();
driver.get("http://www.google.com/");
Element searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("stringToSearch");
searchBox.submit();
// some assertions here
}
}
您将为页面定义一个类(对所用元素使用@FindBy
批注):
You'd define a class for the page (with the @FindBy
annotation for the elements used):
public class GooglePage {
@FindBy(how = How.NAME, using = "q")
private WebElement searchBox;
public void searchFor(String text) {
searchBox.sendKeys(text);
searchBox.submit();
}
}
并像这样使用它:
public class TestClass {
public void testSearch() {
WebDriver driver = new HtmlUnitDriver();
driver.get("http://www.google.com/");
GooglePage page = PageFactory.initElements(driver, GooglePage.class);
page.searchFor("stringToSearch");
// some assertions here
}
}
现在,我知道这乍看起来似乎很冗长,但请稍等一下,考虑为该页面准备几个测试用例.如果searchBox
的名称更改怎么办? (从name
"q"
到id
,例如query
?)
Now, I know this may seem verbose at first, but just give it a moment and consider having several tests cases for that page. What if the name of the searchBox
changes? (From the name
"q"
to an id
, say query
?)
在什么代码中,将进行更多更改以使其再次运行?有页对象(和@FindBy
)的一个?如果页面的结构发生了很大变化,那么使用哪种代码进行维护会更容易?
In what code there would be more changes to make it work again? The one with or the one without the Page Objects (and @FindBy
)? If a page changes its structure a lot, in what code the maintenance would be easier?
还有其他一些优点,例如像这样的附加注释:
There are some other advantages, such as additional annotations like:
@FindBy(name = "q")
@CacheLookup
private WebElement searchBox;
该@CacheLookup
使对该元素的查找仅发生一次.之后,它将被缓存在变量中并且可以更快地访问.
That @CacheLookup
makes the lookup for the element happen just once. After that, it will be cached in the variable and accessible much faster.
希望这会有所帮助.有关更多详细信息,请确保检查 PageFactory 和 <一个href ="http://code.google.com/p/selenium/wiki/PageObjects"> PageObject模式 .
Hope this helps. For more details, make sure to check PageFactory and the PageObject pattern.
这篇关于硒@FindBy与driver.findElement()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!