有没有办法在黄瓜中使用 eventPublisher 读取数据表和场景示例

Is there a way to read the datatable and examples of a scenario using eventPublisher in cucumber

我需要解析我正在使用事件发布者的黄瓜功能。但是我无法读取步骤的数据 table 以及场景附带的示例。

public class DryRunPlugin implements EventListener {
@Override
public void setEventPublisher(EventPublisher publisher) {
    publisher.registerHandlerFor(TestCaseStarted.class, this::handleCaseStarted);
}

private void handleCaseStarted(TestCaseStarted event) {
 
    Iterator steps = event.getTestCase().getTestSteps().iterator();
    while(steps.hasNext())
    {
        Object stp = steps.next();
        if(stp instanceof PickleStepTestStep)
        {
            PickleStepTestStep p = (PickleStepTestStep) stp;
            String keyword = p.getStep().getKeyWord().toString();
            String stepText = p.getStep().getText();
            System.out.println(keyword + stepText);

            Object dataTable = p.getStep().getArgument();
            System.out.println(dataTable.getClass());
            if(dataTable !=null)
            {
                //GherkinVintageDataTableArgument arg = (GherkinVintageDataTableArgument)dataTable;
                //Object Rows = (dataTable.cells();
            }
            
        }

    }
}

}

当我尝试使用 GherkinVintageDataTableArgument 时,它给我一个错误,因此无法访问要打印的数据table。我正在使用黄瓜版本 5.5

错误: io.cucumber.core.gherkin.vintage.GherkinVintageDataTableArgument 不是 io.cucumber.core.gherkin.vintage 中的 public。包外无法访问

Step.getArgument()的签名显示:

@API(status = API.Status.STABLE)
public interface Step {

    /**
     * Returns this Gherkin step argument. Can be either a data table or doc
     * string.
     *
     * @return a step argument, null if absent
     */
    StepArgument getArgument();

然后查看 StepArgument 你会看到一个带有两个子接口的空接口,DataTableArgumentDocStringArgument

@API(status = API.Status.STABLE)
public interface StepArgument {

}

你会想要使用:

package io.cucumber.plugin.event;

import org.apiguardian.api.API;

import java.util.List;

/**
 * Represents a Gherkin data table argument.
 */
@API(status = API.Status.STABLE)
public interface DataTableArgument extends StepArgument {

    List<List<String>> cells();

    int getLine();

}

您可能还看到了接口的几个具体实现,例如 GherkinVintageDataTableArgument 但您无法访问它们,因为它们是 package private.

这是故意的。

一个插件应该只需要使用 类 和来自 cucumber-plugin 模块的接口。这确保您不依赖于 Cucumber 的内部实现细节,并且您的插件在升级时可能会正常工作。所以请尝试使用 io.cucumber.plugin.event.DataTableArgument 代替。

I have a requirement to parse the cucumber features for which I am using event publisher.

根据您实际尝试实现的目标,Cucumber 提供的保真度可能还不够。考虑在 TestSourceRead 事件提供的源上使用小黄瓜解析器。