运行黄瓜测试时的例外情况

时间:2018-05-17 11:26:42

标签: java selenium cucumber gherkin

我正在尝试使用Firefox驱动程序和selenium在Eclipse中运行脚本,使用Cucumber Gherkin格式进行BDD。当我在Junit上运行它时,我得到了很多例外,这是我的代码,如下所示为Java文件。

annotation.java

package annotation; 

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

import cucumber.annotation.en.Given; 
import cucumber.annotation.en.Then; 
import cucumber.annotation.en.When; 

public class annotation { 
   WebDriver driver = null; 
   @Given("^I am on Facebook login page$") 

   public void goToFacebook() { 
      driver = new FirefoxDriver(); 
      driver.navigate().to("https://www.facebook.com/"); 
   }

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

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

   @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(); 
   }

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

这些是我在执行测试后收到的例外

  

功能:注释

     

#这是背景如何用于消除重复步骤的原因   背景:[90m#annotation \ outline.feature:4 [0m        用户导航到Facebook给定        我在Facebook登录页面

     

#Scenario with AND Scenario:[90m#   注释\ outline.feature:90米       [90mWhen [0m [90mI输入用户名为" [0m [90m [1mTOM [0m [90m" [0m [90m#]   annotation.enterUsername(String)[0m [1A [31m] [0m [31mI   输入用户名为" [0m [31m [1mTOM [0m [31m" [0m [90m#]   annotation.enterUsername(字符串)[0米         [31mjava.lang.NullPointerException             at annotation.annotation.enterUsername(annotation.java:22)             在✽。当我输入用户名为" TOM"(注释\ outline.feature:10)         [0米       [90毫秒[0m [90mI输入密码为" [0m [90m [1mJERRY [0m [90m" [0m [90m#]   annotation.enterPassword(String)[0m [1A [36mAnd [0m [36mI输入   密码为" [0m [36m [1mJERRY [0m [36m" [0m [90m#   annotation.enterPassword(字符串)[0米       [90mThen [0m [90mLogin应该失败] [0m [90m#annotation.checkFail()[0m [1A [36mThen [0m [36mLogin应该]   失败[0m [90m#annotation.checkFail()[0m

     

#这是背景如何用于消除重复步骤的原因   背景:[90m#annotation \ outline.feature:4 [0m        用户导航到Facebook给定        我在Facebook登录页面

     

#Scenario with BUT Scenario:[90m#   注释\ outline.feature:15 [0米       [90mWhen [0m [90mI输入用户名为" [0m [90m [1mTOM [0m [90m" [0m [90m#]   annotation.enterUsername(String)[0m [1A [31m] [0m [31mI   输入用户名为" [0m [31m [1mTOM [0m [31m" [0m [90m#]   annotation.enterUsername(字符串)[0米         [31mjava.lang.NullPointerException             at annotation.annotation.enterUsername(annotation.java:22)             在✽。当我输入用户名为" TOM"(注释\ outline.feature:16)         [0米       [90毫秒[0m [90mI输入密码为" [0m [90m [1mJERRY [0m [90m" [0m [90m#]   annotation.enterPassword(String)[0m [1A [36mAnd [0m [36mI输入   密码为" [0m [36m [1mJERRY [0m [36m" [0m [90m#   annotation.enterPassword(字符串)[0米       [90mThen [0m [90mLogin应该失败] [0m [90m#annotation.checkFail()[0m [1A [36mThen [0m [36mLogin应该]   失败[0m [90m#annotation.checkFail()[0m       [90mBut [0m [90mRelogin选项应该可用[0m [90m#annotation.checkRelogin()[0m [1A [36mBut [0m [36mRelogin选项]   应该可用[0m [90m#annotation.checkRelogin()[0m

     

java.lang.NullPointerException at   在✽。我的annotation.annotation.enterUsername(annotation.java:22)   输入用户名为" TOM"(annotation \ outline.feature:10)

     

java.lang.NullPointerException at   在✽。我的annotation.annotation.enterUsername(annotation.java:22)   输入用户名为" TOM"(annotation \ outline.feature:16)

这是我的outline.feature文件

Feature: annotation 
#This is how background can be used to eliminate duplicate steps 

Background: 
   User navigates to Facebook Given 
   I am on Facebook login page 

#Scenario with AND 
Scenario: 
   When I enter username as "TOM"
   And I enter password as "JERRY" 
   Then Login should fail 

#Scenario with BUT 
Scenario: 
   When I enter username as "TOM" 
   And I enter password as "JERRY" 
   Then Login should fail 
   But Relogin option should be available

2 个答案:

答案 0 :(得分:1)

“Given”应该是后台步骤中该行的第一个关键字,如下所示:

Background: User navigates to Facebook  
   Given I am on Facebook login page 

答案 1 :(得分:0)

我可以在我的系统中运行您的方案。您可以共享您正在使用的依赖项/ jar。 此外, 然后 语句中的driver.close()方法(登录失败)将适用于第一种情况,但在第二种情况下,它会在下一次 然后 (Relogin选项应该可用)statement.so你可能想删除那个close方法。