我正在使用页面对象模型自动化我的项目。但我注意到,每当我为同一个页面对象编写两个不同的场景时,每次使用Pagefactory.initElements方法编写测试用例时,我都需要初始化该测试用例的元素。有没有办法让它只被初始化一次,并且可以在所有测试用例中重用?
我尝试将引用变量设置为静态变量,例如"manorgpom ort=PageFactory.initElements(getdriver(),manorgpom.class);“,但它给出了空指针异常。我在测试用例之外对它们进行了初始化,并将引用变量设为静态变量,但没有成功。
@Test(priority=3)
public void orgact() throws Exception {
manorgpom ort=PageFactory.initElements(getdriver(), manorgpom.class);
getdriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
ort.actorg();
Thread.sleep(5000);
}
@Test(priority=4)
public void orgadd() throws Exception{
manorgpom ort=PageFactory.initElements(getdriver(), manorgpom.class);
getdriver().manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
ort.addorg();
Thread.sleep(10000);
}如果你在上面看到,我需要在每次写测试用例的时候初始化web元素。每次我都需要提到"manorgpom ort=PageFactory.initElements(getdriver(),manorgpom.class);“。我想对此进行优化。
这是manageorgpom类的代码片段。
public class manorgpom extends Basetest{
@FindBy(xpath="//*[@href='/organization']")
WebElement orglink;
@FindBy(xpath="//*[@class='anticon anticon-filter']")
WebElement filter;
@FindBy(xpath="//*[@placeholder='e.g. High School USA']")
WebElement filternametxt;
@FindBy(xpath="//*[text()='Activate']")
WebElement activatelink;
@FindBy(xpath="//*[@placeholder='e.g. Johnny']")
WebElement contactfirstname;当我将ort引用变量设为静态时,每次运行该套件时都会得到java.lang.nullPointerException。请帮帮我。
发布于 2019-10-15 22:12:52
这是因为你的webDriver每次都会被实例化。您需要使其成为单例,并且只使用webDriver的一个实例。
public class driverSingleton {
private static final webDriver instance = new ChromeDriver(); //just for example
//private constructor to avoid client applications to use constructor private EagerInitializedSingleton(){}
public static driverSingleton getInstance(){ return instance;
} https://stackoverflow.com/questions/58315289
复制相似问题