[英]Which is the better way to create instance of PageObject?
I would like to know from the below which is better option for creating the instance of the PageObject class(Ex: LoginPage):- 我想从下面知道哪个是创建PageObject类实例的更好选项(Ex:LoginPage): -
1) Create an instance of Pageobject class in all the tests and steps (Wherever required)? 1)在所有测试和步骤(任何需要的地方)中创建Pageobject类的实例?
LoginPage loginpage = PageFactory.initElements(webDriver, LoginPage.class);
(or) (要么)
2) Create a class with a static method to return the instance for the requested PageObject class. 2)使用静态方法创建一个类,以返回所请求的PageObject类的实例。 In this method, check if the instance is null before creating a new instance for the requested class? 在此方法中,在为请求的类创建新实例之前检查实例是否为null?
LoginPage loginpage = PageUtil.getPageObject("login");
Please advise. 请指教。
There are multiple ways to do it. 有多种方法可以做到这一点。 I like to create a BaseClass()
and instantiate the PageFactory.initElements(driver, this);
我喜欢创建一个BaseClass()
并实例化PageFactory.initElements(driver, this);
there. 那里。 See my gist . 看看我的要点 。 Also, the public repository here 此外, 这里是公共存储库
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
/**
* Created by Saifur on 2/14/2015.
*/
public class BaseClass {
//global driver instance.
WebDriver driver;
//super constructor
public BaseClass(WebDriver _driver)
{
//assigning driver instance globally.
driver = _driver;
/*Instantiating all elements since this is super class
and inherited by each and every page object */
PageFactory.initElements(driver, this);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.