2

我正在尝试为 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 的一种或另一种实现。

4

1 回答 1

0

简单的解决方法是移动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));
    }

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

于 2019-03-21T09:45:10.887 回答