问题描述
我有一个问题,当我使用FindBy批注时,Selenium Webdriver的PageFactory.initElements方法如何工作
I have a question on how the PageFactory.initElements method of Selenium webdriver works when I use FindBy annotation
我有一段这样的代码
Class PageObject {
@FindBy(id = "username")
private WebElement userName;
@FindBy(id = "password")
private WebElement passWord;
private WebDriver driver;
private String url = "http://www.loginpage.com";
public PageObject() {
driver = new FirefoxDriver();
PageFactory.initElements(driver, this);
}
void load() {
this.driver.get(url);
}
}
class TestClass {
public void testMethod() {
PageObject po = new PageObject();
po.load();
}
}
如果我阅读PageFactory.initElements方法的文档,它说-实例化给定类的实例,并为每个已声明的WebElement和List字段设置一个惰性代理,并假定该字段名称也是HTML元素的"id"或"name".
If I read documentation of the PageFactory.initElements method, it says - Instantiate an instance of the given class, and set a lazy proxy for each of the WebElement and List fields that have been declared, assuming that the field name is also the HTML element's "id" or "name".
我的问题是,当我在测试类中创建页面的对象时,它将触发页面对象的构造函数并使用PageFactory初始化Webelements.但是它将初始化为什么呢?仅在使用load方法构造页面对象之后,才导航到该页面.当页面尚不可用时,PageFactory会将我的WebElement初始化为什么.它是如何工作的.有人可以帮我理解这一点吗
My question here is when I create the object of the page in the test class, it fires the constructor of the page object and initializes the webelements using PageFactory. But what it would initialize to? I navigate to the page only after I construct the page object, using the load method. What would the PageFactory initialize my WebElements to when the page is not yet available. How does it work. Can somebody please help me understand this
问候高丽人
推荐答案
会发生以下情况:调用initElements
时,PageObjectFactory
会扫描PageObject
以查找类型为WebElement
的字段.对于页面对象具有的每个WebElement
字段,它将创建一个代理对象.该代理对象存储WebElement
的定位符(@FindBy
批注的值).然后将代理分配给该字段.
Here's what happens: When you call initElements
, the PageObjectFactory
scans your PageObject
for fields of the type WebElement
. For each WebElement
field that your page object has, it creates a proxy object. That proxy object stores the locator of the WebElement
(the value of the @FindBy
annotation). The proxy is then assigned to the field.
稍后,当您的代码实际访问该字段而不是WebElement
对象时,您将检索代理.请记住,代理对象知道"它代表的WebElement
的定位符.现在,代理实际上尝试在当前页面上定位该定位器,并返回实际的WebElement
(如果已找到).
Later, when your code actually accesses the field, instead of a WebElement
object, you retrieve the proxy. Remember that the proxy object "knows" the locator of the WebElement
it represents. Now the proxy actually tries to locate that locator on the current page, and returns the actual WebElement
if it was found.
因此,在您的示例中,只要您不使用userName
或passWord
字段,它们实际上就不会位于 中.这意味着,例如,即使定位器错误,您也不会得到NoSuchElementException
,除非您实际使用这些元素.
So, in your example, as long as you don't work with the userName
or passWord
fields, they won't be actually located. That means, that for example you wouldn't get a NoSuchElementException
, even if the locators were wrong, unless you actually work with those elements.
因此回答您的问题:初始化PageObject
时,驱动程序尚未导航到任何地方都没有关系,因为代理对象的创建实际上并没有 locate 他们.
So to answer your question: It doesn't matter that at the time when the PageObject
is initialized the driver hasn't navigated anywhere yet, as the creation of the proxy objects doesn't actually locate them.
这篇关于硒的PageFactory类及其与FindBy批注一起使用时的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!