【问题标题】:Getting "Null pointer exception" at the time of executing maven project using selenium webdriver在使用 selenium webdriver 执行 maven 项目时获取“空指针异常”
【发布时间】:2015-10-26 05:48:41
【问题描述】:

当我的代码首先执行时,它执行LoginTest.Java 类,然后它转到Main.Java 但在这些我得到空指针异常,它没有读取获取驱动程序的方法。 谁能帮我解决,我是这方面的初学者。

提前谢谢..!!

LoginTest.Java 类

public class LoginTest {
    private static final String ADDRESS_TO_TEST = "http://www.gmail.com";

    private WebDriver driver;
    Main Login;
    HomePage homePage;

    @BeforeTest
    public void setUp() {

        FirefoxDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get(ADDRESS_TO_TEST);

    }

    @Test(priority = 0)
    public void testLoginPageAppear() {

        // Create Login Page object
        Login = new Main(driver);
        System.out.println("Login-->" + Login);
        Login.loginToGmail("abc@gmail.com", "abc");
        homePage = new HomePage(driver);
        // Verify home page
        Assert.assertTrue(homePage.getHOmePageEmailAddress().toLowerCase()
                .contains("Google Account: abc@gmail.com"));
    }
}

Main.Java 类:

package GmailLogin;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class Main {

    /*
     * Reference
     * URL:http://www.guru99.com/page-object-model-pom-page-factory-in-
     * selenium-ultimate-guide.html
     */

    WebDriver driver;

    By email = By.id("Email");
    By nextButton = By.id("next");
    By password = By.id("Passwd");
    By signInButton = By.id("signIn");

    public Main(WebDriver driver) {
        System.out.println("Driver--->" + driver);
        this.driver = driver;
    }

    public void setEmailAddress(String strEmailAddress) {
        driver.findElement(email).sendKeys(strEmailAddress);

    }

    public void clickNextButton() {
        driver.findElement(nextButton).click();
    }

    public void setPassword(String strPassword) {
        driver.findElement(password).sendKeys(strPassword);
    }

    public void clickSignInButton() {
        driver.findElement(signInButton).click();
    }

    public void loginToGmail(String strEmailAddress, String strPassword) {

        System.out.println("Email-->" + strEmailAddress);
        System.out.println("Password-->" + strPassword);
        this.setEmailAddress(strEmailAddress);
        this.clickNextButton();
        this.setPassword(strPassword);
        this.clickSignInButton();
    }
}

空指针异常:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Driver--->null
Login-->GmailLogin.Main@5940ce27
Email-->abc@gmail.com
Password-->abc
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 21.016 sec <<< FAILURE! - in TestSuite
testLoginPageAppear(GmailLogin.LoginTest)  Time elapsed: 0.047 sec  <<< FAILURE!
java.lang.NullPointerException
    at GmailLogin.LoginTest.testLoginPageAppear(LoginTest.java:47)

【问题讨论】:

    标签: java maven selenium selenium-webdriver


    【解决方案1】:

    这一行会使您的驱动程序对象仅在方法范围内:

    FirefoxDriver driver = new FirefoxDriver();
    

    改成

    driver = new FirefoxDriver();
    

    改为填充你的类属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-10
      • 2013-07-27
      • 1970-01-01
      相关资源
      最近更新 更多