selenium webdriver中的空指针异常 - TestNG - PageFactory

时间:2016-08-25 11:12:40

标签: java selenium selenium-webdriver testng

我正在尝试使用selenium webdriver,testng和page factory自动化。但我面临空指针异常。

public class BrowserOpenClose extends Common {
    public WebDriver driver;

    @BeforeTest
    @Parameters("browser")
    public void initBrowser(String browser) {
        if (browser.equalsIgnoreCase("FF")) {
            System.setProperty("webdriver.firefox.marionette", "./gecko/geckodriver.exe");
            driver = new FirefoxDriver();
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("https://www.flipkart.com/");
        } else if (browser.equalsIgnoreCase("CH")) {
            System.setProperty("webdriver.chrome.driver", "./chrome/chromedriver.exe");
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("https://www.flipkart.com/");
        }
    }

    @AfterTest
    public void closeBrowser() {
        waitUntil(10);
        driver.quit();
    }
}

    public class Login extends BrowserOpenClose {

    @FindBy(xpath = "//a[text()='Log In']")
    private WebElement loginLink;

    @FindBy(xpath = "//input[@class='_2zrpKA']")
    private WebElement username;

    @FindBy(xpath = "//input[@class='_2zrpKA _3v41xv']")
    private WebElement password;

    @FindBy(xpath = "//button[@class='_3zLR9i _1LctnI _36SmAs']")
    private WebElement loginButton;

    public Login(WebDriver driver) {
        PageFactory.initElements(driver, this);
        this.driver = driver;
    }

    public void openLoginDialog() {
        loginLink.click();
    }

    public void enterUsername(String name) {
        username.sendKeys(name);
    }

    public void enterPassword(String pass) {
        password.sendKeys(pass);
    }

    public void clickLogin() {
        loginButton.click();
    }

}

    public class SwitchTShirtPage extends BrowserOpenClose {
    WebDriver driver;
    Login login;

    @BeforeMethod
    public void initElements() {
        login = new Login(driver);
    }

    @Test
    public void switchToTShirt() {
        login.openLoginDialog();
        login.enterUsername("abc");
        login.enterPassword("xyz");
        login.clickLogin();
    }

}
  

错误跟踪日志:org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)中的java.lang.NullPointerException     在   org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)     在com.sun.proxy。$ Proxy9.click(未知来源)at   com.login.test.Login.openLoginDialog(Login.java:50)at   com.homepage.test.SwitchTShirtPage.switchToTShirt(SwitchTShirtPage.java:28)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100)     在org.testng.internal.Invoker.invokeMethod(Invoker.java:646)at   org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811)at at   org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1129)at at   org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)     在   org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)     在org.testng.TestRunner.privateRun(TestRunner.java:746)at   org.testng.TestRunner.run(TestRunner.java:600)at   org.testng.SuiteRunner.runTest(SuiteRunner.java:366)at   org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)at at   org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)at at   org.testng.SuiteRunner.run(SuiteRunner.java:268)at   org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)at   org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)at   org.testng.TestNG.runSuitesSequentially(TestNG.java:1264)at at   org.testng.TestNG.runSuitesLocally(TestNG.java:1189)at at   org.testng.TestNG.runSuites(TestNG.java:1104)at   org.testng.TestNG.run(TestNG.java:1076)at   org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)     在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152)     在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57)

1 个答案:

答案 0 :(得分:0)

来自WebDriver driver

SwitchTShirtPage从未初始化,始终为null

你想要的是来自BrowserOpenClose的那个,所以只需从SwitchTShirtPage删除声明。

然后,要小心,因为driver初始化为@BeforeTest,它只能通过test(类组)运行一次。因此,只有BrowserOpenClose的第一个实例才会初始化driver。 考虑将@BeforeTest替换为@BeforeClass,以解决问题。