gpt4 book ai didi

java - cucumber 钩子(Hook)正在运行,但没有测试

转载 作者:行者123 更新时间:2023-12-02 09:49:05 26 4
gpt4 key购买 nike

我收到错误:cucumber.runtime.CucumberException:无法实例化类steps.MyStepdefs

这就是我正在尝试做的事情。我的钩子(Hook)位于包 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。我在 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 {}

为什么它不执行MyStepsdefs

堆栈跟踪:https://pastebin.com/X5KhHfuP

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

最佳答案

好吧,我明白了。当我尝试实例化类 ProductPage 时,由于 Web 驱动程序调用而出现错误,即 private WebElement TotalQtyElement = webDriver.findElement(By.cssSelector(".sanitized"));

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

关于java - cucumber 钩子(Hook)正在运行,但没有测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56446286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com