[英]Cucumber hooks are being run, but not the tests
I'm getting the error: cucumber.runtime.CucumberException: Failed to instantiate class steps.MyStepdefs
我收到错误消息:
cucumber.runtime.CucumberException: Failed to instantiate class steps.MyStepdefs
Here's what I'm trying to do. 这就是我想要做的。 My hooks are located in the package
steps
: 我的钩子位于包装
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!");
}
The above is executed... 上面执行了...
But the MyStepdefs
test class does not execute (it is also in the steps
package) & I get the above error. 但是
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();
}
}
In IntelliJ, my glue
is set as steps
. 在IntelliJ中,我的
glue
设置为steps
。 I also have a RunCucumberTest
class in the steps
package. 我在
steps
包中还有一个RunCucumberTest
类。
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 {}
Why does it not execute MyStepsdefs
? 为什么不执行
MyStepsdefs
?
stacktrace: https://pastebin.com/X5KhHfuP 堆栈跟踪: https : //pastebin.com/X5KhHfuP
Update : When I comment out calls to ProductPage
the line System.out.println("Hello from MySteps!");
更新 :当我注释掉对
ProductPage
的调用时,行System.out.println("Hello from MySteps!");
does get executed. 确实被执行。 So there is an issue with that particular call.
因此,该特定呼叫存在问题。
Ok I figured it out. 好吧,我知道了。 When I try to instantiate the class
ProductPage
, I get an error due to the web driver calls ie private WebElement totalQtyElement = webDriver.findElement(By.cssSelector(".sanitized"));
当我尝试实例化类
ProductPage
,由于Web驱动程序调用而导致错误,即private WebElement totalQtyElement = webDriver.findElement(By.cssSelector(".sanitized"));
The problem is that I haven't visited a URL yet! 问题是我还没有访问URL! So, I am going to put the above in a method and do some refactoring.
因此,我将把以上内容放入方法中并进行一些重构。
If you analyse the stacktrace then we see NullPointerException being occurred at 16 line with in Class ProductPage.java 如果您分析堆栈跟踪,那么我们将在类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
Please check the initialization at the above line as probably that reference is being used in below code at line productPageObjects.Visit(); 请检查上一行的初始化,因为以下代码可能在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);
}
The problem is in your ProductPage. 问题出在您的ProductPage中。 When you are instantiating those 2 web elements fields :
实例化这两个Web元素字段时:
private WebElement totalQtyElement = webDriver.findElement(By.cssSelector(".sanitized"));
private WebElement plusQtyElement = webDriver.findElement(By.cssSelector(".sanitized"));
you get a null pointer exception. 您会得到一个空指针异常。
Why? 为什么? Because at that moment the
@Before
hook hasn't run and your webDriver
is still null when you are trying to instantiate your ProductPage. 因为那时
@Before
挂钩尚未运行,并且在尝试实例化ProductPage时,您的webDriver
仍然为null。
I'd suggest moving all these calls ( webDriver.findElement
) inside a step definition or inside a method that is called from a step definition. 我建议将所有这些调用(
webDriver.findElement
)移动到步骤定义内或从步骤定义调用的方法内。 This way you can be sure that the ordering of the instantiation will not cause you problems. 这样,您可以确保实例化的顺序不会引起问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.