这里简单的介绍一下使用Loadable Component 的使用方法,后面附上selenium官方wiki中的详细介绍。
使用Loadable Component编写PageObjects方式的webdriver脚本会非常的方便。
第一步
当你编写一个page类时,首先需要继承Loadable Component 这个父类,方式如下:public class TestPage extends LoadableComponent<TestPage> {
//......
}第二步
当你继承Loadable Component 后需要实现父类的两个方法:@Override
protected void load() {
driver.get("http://xxxxxxxxxxx");
}
@Override
protected void isLoaded() throws Error {
String url = driver.getCurrentUrl();
assertTrue("Not on the issue entry page: " + url, url.endsWith("xxxxxx"));
}第三步
定义改page类的相关页面元素和方法,这里就是Loadable Component的方便之处。方法如下:// By default the PageFactory will locate elements with the same name or id
// as the field. Since the summary element has a name attribute of "summary"
// we don't need any additional annotations.
private WebElement summary;
// Same with the submit element, which has the ID "submit"
private WebElement submit;summary submit 是我们定义的页面元素,在使用时,Loadable Component 的实现会自动的使用PageFactory的方法在页面中查找 name或id 为summary submit 的元素。
如果我们不想使用与页面元素相同名字的变量,可以通过PageFcatory的@FindBy方法重写元素定义,方式如下:@FindBy(name = "XXXXXXX")第四步
在page类中实现PageFcatory,写一个构造函数 方法如下:public
TestPage (WebDriver driver) {
this.driver = driver;
// This call sets the WebElement fields.
PageFactory.initElements(driver, this);
}这样就简单的实现了Loadable Component
第五步
测试脚本的编写与PageFcatory 基本一样。
由于每个页面的url已经在page类中写好,使用时只需要在new这个page类时后面跟上get 方法即可打开url,方法如下:TestPage page = new
TestPage(driver).get();以下是官方wiki中的详细介绍:
使用Loadable Component编写PageObjects方式的webdriver脚本会非常的方便。
第一步
当你编写一个page类时,首先需要继承Loadable Component 这个父类,方式如下:public class TestPage extends LoadableComponent<TestPage> {
//......
}第二步
当你继承Loadable Component 后需要实现父类的两个方法:@Override
protected void load() {
driver.get("http://xxxxxxxxxxx");
}
@Override
protected void isLoaded() throws Error {
String url = driver.getCurrentUrl();
assertTrue("Not on the issue entry page: " + url, url.endsWith("xxxxxx"));
}第三步
定义改page类的相关页面元素和方法,这里就是Loadable Component的方便之处。方法如下:// By default the PageFactory will locate elements with the same name or id
// as the field. Since the summary element has a name attribute of "summary"
// we don't need any additional annotations.
private WebElement summary;
// Same with the submit element, which has the ID "submit"
private WebElement submit;summary submit 是我们定义的页面元素,在使用时,Loadable Component 的实现会自动的使用PageFactory的方法在页面中查找 name或id 为summary submit 的元素。
如果我们不想使用与页面元素相同名字的变量,可以通过PageFcatory的@FindBy方法重写元素定义,方式如下:@FindBy(name = "XXXXXXX")第四步
在page类中实现PageFcatory,写一个构造函数 方法如下:public
TestPage (WebDriver driver) {
this.driver = driver;
// This call sets the WebElement fields.
PageFactory.initElements(driver, this);
}这样就简单的实现了Loadable Component
第五步
测试脚本的编写与PageFcatory 基本一样。
由于每个页面的url已经在page类中写好,使用时只需要在new这个page类时后面跟上get 方法即可打开url,方法如下:TestPage page = new
TestPage(driver).get();以下是官方wiki中的详细介绍:

