Selenium @FindBy 与 driver.findElement() [英] Selenium @FindBy vs driver.findElement()

查看:40
本文介绍了Selenium @FindBy 与 driver.findElement()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我应该使用 @FindBydriver.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页面对象模式.

Hope this helps. For more details, make sure to check PageFactory and the PageObject pattern.

这篇关于Selenium @FindBy 与 driver.findElement()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆