Java cucumber Selenium : NullPointerException for scenario.write for first scenario on before tag

Java cucumber Selenium : NullPointerException for scenario.write for the first scenario on before tag

我在执行下面的代码时收到 NullPointerException。 请注意,对于执行的 first 黄瓜场景,仅 收到异常。 对于 second 场景,no 例外。 我实际上是在尝试在每个场景开始之前插入时间戳。 请告知如何解决此问题。 任何帮助将不胜感激。

@Before //cucumber.api.java
public void beforeScenario(Scenario scenario1)
{
this.scenario=scenario1; //I would need this for a later use.
scenario1.write("Start Time:"+new Date()); //Nullpointerexception

}

我也试过了,还是一样的错误。

@Before //cucumber.api.java
public void beforeScenario(Scenario scenario)
{
this.scenario=scenario; //wont matter if i have this or not
scenario1.write("Start Time:"+new Date()); //Nullpointerexception

}

该方法不应是静态的。

无法找到直接答案。 但是作为替代方案:我避免在@Before

中写“场景”
@Before //cucumber.api.java
public void beforeScenario(Scenario scenario)
{
startDate = new Date(); //defined globally
}

@After//cucumber.api.java
public void afterScenario(Scenario scenario)
{
this.scenario=scenario; 
scenario1.write("StartTime:"+startDate);
scenario1.write("End Time:"+new Date());
}