Java Selenium PageObject引发调用目标异常
我正在尝试为一个Web应用程序创建一个框架(Selenium+TestNg+java)(环境是MacOs+ChromeDriver,驱动程序服务器位于\usr\local\bin中),但陷入了基本结构的困境。我有一个类(Driversetup.java)启动浏览器,另一个类包含WebElements和方法(ProfileUpdateObjects.java),第三个类包含测试方法。现在,当我尝试运行这个只有一个方法的TestNG类时,我得到了以下异常Java Selenium PageObject引发调用目标异常,java,selenium,testng,page-factory,Java,Selenium,Testng,Page Factory,我正在尝试为一个Web应用程序创建一个框架(Selenium+TestNg+java)(环境是MacOs+ChromeDriver,驱动程序服务器位于\usr\local\bin中),但陷入了基本结构的困境。我有一个类(Driversetup.java)启动浏览器,另一个类包含WebElements和方法(ProfileUpdateObjects.java),第三个类包含测试方法。现在,当我尝试运行这个只有一个方法的TestNG类时,我得到了以下异常 java.lang.RuntimeExcep
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at org.openqa.selenium.support.PageFactory.instantiatePage(PageFactory.java:138).
下面是代码(所有类都在不同的包中)
ProfileUpdateObject类的代码
public class ProfileUpdateObjects {
WebDriver driver;
public ProfileUpdateObjects(WebDriver cdriver) {
this.driver = cdriver;
}
@FindBy(xpath = " //div[@class='ico-menu']")
private WebElement menu;
@FindBy(xpath = "//a[@title='My Dashboard']")
private WebElement myDashboard;
@FindBy(xpath = " //a[contains(text(),'View Profile')]")
public WebElement profile;
@FindBy(xpath = "//li[contains(text(),'Permanent Address')]")
private WebElement permanentAddress;
@FindBy(xpath = "//li[contains(text(),'Banking Information')]")
private WebElement bankingInformation;
WebDriverWait waitfor = new WebDriverWait(driver, 2000);
public void navigateProfile() throws InterruptedException {
menu.click();
profile.click();
waitfor.until(ExpectedConditions.visibilityOf(permanentAddress));
}
}
DriverSetup.java
public class DriverSetup {
public static WebDriver driver;
public static WebDriver startBrowser(String browserName, String url) {
if (browserName.equalsIgnoreCase("chrome")) {
driver = new ChromeDriver();
}
driver.manage().window().maximize();
driver.get(url);
return driver;
}
}
pu.navigateProfile()调用失败。另外,与driver.find()语法相比,@FindBy确实占用了更多的内存吗?除了POM之外,自动化框架还有其他设计原则吗?因为Web上的大多数资源都是POM的一种或另一种实现。简单的解决方案是移动
新的WebDriverWait
。不应将其实例化为实例变量
而不是:
WebDriverWait waitfor = new WebDriverWait(driver, 2000);
public void navigateProfile() throws InterruptedException {
menu.click();
profile.click();
waitfor.until(ExpectedConditions.visibilityOf(permanentAddress));
}
使用:
这将解决您的问题(已测试过)
startBrowser
方法acceps 2参数。“browserName”和“url”,但您在profileUpdate()
test中只提供了“browserName”。
WebDriverWait waitfor = new WebDriverWait(driver, 2000);
public void navigateProfile() throws InterruptedException {
menu.click();
profile.click();
waitfor.until(ExpectedConditions.visibilityOf(permanentAddress));
}
public void navigateProfile() {
menu.click();
profile.click();
new WebDriverWait(driver, 2000).until(ExpectedConditions.visibilityOf(permanentAddress));
}