java.lang.NullPointerException 当使用带有 Java 的黄瓜 junit 通过步骤定义文件执行黄瓜特征文件时

Ab1*_*123 2 java selenium webdriver cucumber-junit selenium-webdriver

在使用 junit 和 Java 运行黄瓜功能文件时,我收到一个 NULL 指针异常。我无法理解为什么会出现此异常。
这是我用java编写的步骤定义文件。

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import pageObjects.CartPage;
import pageObjects.Checkoutpage;
import pageObjects.HomePage;
import pageObjects.ProductListingPage;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;

public class EndtoEndTest {

WebDriver driver;

@Given("^User is on Homepage$")
public void user_is_on_Homepage() throws Throwable {

    System.setProperty("webdriver.chrome.driver", "C:\\Users\\Downloads\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.shop.demoqa.com");
    driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
    driver.manage().window().maximize();


}

@When("^he searches for \"([^\"]*)\"$")
public void he_searches_for(String arg1) throws Throwable {

    HomePage home = new HomePage(driver);
    home.perform_Search(arg1);
}

@When("^Choose to buy the first item$")
public void choose_to_buy_the_first_item() throws Throwable {
    ProductListingPage productListingPage = new ProductListingPage(driver);
    productListingPage.select_Product(0);
    productListingPage.clickOn_AddToCart(); 
}

@When("^moves to checkout from mini cart$")
public void moves_to_checkout_from_mini_cart() throws Throwable {
    CartPage cartPage = new CartPage(driver);
    cartPage.clickOn_Cart();
    cartPage.clickOn_ContinueToCheckout();
}

@When("^enter personal details onn checkout page$")
public void enter_personal_details_onn_checkout_page() throws Throwable {


    Checkoutpage checkoutPage = new Checkoutpage(driver);
    checkoutPage.fill_PersonalDetails();    
}

@When("^select same delivery address$")
public void select_same_delivery_address() throws Throwable {
    Checkoutpage checkoutPage = new Checkoutpage(driver);
    checkoutPage.check_ShipToDifferentAddress(false);
}

@When("^select payment method as \"([^\"]*)\" payment$")
public void select_payment_method_as_payment(String arg1) throws Throwable {
    Checkoutpage checkoutPage = new Checkoutpage(driver);
    checkoutPage.select_PaymentMethod("CheckPayment");
}

@When("^place the order$")
public void place_the_order() throws Throwable {
    Checkoutpage checkoutPage = new Checkoutpage(driver);
    checkoutPage.check_TermsAndCondition(true);
    checkoutPage.clickOn_PlaceOrder();

    driver.quit();
}
}
Run Code Online (Sandbox Code Playgroud)

这是我的 PageObject 文件

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

public class HomePage {

WebDriver driver;

public HomePage(WebDriver driver) {
    this.driver = driver;
    PageFactory.initElements(driver, this);
}

@FindBy(how=How.XPATH, using="//a[@class='noo-search icon_search']")
private WebElement click_on_search_icon;

@FindBy(how = How.XPATH, using="//input[@class='form-control']")
private WebElement enter_data_for_search;

public void perform_Search(String search) {

    click_on_search_icon.click();
    enter_data_for_search.sendKeys(search);
    enter_data_for_search.sendKeys(Keys.ENTER);

}

public void navigateTo_HomePage() {
    driver.get("http://www.shop.demoqa.com");
}

}
Run Code Online (Sandbox Code Playgroud)

**运行功能文件时遇到的错误堆栈跟踪:

java.lang.NullPointerException
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy13.click(Unknown Source)
    at pageObjects.HomePage.perform_Search(HomePage.java:27)
    at stepDefinations.EndtoEndTest.he_searches_for(EndtoEndTest.java:39)
    at ?.When he searches for "dress"(src/test/resources/functionalTest/EndtoEndTest.feature:9)
Run Code Online (Sandbox Code Playgroud)

我不确定为什么我会收到空​​指针异常。任何帮助,将不胜感激。谢谢**

Deb*_*anB 5

您将看到NullPointerException,因为您已经声明了一个WebDriver实例,如下所示:

WebDriver driver;
Run Code Online (Sandbox Code Playgroud)

但是在user_is_on_Homepage()您启动了另一个WebDriver实例的功能中继续前进,如下所示:

WebDriver driver = new ChromeDriver();
Run Code Online (Sandbox Code Playgroud)

由于您的所有函数都将使用WebDriver的相同实例,因此您需要使用您全局声明的WebDriver实例。

解决方案

更改行:

WebDriver driver = new ChromeDriver();
Run Code Online (Sandbox Code Playgroud)

到 :

driver = new ChromeDriver();
Run Code Online (Sandbox Code Playgroud)