黄瓜钩子正在 运行,但不是测试
Cucumber hooks are being run, but not the tests
我收到错误: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
测试 class 没有执行(它也在 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
。我在 steps
包中也有一个 RunCucumberTest
class。
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
?
堆栈跟踪:https://pastebin.com/X5KhHfuP
Update:当我注释掉对 ProductPage
的调用时,System.out.println("Hello from MySteps!");
行确实得到执行。所以那个特定的电话有问题。
如果您分析堆栈跟踪,我们会看到 NullPointerException 发生在第 16 行 Class ProductPage.java
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 个 Web 元素字段时:
private WebElement totalQtyElement = webDriver.findElement(By.cssSelector(".sanitized"));
private WebElement plusQtyElement = webDriver.findElement(By.cssSelector(".sanitized"));
你得到一个空指针异常。
为什么?因为在那一刻 @Before
挂钩还没有 运行 并且当您尝试实例化 ProductPage 时您的 webDriver
仍然为 null。
我建议将所有这些调用 (webDriver.findElement
) 移动到步骤定义内或从步骤定义调用的方法内。这样你就可以确定实例化的顺序不会给你带来问题。
好的,我明白了。当我尝试实例化 class ProductPage
时,由于 Web 驱动程序调用,我收到一个错误,即 private WebElement totalQtyElement = webDriver.findElement(By.cssSelector(".sanitized"));
问题是我还没有去过URL!因此,我将把上面的内容放在一个方法中并进行一些重构。
我收到错误: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
测试 class 没有执行(它也在 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
。我在 steps
包中也有一个 RunCucumberTest
class。
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
?
堆栈跟踪:https://pastebin.com/X5KhHfuP
Update:当我注释掉对 ProductPage
的调用时,System.out.println("Hello from MySteps!");
行确实得到执行。所以那个特定的电话有问题。
如果您分析堆栈跟踪,我们会看到 NullPointerException 发生在第 16 行 Class ProductPage.java
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 个 Web 元素字段时:
private WebElement totalQtyElement = webDriver.findElement(By.cssSelector(".sanitized"));
private WebElement plusQtyElement = webDriver.findElement(By.cssSelector(".sanitized"));
你得到一个空指针异常。
为什么?因为在那一刻 @Before
挂钩还没有 运行 并且当您尝试实例化 ProductPage 时您的 webDriver
仍然为 null。
我建议将所有这些调用 (webDriver.findElement
) 移动到步骤定义内或从步骤定义调用的方法内。这样你就可以确定实例化的顺序不会给你带来问题。
好的,我明白了。当我尝试实例化 class ProductPage
时,由于 Web 驱动程序调用,我收到一个错误,即 private WebElement totalQtyElement = webDriver.findElement(By.cssSelector(".sanitized"));
问题是我还没有去过URL!因此,我将把上面的内容放在一个方法中并进行一些重构。