提问者:小点点

如何在Cucumber步骤中将Cucumber特征文件中的场景名作为参数传递?


我需要一些帮助,如果有人能帮忙,将不胜感激?

我需要将Cucumber特征文件中的场景名作为步骤中的参数传递..

在后台步骤中,我启动浏览器并登录到应用程序,这样我就不必在每个场景中重复相同的步骤。有一种JAVA方法可以为后台测试中使用的GUI启动视频录制-视频录制将针对各个场景-因此,如果功能文件中有10个场景,视频录制需要提供10个输出,以显示这10个场景的自动化运行。视频录制捕获的方法根据将传递的参数保存文件名。

例如-功能文件中的我的场景是:

Feature: Do Something

Background:
    Given I start the recording for the scenario "Pass the scenario name here"
    And I navigate to the login page
    When I submit username and password
    Then I should be logged in 

Scenario: Scenario Name
    Given I start the test for "Scenario Name"
    Then it should do something
    And stop the recording

Scenario: Scenario Name 2
    Given I start the test for "Scenario Name 2"
    Then it should do something
    And stop the recording

如何在步骤中将场景名称作为参数传递?


共1个答案

匿名用户

开始记录测试执行并不属于你的cucumber测试。正如你所发现的,完成你想要的是非常困难的。

这就是cucumber挂钩的用处:

@Binding
public class TestRecorder {
    private final VideoRecorder videoRecorder;

    public TestRecorder() {
        this(new VideoRecorder(...));
    }

    public TestRecorder(VideoRecorder videoRecorder) {
        this.videoRecorder = videoRecorder;
    }

    @Before
    public void BeginRecording(Scenario scenario) {
        String scenarioName = scenario.getName();

        // do something with scenarioName and start recording...
        videoRecorder.start();
    }

    @After
    public void StopRecording(Scenario scenario) {
        String scenarioName = scenario.getName();

        // Stop recording, and use scenarioName to save to a file
        videoRecorder.stop();
    }
}

在场景开始之前,开始视频录制。TestRecorder类可以声明私有字段来保存对录像机的引用。传递给场景前和场景后钩子的Scenario对象为您提供有关场景的信息,包括名称。这应该足以让您使用场景名称作为文件名将视频录制保存到文件中。

因为这只是一个POJO,你也可以为视频录制功能编写一些单元测试(如果你真的想的话)。

现在,cucumber测试可以专注于被测系统,而不是监控测试的系统:

Feature: Do Something

Background:
    Given I navigate to the login page
    When I submit username and password
    Then I should be logged in 

Scenario: Scenario Name
    When I do the thing
    Then it should do something

Scenario: Scenario Name 2
    When I do the thing
    Then it should do something

不需要从特征文件中传递场景名称。这一切都是在cucumber挂钩的“幕后”完成的。