【问题标题】:Exceptions while running Cucumber Tests运行 Cucumber 测试时出现异常
【发布时间】:2018-05-17 11:26:41
【问题描述】:

我正在尝试使用 Firefox 驱动程序在 Eclipse 中运行脚本,并使用 Cucumber Gherkin 格式的 BDD 运行 selenium。当我在 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#注解\outline.feature:4[0m 用户导航到 Facebook Given 我在 Facebook 登录页面上

#场景与 AND 场景:[90m# 注释\outline.feature:9[0m [90m当[0m[90m我输入用户名为“[0m[90m[1mTOM[0m[90m”[0m[90m# annotation.enterUsername(String)[0m [1A [31mWhen [0m[31mI 输入用户名“[0m[31m[1mTOM[0m[31m”[0m[90m# annotation.enterUsername(String)[0m [31mjava.lang.NullPointerException 在 annotation.annotation.enterUsername(annotation.java:22) 在✽。当我输入用户名作为“TOM”时(annotation\outline.feature:10) [0米 [90m和[0m[90m我输入密码为“[0m[90m[1mJERRY[0m[90m”[0m[90m# annotation.enterPassword(String)[0m [1A [36m和[0m[36m我输入 密码为“[0m[36m[1mJERRY[0m[36m”[0m[90m# annotation.enterPassword(String)[0m [90mThen [0m[90mLogin 应该失败[0m [90m# annotation.checkFail()[0m [1A [36mThen [0m[36mLogin 应该 fail[0m [90m# annotation.checkFail()[0m

#这是如何使用背景来消除重复步骤
背景:[90m#注解\outline.feature:4[0m 用户导航到 Facebook Given 我在 Facebook 登录页面上

#Scenario with BUT Scenario: [90m# 注释\outline.feature:15[0m [90m当[0m[90m我输入用户名为“[0m[90m[1mTOM[0m[90m”[0m[90m# annotation.enterUsername(String)[0m [1A [31mWhen [0m[31mI 输入用户名“[0m[31m[1mTOM[0m[31m”[0m[90m# annotation.enterUsername(String)[0m [31mjava.lang.NullPointerException 在 annotation.annotation.enterUsername(annotation.java:22) 在✽。当我输入用户名作为“TOM”时(annotation\outline.feature:16) [0米 [90m和[0m[90m我输入密码为“[0m[90m[1mJERRY[0m[90m”[0m[90m# annotation.enterPassword(String)[0m [1A [36m和[0m[36m我输入 密码为“[0m[36m[1mJERRY[0m[36m”[0m[90m# annotation.enterPassword(String)[0m [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 在 annotation.annotation.enterUsername(annotation.java:22) at ✽。当我 输入用户名“TOM”(annotation\outline.feature:10)

java.lang.NullPointerException 在 annotation.annotation.enterUsername(annotation.java:22) at ✽。当我 输入用户名“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

【问题讨论】:

  • @Before 替换@Given("^I am on Facebook login page$") 能解决问题吗?
  • ...或者在WebDriver driver = null;之前添加static
  • 请出示outline.feature
  • 请分享您的功能文件

标签: java selenium cucumber gherkin


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我能够在我的系统中运行你的场景。你能分享你正在使用的依赖项/jars。 此外, Then 语句中的 driver.close() 方法(登录应该失败)在第一种情况下可以正常工作,但在第二种情况下,它会在下一个 之前关闭浏览器>然后(重新登录选项应该可用)语句。因此您可能需要删除该关闭方法。

    【讨论】:

      猜你喜欢
      • 2016-10-17
      • 1970-01-01
      • 2016-02-15
      • 2018-11-17
      • 2021-08-06
      • 2020-05-23
      • 2013-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多