java - 运行测试时页面对象模型看到空指针异常?

标签 java selenium selenium-webdriver pageobjects

当触发我的测试时,我在尝试与我的 Page Factory WebElement 交互时似乎看到了空指针异常。

我的DriverFactory中包含的代码:

public class DriverFactory {
    private static DriverFactory instance = null;
    public static ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();

    public static DriverFactory getInstance() {
        if ( instance == null ) {
            instance = new DriverFactory();
        }
        return instance;
    }

    public static final void setDriver(String browser) {
        switch (browser) {

        case "firefox":
                System.setProperty("webdriver.gecko.driver", 
                Global_VARS.FIREFOX_DRIVER_DIRECTORY);
                webDriver.set(new FirefoxDriver());
            break;

        case "chrome":
                System.setProperty("webdriver.chrome.driver", 
                Global_VARS.CHROME_DRIVER_DIRECTORY);
                webDriver.set(new ChromeDriver());
            break;
        }
        getDriver().manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        getDriver().manage().window().maximize();
    }

    public static WebDriver getDriver() {
        return webDriver.get();
    }

    public static void tearDown() {
        getDriver().quit();
    }
}

我的基本页面中包含的代码:

public abstract class BasePO<T>  {
    private @FindBy(xpath = "//a[text()='Log in']") WebElement logIn_button;
    protected WebDriver driver;

    public BasePO() {
        this.driver = DriverFactory.getDriver();
        PageFactory.initElements(this.driver, this);
    }

    public void openHomepage() {
        driver.get("https://stackoverflow.com/");
    }

    public void baseClickOnLoginButton() {
        logIn_button.click();
    }
}

我的BaseTest类中包含的代码:

public class BaseTest {
    public SubPage subPage;
    public BasePO<?> basePage;

    @BeforeClass
    public void pomSetup() {
        subPage = PageFactory.initElements(DriverFactory.getDriver(), SubPage.class);
        basePage = PageFactory.initElements(DriverFactory.getDriver(), BasePO.class);
    }

    @BeforeMethod
    public void setup() {
        DriverFactory.setDriver("chrome");

        //works
        //subPage.openHomepage();
    }

    @AfterMethod
    public void tearDown() {
        if (DriverFactory.getDriver() != null) {
            DriverFactory.tearDown();
        }
    }

构成我的测试用例的代码:

public class Test1 extends BaseTest {
    @Test
    public void exampleTest1() throws InterruptedException {
        subPage.openHomepage(); //works as expected

        subPage.clickOnLoginButton(); //Exception here, null pointer 
    }
}

当触发我的TestNg测试时,其openHomePage方法有效,依次打开指定的url;但是,当尝试单击 Page Factory 元素时,它会使用 DriverFactory.getDriver().get() ,例如调用: logIn_button.click(); 在我的测试中,即使我已经初始化了该类,我似乎仍收到空指针异常?

最佳答案

在@BeforeClass中,您在创建驱动程序实例之前已经初始化了Page工厂。如果将 pomSetup() 中的代码移至 DriverFactory.setDriver("chrome"); 测试代码之后的 setup() 方法应该管用。另外,在BasePO类中,您已经在构造函数中初始化了页面工厂,因此在BaseTest类中调用new就足够了。

@BeforeClass
public void pomSetup() {

}

@BeforeMethod
public void setup() {
    DriverFactory.setDriver("chrome");
    // Page factory initialized the constructor of BasePO class
    subPage = new SubPage();
}

关于java - 运行测试时页面对象模型看到空指针异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53839016/

相关文章:

Java 程序因某些测试用例超时而失败

java - JSTL fmt :message and resource bundle result in ? ??你好?

java - 具有 nativeEvents 的 InternetExplorer Webdriver

selenium - 如果Implicit和Explicit等待,两者都用在Framework中怎么办

unit-testing - Chrome Webdriver 在 headless 模式下丢失用户凭据

java - 计算句子中重复单词的总数

java - LDAP:错误代码 21 - ct;二进制:每个语法的值 #0 无效

javascript - 如何设置浏览器视口(viewport)大小

ruby-on-rails - 使用 RSPEC 进行测试时突然莫名其妙的事件记录连接超时

python - 在 Python 中使用 Selenium Webdriver 下载图像