正在运行黄瓜挂钩,但未运行测试

时间:2019-06-04 14:48:28

标签: java cucumber cucumber-jvm

我收到错误消息:cucumber.runtime.CucumberException: Failed to instantiate class steps.MyStepdefs

这就是我想要做的。我的钩子位于软件包steps中:

public class hooks {
    public static WebDriver webDriver;

    @Before
    public static void ChromeDriverSetup() {

        System.out.println("Creating new ChromeDriver instance...");
        webDriver = new ChromeDriver();

        System.out.println("Hello from hooks!");
    }

以上已执行...

但是MyStepdefs测试类无法执行(它也位于steps包中),并且出现上述错误。

public class MyStepdefs {
   ProductPage productPageObjects = new ProductPage();


    @Given("I purchase {int} items of the same product")
    public void iPurchaseItemsOfTheSameProduct(int qty)  {

        System.out.println("Hello from MySteps!");
        productPageObjects.Visit();
        productPageObjects.ClickPlusQtyElement(qty);
    }
package pageobjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import static steps.hooks.webDriver;

public class ProductPage {

    private WebElement totalQtyElement = webDriver.findElement(By.cssSelector(".sanitized"));
    private WebElement plusQtyElement = webDriver.findElement(By.cssSelector(".sanitized"));

    public void Visit() {
        webDriver.get("https://www.example.com");
    }


    public String ClickPlusQtyElement(int qty) {

        int minAmount = 1;
        while (minAmount < qty)
        {
            plusQtyElement.click();
            minAmount ++;

        }
        System.out.println("The amount is now: " + totalQtyElement.getText());

        return totalQtyElement.getText();
    }
}

在IntelliJ中,我的glue被设置为steps。我在RunCucumberTest包中也有一个steps类。

package steps;

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

@RunWith(Cucumber.class)
@CucumberOptions(tags = "not @ignore", plugin = {"pretty", "html:target/cucumber"})
public class RunCucumberTest {}

为什么它不执行MyStepsdefs

stacktrace:https://pastebin.com/X5KhHfuP

更新:当我注释掉对ProductPage的调用时,行System.out.println("Hello from MySteps!");会被执行。因此,该特定呼叫存在问题。

3 个答案:

答案 0 :(得分:1)

好吧,我知道了。当我尝试实例化类ProductPage时,由于Web驱动程序调用{​​{1}}

而导致错误

问题是我还没有访问URL!因此,我将把以上内容放入方法中并进行一些重构。

答案 1 :(得分:0)

如果您分析堆栈跟踪,那么我们会在类 ProductPage.java

中的第16行发生 NullPointerException
Caused by: java.lang.NullPointerException
    at pageobjects.ProductPage.<init>(ProductPage.java:16)
    at steps.MyStepdefs.<init>(MyStepdefs.java:15)
    ... 18 more

请检查上一行的初始化,因为下面的代码可能在 productPageObjects.Visit();

public class MyStepdefs {

    @Given("I purchase {int} items of the same product")
    public void iPurchaseItemsOfTheSameProduct(int qty)  {

        System.out.println("Hello from MySteps!");
        productPageObjects.Visit();
        productPageObjects.ClickPlusQtyElement(qty);
    }

答案 2 :(得分:0)

问题出在您的ProductPage中。当您实例化这两个Web元素字段时:

private WebElement totalQtyElement = webDriver.findElement(By.cssSelector(".sanitized"));
private WebElement plusQtyElement = webDriver.findElement(By.cssSelector(".sanitized"));

您会得到一个空指针异常。

为什么?因为那时@Before钩子还没有运行,并且当您尝试实例化ProductPage时,您的webDriver仍然为空。

我建议将所有这些调用(webDriver.findElement)移到步骤定义内或从步骤定义中调用的方法内。这样,您可以确保实例化的顺序不会引起问题。

相关问题