【问题标题】:Selenium PageObject throws Invocation Target ExceptionSelenium 页面对象抛出 Invocationtargetexception
【发布时间】:2019-07-19 13:24:42
【问题描述】:

我正在尝试为 Web 应用程序创建一个框架(Selenium+TestNg+java)(环境是 MacOs+ChromeDriver,驱动程序服务器在 \usr\local\bin 中)但卡在了基本结构中。我有一个启动浏览器的类(Driversetup.java),另一个包含 WebElements 和方法(ProfileUpdateObjects.java),第三个包含测试方法。现在,当我尝试运行这个只有一个方法的 TestNG 类时,我得到了以下异常。

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at org.openqa.selenium.support.PageFactory.instantiatePage(PageFactory.java:138).

下面是代码(所有类都在不同的包中)。

public class ProfileUpdateTest {

    @Test(enabled = true, priority = 1)
    public void profileUpdate() throws MalformedURLException, InterruptedException, ParseException {
        WebDriver driver = DriverSetup.startBrowser("chrome");
        ProfileUpdateObjects pu = PageFactory.initElements(driver, ProfileUpdateObjects.class);
        pu.navigateProfile();
    }
}

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 的一种或另一种实现。

【问题讨论】:

  • startBrowser 方法接受 2 个参数。 “browserName”和“url”,但您在 profileUpdate() 测试中只提供了“browseName”。

标签: java selenium testng page-factory


【解决方案1】:

简单的解决方案是移动new WebDriverWait。它不应该被实例化为实例变量。

代替:

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));
    }

这将解决您的问题(已测试)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-11
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多