执行junit测试后显示空指针异常错误

时间:2017-03-22 10:52:08

标签: cucumber-junit

我用黄瓜创建了一个程序,面对这样的问题。 创建了pom.xml,步骤定义文件,功能文件和可运行文件。

在pom.xml中

<project xmlns="http://maven.apache.org/POM/4.0.0"        
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CucumberTest1</groupId>
<artifactId>CucumberTest1</artifactId>
<version>0.0.1-SNAPSHOT</version>
 <dependencies>  
<dependency> 
<groupId>org.seleniumhq.selenium</groupId> 
<artifactId>selenium-java</artifactId> 
<version>2.47.1</version> 
</dependency>

<dependency> 
<groupId>info.cukes</groupId> 
<artifactId>cucumber-java</artifactId> 
<version>1.0.2</version> 
<scope>test</scope> 
</dependency>
<dependency> 
<groupId>info.cukes</groupId> 
<artifactId>cucumber-junit</artifactId> 
<version>1.0.2</version> 
<scope>test</scope> 

<dependency> 
<groupId>junit</groupId> 
<artifactId>junit</artifactId> 
<version>4.10</version> 
<scope>test</scope> 
</dependency>
</dependencies>
</project>

** Annotation.java *

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()
 {               System.setProperty("webdriver.firefox.marionette","C:\\Users\\Administrator\\Downloads\\geckodriver-v0.15.0-win64\\geckodriver.exe");
  driver = new FirefoxDriver(); 
  driver.get("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(); 
   }
   } 

Ruunable class

package Annotation; 

import org.junit.runner.RunWith; 
import cucumber.junit.Cucumber; 

@RunWith(Cucumber.class) 
@Cucumber.Options(format = {"pretty", "html:target/cucumber"}) 

public class runTest {   }

featurefile       功能:注释       #这就是如何使用背景消除重复步骤

  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

控制台输出

Feature: annotation

     #This is how background can be used to eliminate duplicate steps
    Background:  [90m# Annotation\outline.feature:5[0m
     User navigates to Facebook Given 
      I am on Facebook login page


      #Scenario with AND
     Scenario:                         [90m#        Annotation\outline.feature:10[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:23)
        at ?.When I enter username as "TOM"(Annotation\outline.feature:11)
     [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

   #This is how background can be used to eliminate duplicate steps
   Background:  [90m# Annotation\outline.feature:5[0m
    User navigates to Facebook Given 
     I am on Facebook login page


    #Scenario with BUT
       Scenario:                                [90m#               Annotation\outline.feature:16[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:23)
    at ?.When I enter username as "TOM"(Annotation\outline.feature:17)
  [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# an notation.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

      java.lang.NullPointerException
       at Annotation.annotation.enterUsername(annotation.java:23)
        at ?.When I enter username as "TOM"(Annotation\outline.feature:11)

       java.lang.NullPointerException
         at Annotation.annotation.enterUsername(annotation.java:23)
       at ?.When I enter username as "TOM"(Annotation\outline.feature:17

2 个答案:

答案 0 :(得分:1)

请更改功能文件的背景部分,如下所示

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

目前就像是,

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

请更新我是否解决了问题?

答案 1 :(得分:0)

试试这个:   System.setProperty( “webdriver.gecko.driver”, “C:\用户\管理员\下载\ geckodriver-v0.15.0-Win64的\ geckodriver.exe”);

希望这能解决问题!