简体   繁体   English

运行黄瓜测试时的异常

[英]Exceptions while running Cucumber Tests

I am trying to run a script in Eclipse using Firefox driver and selenium using Cucumber Gherkin format for BDD. 我正在尝试使用Firefox驱动程序在Eclipse中运行脚本,并使用针对BDD的Cucumber Gherkin格式运行selenium。 I am getting a lot of exceptions when I run this on Junit this is my code as follows for the Java file. 当我在Junit上运行此程序时,我遇到很多异常,这是我的Java文件代码,如下所示。

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

These are the exceptions that I am receiving once I execute the test 这些是我执行测试后收到的例外情况

Feature: annotation 功能:注解

#This is how background can be used to eliminate duplicate steps #这是可以用来消除重复步骤的背景
Background: [90m# annotation\\outline.feature:4[0m User navigates to Facebook Given I am on Facebook login page 背景:[90m#注解\\ outline.feature:4 [0m鉴于我在Facebook登录页面上,所以用户导航到Facebook

#Scenario with AND Scenario: [90m# annotation\\outline.feature:9[0m [90mWhen [0m[90mI enter username as "[0m[90m[1mTOM[0m[90m"[0m [90m# annotation.enterUsername(String)[0m [1A [31mWhen [0m[31mI enter username as "[0m[31m[1mTOM[0m[31m"[0m [90m# annotation.enterUsername(String)[0m [31mjava.lang.NullPointerException at annotation.annotation.enterUsername(annotation.java:22) at ✽.When I enter username as "TOM"(annotation\\outline.feature:10) [0m [90mAnd [0m[90mI enter password as "[0m[90m[1mJERRY[0m[90m"[0m [90m# annotation.enterPassword(String)[0m [1A [36mAnd [0m[36mI enter password as "[0m[36m[1mJERRY[0m[36m"[0m [90m# annotation.enterPassword(String)[0m [90mThen [0m[90mLogin should fail[0m [90m# annotation.checkFail()[0m [1A [36mThen [0m[36mLogin should fail[0m [90m# annotation.checkFail()[0m 具有AND方案的#方案:[90m#注解\\ outline.feature:9 [0m [90m当[0m [90mI输入用户名作为“ [0m [90m [1mTOM [0m [90m] [0m [90m#注解.enterUsername(String) [0m [1A [31m当[0m [31mI]输入用户名作为“ [0m [31m [1mTOM [0m [31m] [0m (annotation.java:22)在✽。当我输入用户名作为“ TOM”时(annotation \\ outline.feature:10)[0m [90mAnd [0m [90mI]输入密码为“ [0m [90m [1mJERRY [0m [90m]] [0m [90m#注解.enterPassword(String)[0m [1A [36mAnd [0m [36mI输入密码为“ [0m [36m [1mJERRY [0m [36m] [0m [90m#注解.enterPassword(String)[0m [ 90mThen [0m [90mLogin应该失败[0m [90m#注解.checkFail()[0m [1A [36m然后n [0m [36mLogin应该失败[0m [90m#注解.checkFail(] [0m

#This is how background can be used to eliminate duplicate steps #这是可以用来消除重复步骤的背景
Background: [90m# annotation\\outline.feature:4[0m User navigates to Facebook Given I am on Facebook login page 背景:[90m#注解\\ outline.feature:4 [0m鉴于我在Facebook登录页面上,所以用户导航到Facebook

#Scenario with BUT Scenario: [90m# annotation\\outline.feature:15[0m [90mWhen [0m[90mI enter username as "[0m[90m[1mTOM[0m[90m"[0m [90m# annotation.enterUsername(String)[0m [1A [31mWhen [0m[31mI enter username as "[0m[31m[1mTOM[0m[31m"[0m [90m# annotation.enterUsername(String)[0m [31mjava.lang.NullPointerException at annotation.annotation.enterUsername(annotation.java:22) at ✽.When I enter username as "TOM"(annotation\\outline.feature:16) [0m [90mAnd [0m[90mI enter password as "[0m[90m[1mJERRY[0m[90m"[0m [90m# annotation.enterPassword(String)[0m [1A [36mAnd [0m[36mI enter password as "[0m[36m[1mJERRY[0m[36m"[0m [90m# annotation.enterPassword(String)[0m [90mThen [0m[90mLogin should fail[0m [90m# annotation.checkFail()[0m [1A [36mThen [0m[36mLogin should fail[0m [90m# annotation.checkFail()[0m [90mBut [0m[90mRelogin option should be available[0m [90m# annotation.checkRelogin()[0m [1A [36mBut [0m[36mRelogin option should be available[0m [90m# annotation.checkRelogin()[0m #具有BUT的方案方案:[90m#注解\\ outline.feature:15 [0m [90m当[0m [90mI输入用户名作为“ [0m [90m [1mTOM [0m [90m]] [0m [90m#注解.enterUsername(String) [0m [1A [31m当[0m [31mI]输入用户名作为“ [0m [31m [1mTOM [0m [31m] [0m (annotation.java:22)在✽。当我输入用户名作为“ TOM”时(annotation \\ outline.feature:16)[0m [90mAnd [0m [90mI]输入密码为“ [0m [90m [1mJERRY [0m [90m]] [0m [90m#注解.enterPassword(String)[0m [1A [36mAnd [0m [36mI输入密码为“ [0m [36m [1mJERRY [0m [36m] [0m [90m#注解.enterPassword(String)[0m [ 90mThen [0m [90mLogin应该失败[0m [90m#注解.checkFail()[0m [1A [36m然后n [0m [36mLogin应该失败[0m [90m#注解.checkFail(] [0m可用[0m [90m#注解.checkRelogin()[0m [1A [36m但是[0m [36mRelogin选项应该可用[0m [90m#注解.checkRelogin()[0m

java.lang.NullPointerException at annotation.annotation.enterUsername(annotation.java:22) at ✽.When I enter username as "TOM"(annotation\\outline.feature:10) ✽处的annotation.annotation.enterUsername(annotation.java:22)处的java.lang.NullPointerException。当我输入用户名作为“ TOM”时(annotation \\ outline.feature:10)

java.lang.NullPointerException at annotation.annotation.enterUsername(annotation.java:22) at ✽.When I enter username as "TOM"(annotation\\outline.feature:16) ✽处的annotation.annotation.enterUsername(annotation.java:22)处的java.lang.NullPointerException。当我输入用户名作为“ TOM”时(annotation \\ outline.feature:16)

This is my outline.feature file 这是我的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

"Given" should be the first keyword on the line in the Background step, like so: “ Given”应该是“背景”步骤中该行的第一个关键字,如下所示:

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

I am able to run your scenarios in my system.Can you share the dependency/jars you are using. 我可以在系统中运行您的方案。您可以共享正在使用的依赖项/ jar。 Also, driver.close() method in Then Statement (Login should fail ) will work fine for first scenario but in second scenario it would close the browser before the next Then (Relogin option should be available) statement.so you may want to remove that close method . 另外, then语句(登录应该失败)中的driver.close()方法在第一种情况下可以正常工作,但是在第二种情况下,它将在下一个Then (Relogin选项应该可用)语句之前关闭浏览器。因此您可能想要删除这种关闭方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM