获取Null指针驱动程序在黄瓜步骤定义类下的驱动程序异常

时间:2017-04-14 19:38:21

标签: java webdriver cucumber cucumber-junit

我正在为Cucumber功能文件编写步骤定义,下面是我的代码。我已经初始化了驱动程序并在代码开头的@Given ste中给出了驱动程序的路径,但是即使整个代码使用相同的驱动程序,仍然只能在特定行上获得驱动程序的Null指针异常。

以下是我的代码:

public class annotation { 
 WebDriver driver = null;

  @Given("^I am on Facebook login page$")   
  public void goToFacebook() { 
   System.setProperty("webdriver.chrome.driver", "D:\\Automation 
   Jars\\chromedriver_win32\\chromedriver.exe");
  driver = new ChromeDriver();     
 driver.navigate().to("https://www.facebook.com/"); 
  } 

 @When ("^I enter password as \"(.*)\"$") 
  public void enterPassword(String arg1) {
  System.out.println("driver= "+ driver);
  driver.findElement(By.id("pass")).sendKeys(arg1);
  driver.findElement(By.id("u_0_v")).click(); 
} 

  @When("^I enter username as \"(.*)\"$") 
  public void enterUsername(String arg1){     
  System.out.println("driver= "+ driver);
  driver.findElement(By.id("email")).sendKeys(arg1); 
}

 @Then("^Login should fail$") 
 public void checkFail() {  
 if(driver.getCurrentUrl().equalsIgnoreCase(
 "https://www.facebook.com/login.php?login_attempt=1&lwv=110")){ 
    System.out.println("Test1 Pass"); 
 } else { 
 System.out.println("Test1 Failed"); 
} 
 driver.close(); 
}

以下是我得到的错误:

java.lang.NullPointerException
    at annotation.annotation.enterUsername(annotation.java:32)
    at ✽.When I enter username as "TOM"(annotation\outline.feature:10)

java.lang.NullPointerException
    at annotation.annotation.enterUsername(annotation.java:32)
    at ✽.When I enter username as "TOM"(annotation\outline.feature:16)

但我无法弄明白。请帮忙

0 个答案:

没有答案