"显示java.lang.NullPointerException"当作为Junit测试而不是作为Java应用程序运行时抛出

时间:2017-03-13 13:19:03

标签: java selenium junit cucumber

我正在运行一个BDD Cucumber代码作为Junit测试,并且这个测试继续使用javaLang nullPointerException失败。但是当我删除黄瓜标签并作为java应用程序运行时,相同的代码通过,它通过。任何人都可以帮忙。我已经粘贴了脚本和错误跟踪。 我的appologies伙计们!我是Java新手 这是以下代码:

package simpleSteps1;

import java.io.File;
import java.util.concurrent.TimeUnit;

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

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import junit.framework.Assert;
import cucumber.api.java.en.And;

public class TestOrderStepDef {

WebDriver driver = null;

@Given("^user is on logon page$")
public void user_is_on_logon_page() throws InterruptedException {
    File file = new File("C:\\Users\\Public\\TestFolder\\geckodriver.exe");
    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
    System.setProperty("webdriver.gecko.driver", "C:\\Users\\Public\\TestFolder\\geckodriver.exe");
    driver = new FirefoxDriver();
    driver.get("http://automationpractice.com");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    Thread.sleep(3000);
    driver.findElement(By.xpath(".//*[@id='header']/div[2]/div/div/nav/div[1]/a")).click();
    Thread.sleep(3000);

}

@When("^user enteres valid email$")
public void user_enteres_valid_email() throws InterruptedException {
    driver.findElement(By.xpath(".//*[@id='email']")).sendKeys("test1@email.com");
    Thread.sleep(3000);

}

@And("^User enters valid password$")
public void User_enters_valid_password() throws InterruptedException {
    driver.findElement(By.xpath(".//*[@id='passwd']")).sendKeys("password1");
    Thread.sleep(2000);
    driver.findElement(By.id("SubmitLogin")).click();
    Thread.sleep(3000);

}

@Then("^User should be logged in$")
public void User_should_be_logged_in() throws InterruptedException {
    String homePageText = driver.findElement(By.xpath(".//*[@id='center_column']/h1")).getText();
    System.out.println("Key HomePage text :" + " " + homePageText);
    Thread.sleep(3000);
    System.out.println("Braaaaaaaaaaaaavvvvvvvvvvvvvvvvooooooooooooo: AmLogged on!!!!!!!!");
    Thread.sleep(3000);
}

@Given("^User is logged on$")
public void User_is_logged_on() {
    String homePageText = driver.findElement(By.xpath(".//*[@id='center_column']/h1")).getText();
    System.out.println("Key HomePage text :" + " " + homePageText);
    System.out.println("Am still logged on AmLogged on!!!!!!!! If: " + homePageText + "is still displayed here");

}

@When("^User performs a serach$")
public void User_performs_a_serach() throws InterruptedException {
    driver.findElement(By.id("search_query_top")).sendKeys("DRESSES");
    Thread.sleep(3000);
    driver.findElement(By.xpath(".//*[@id='searchbox']/button")).click();
    Thread.sleep(2000);

}

@Then("^User is taken to the correct page$")
public void User_is_taken_to_the_correct_page() {
    String sText = driver.findElement(By.xpath(".//*[@id='categories_block_left']/h2")).getText();
    String sText1 = driver.findElement(By.xpath(".//*[@id='informations_block_left_1']/p/a")).getText();
    String sText2 = driver.findElement(By.xpath(".//*[@id='manufacturers_block_left']/p/a")).getText();
    Assert.assertEquals("CATEGORIES", sText);
    Assert.assertEquals("INFORMATION", sText1);
    Assert.assertEquals("MANUFACTURERS", sText2);
    System.out.println("Key Page Texts are :" + ":" + sText + ":" + sText1 + ":" + sText2);

}

@And("^User Clicks on Orders Page$")
public void Clicks_on_Orders_Page() throws InterruptedException {
    Thread.sleep(2000);
    driver.findElement(By.xpath(".//*[@id='left_column']/div[4]/div/ul/li[1]/a")).click();

}

@Then("^User should land on Order History Page$")
public void User_should_land_on_Order_History_Page() throws InterruptedException {
    Thread.sleep(2000);
    String OrderPage = driver.findElement(By.xpath(".//*[@id='center_column']/h1")).getText();
    Assert.assertEquals("ORDER HISTORY", OrderPage);
    Thread.sleep(2000);

}

@Given("^user had completed search$")
public void user_had_completed_search() throws InterruptedException {
    driver.findElement(By.xpath(".//*[@id='center_column']/ul/li[1]/a/span")).click();
    Thread.sleep(2000);
}

@When("user clicks on Sign Out button$")
public void user_clicks_on_Sign_Out_button() throws InterruptedException {
    driver.findElement(By.xpath(".//*[@id='header']/div[2]/div/div/nav/div[2]/a")).click();
    Thread.sleep(6000);
}

@Then("^User is signed out of page$")
public void User_is_signed_out_of_page() {
    String LogoutPage = driver.findElement(By.xpath(".//*[@id='login_form']/h3")).getText();
    Assert.assertEquals("ALREADY REGISTERED?", LogoutPage);
    System.out.println("Logout page should have: " + LogoutPage);

    driver.close();
    driver.quit();
}

}

这是跑步者类:

package simpleSteps1;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)

@CucumberOptions(features = "src/test/java/simpleSteps1/TestOrders.feature", 
                    format = {"pretty"})

public class TestOrderRunner {


}

以下是控制台输出跟踪:

Feature: General Page Navigation
1489409972904   geckodriver INFO    Listening on 127.0.0.1:42232
1489409973750   mozprofile::profile INFO    Using profile path C:\Users\Hp\AppData\Local\Temp\rust_mozprofile.HQ7KCPRaWEBo
1489409973786   geckodriver::marionette INFO    Starting browser C:\Program Files\Mozilla Firefox\firefox.exe with args []
1489409973817   geckodriver::marionette INFO    Connecting to Marionette on localhost:57692
1489409976216   Marionette  INFO    Listening on port 57692
Mar 13, 2017 12:59:40 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Key HomePage text : MY ACCOUNT
Braaaaaaaaaaaaavvvvvvvvvvvvvvvvooooooooooooo: AmLogged on!!!!!!!!

Scenario: Verify Logon           [90m# src/test/java/simpleSteps1/TestOrders.feature:3[0m
[32mGiven [0m[32muser is on logon page[0m    [90m# TestOrderStepDef.user_is_on_logon_page()[0m
[32mWhen [0m[32muser enteres valid email[0m  [90m# TestOrderStepDef.user_enteres_valid_email()[0m
[32mAnd [0m[32mUser enters valid password[0m [90m# TestOrderStepDef.User_enters_valid_password()[0m
[32mThen [0m[32mUser should be logged in[0m  [90m# TestOrderStepDef.User_should_be_logged_in()[0m

Scenario: Verify Logout                       [90m# src/test/java/simpleSteps1/TestOrders.feature:9[0m
[31mGiven [0m[31mUser is logged on[0m                     [90m# TestOrderStepDef.User_is_logged_on()[0m
  [31mjava.lang.NullPointerException
    at simpleSteps1.TestOrderStepDef.User_is_logged_on(TestOrderStepDef.java:62)
    at ✽.Given User is logged on(src/test/java/simpleSteps1/TestOrders.feature:10)
  [0m
[36mWhen [0m[36mUser performs a serach[0m                 [90m# TestOrderStepDef.User_performs_a_serach()[0m
[36mThen [0m[36mUser is taken to the correct page[0m      [90m# TestOrderStepDef.User_is_taken_to_the_correct_page()[0m
[36mAnd [0m[36mUser Clicks on Orders Page[0m              [90m# TestOrderStepDef.Clicks_on_Orders_Page()[0m
[36mThen [0m[36mUser should land on Order History Page[0m [90m# TestOrderStepDef.User_should_land_on_Order_History_Page()[0m

Scenario: Logout                      [90m# src/test/java/simpleSteps1/TestOrders.feature:16[0m
[31mGiven [0m[31muser had completed search[0m     [90m# TestOrderStepDef.user_had_completed_search()[0m
  [31mjava.lang.NullPointerException
    at simpleSteps1.TestOrderStepDef.user_had_completed_search(TestOrderStepDef.java:107)
    at ✽.Given user had completed search(src/test/java/simpleSteps1/TestOrders.feature:17)
  [0m
[36mWhen [0m[36muser clicks on Sign Out button[0m [90m# TestOrderStepDef.user_clicks_on_Sign_Out_button()[0m
[36mThen [0m[36mUser is signed out of page[0m     [90m# TestOrderStepDef.User_is_signed_out_of_page()[0m

[31mFailed scenarios:[0m
[31msrc/test/java/simpleSteps1/TestOrders.feature:9 [0m# Scenario: Verify Logout
[31msrc/test/java/simpleSteps1/TestOrders.feature:16 [0m# Scenario: Logout

3 Scenarios ([31m2 failed[0m, [32m1 passed[0m)
12 Steps ([31m2 failed[0m, [36m6 skipped[0m, [32m4 passed[0m)
0m37.287s

java.lang.NullPointerException
at simpleSteps1.TestOrderStepDef.User_is_logged_on(TestOrderStepDef.java:62)
at ✽.Given User is logged on(src/test/java/simpleSteps1/TestOrders.feature:10)

java.lang.NullPointerException
at simpleSteps1.TestOrderStepDef.user_had_completed_search(TestOrderStepDef.java:107)
at ✽.Given user had completed search(src/test/java/simpleSteps1/TestOrders.feature:17)

0 个答案:

没有答案