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

时间:2018-12-18 18:25:10

标签: 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();
    }
}

我的base页中包含的代码:

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;在尝试单击DriverFactory.getDriver().get()元素(例如在测试中调用:Page Factory)时使用logIn_button.click();的元素,即使我已初始化该类,我似乎仍收到空指针异常? / p>

2 个答案:

答案 0 :(得分:2)

在@BeforeClass中,已在创建驱动程序实例之前初始化了Page工厂。如果在 NAME: AKIO R. USERNAME: ariggins SECLEVEL: 4 Menu: HELP > TIME ENTRY > MESSAGES HELP > ABOUT > ABOUT HELP > CHANGE PASSWORD > CHANGE PASSWORD ..... NAME: THE NEXT EMPLOYEE USERNAME: EMPLOYEE USERNAME SECLEVEL: 6 MENU: HELP > TIME ENTRY > TIME ENTRY HELP > ABOUT > ABOUT HELP > AD-HOC > AD-HOC ....CONTINUES UNTIL COMPLETE 之后将pomSetup()中的代码移至setup()方法中,则测试代码应该可以工作。另外,在DriverFactory.setDriver("chrome");类中,您已经在构造函数中初始化了页面工厂,因此在BasePO类中调用new就足够了。

BaseTest

答案 1 :(得分:0)

在代码部分,有一些建议。

  • DriveFactory的实现是错误的。您正在使用从未使用过的单例初始化。而是将代码更改为下面的代码。

    public class DriverFactory {
        private static DriverFactory instance = null;
        // Singleton initialization
        public static DriverFactory getInstance() {
            if ( instance == null ) 
                instance = new DriverFactory();
            return instance;
        }
    
        public ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();
    
        public 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 WebDriver getDriver() {
            return webDriver.get();
        }
    
        public void tearDown() {
            getDriver().quit();
        }
    }