WebDriver无法初始化

时间:2017-07-05 11:26:03

标签: java selenium webdriver

当我运行@testCase时,我得到了NullPointerException 1.在FrameworkTestCases.class中 - > @BeforeClass我初始化所选webdriver的实例。当我将FrameworkTestCases.class作为jUnit测试启动时,浏览器正在运行,但是当我到达testCase时,它会显示NullPointerException。是什么原因?我还使用了一个带有2个参数的构造函数来继承Generic.class到LoginPageFactory.class的驱动程序,但没有任何反应。

这是我的FrameworkTestCases类:

public class FrameworkTestCases {

static WebDriver driver;
private static String baseURl = "https://management.tacticlicks.com/";
static LoginPageFactory loginPage;
static Generic generic;
//WebDriver driver;
//static LoginPageFactory lpFactory;


@BeforeClass
public static void setUp() {        
    generic = new Generic(driver);
    generic.getDriver(baseURl, "chrome");
}

@Test
public void test() {
    System.out.println("Executing test");

    loginPage
    .fillUsernameField("ivailostefanov1989@gmail.com")
    .fillPasswordField("astral8909")
    .clickSubmit(); 
}


@AfterClass
public static void tearDown() {
    driver.quit();
}
}

public class LoginPageFactory extends Generic {

public LoginPageFactory(WebDriver driver2, Class<LoginPageFactory> class1) {
    super(driver2, class1);
    // TODO Auto-generated constructor stub
}

WebDriver driver;

@FindBy(name="email") //.//*[@id='login']/div[1]/div/div/table/tbody/tr/td[2]/div[1]/form/div[1]/input
WebElement loginUsernameField;

@FindBy(name="password")
WebElement loginPasswordField;

@FindBy(tagName="button")
WebElement loginSubmitButton;

public LoginPageFactory(WebDriver driver) {
    System.out.println("LoginPageFactory");
    this.driver = driver;
    PageFactory.initElements(driver, this);
}

public LoginPageFactory fillUsernameField(String username) {
    System.out.println("Before field initializing");
    WebElement emailField = driver.findElement(By.name("email"));
    emailField.click();
    emailField.sendKeys(username);
    return this;
}

public LoginPageFactory fillPasswordField(String password) {
    loginPasswordField.click();
    loginPasswordField.clear();
    loginPasswordField.sendKeys(password);
    return this;
}

public LoginPageFactory clickSubmit() {
    loginSubmitButton.click();
    return this;
}   
}

public class Generic {

WebDriver driver;

public Generic(WebDriver driver) {
    this.driver = driver;
}

public Generic(WebDriver driver2, Class<LoginPageFactory> class1) {
    // TODO Auto-generated constructor stub
}

private void getBrowser(String browser) {

    if (browser.equalsIgnoreCase("Firefox")) {
        File chromeDriver = new File("C:\\Users\\Ivo\\Desktop\\geckodriver.exe");
        System.setProperty("webdriver.gecko.driver", chromeDriver.getAbsolutePath());
        driver = new FirefoxDriver();
    } else if (browser.equalsIgnoreCase("Chrome")) {

        //set chromedriver property
        File chromeDriver = new File("C:\\Users\\Ivo\\Desktop\\chromedriver.exe");
        System.setProperty("webdriver.chrome.driver", chromeDriver.getAbsolutePath());
        driver = new ChromeDriver();


    } else {
        System.out.println("Browser cannot be launched");
    }

}

public WebDriver getDriver(String appUrl, String browser) {
    getBrowser(browser);
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.get(appUrl);
    return driver;
}
}

1 个答案:

答案 0 :(得分:0)

您在@BeforeClass方法中缺少变量赋值。

更改

@BeforeClass
public static void setUp() {        
    generic = new Generic(driver);
    generic.getDriver(baseURl, "chrome");
}

@BeforeClass
public static void setUp() {        
    generic = new Generic(driver);
    driver = generic.getDriver(baseURl, "chrome");
}

而且你也没有创建LoginPageFactory类的任何实例。您已创建变量static LoginPageFactory loginPage;但未初始化它(至少在提供的代码中没有)。 方法

@Test
public void test() {
    System.out.println("Executing test");

    //add this line of code to initialize elements via Page Factory
    loginPage = new LoginPageFactory(driver);

    loginPage
    .fillUsernameField("ivailostefanov1989@gmail.com")
    .fillPasswordField("astral8909")
    .clickSubmit(); 
}