简体   繁体   English

如何将 Cucumber 功能文件中的场景名称作为 Cucumber 步骤中的参数传递?

[英]How to pass Scenario name from the Cucumber feature file as a parameter in the Cucumber steps?

I need some help, would appreciate if someone can help?我需要一些帮助,如果有人可以提供帮助,将不胜感激?

I need pass the Scenario Name in the Cucumber feature file as a parameter in the steps..我需要将 Cucumber 功能文件中的场景名称作为步骤中的参数传递..

In the Background step- I am launching the browser and logging in to the application, so that I do not have to repeat the same steps in every scenario.在后台步骤中 - 我正在启动浏览器并登录到应用程序,这样我就不必在每个场景中重复相同的步骤。 There is a JAVA method to start the video recording for the GUI which is being used in the Background test- and the video recording will be there for individual scenarios- So if there are 10 scenarios in the feature file- the video recording needs to give 10 outputs which shows the automation run for these 10 scenarios.有一个 JAVA 方法来启动用于后台测试的 GUI 的视频录制-并且视频录制将针对各个场景-因此,如果功能文件中有 10 个场景-视频录制需要给出10 个输出显示了这 10 个场景的自动化运行。 The method for video recording capture saves the file name based on the argument which will be passed.视频录制捕获方法根据将传递的参数保存文件名。

For example- My Scenario in the Feature file is:例如-功能文件中的我的场景是:

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

How can i pass the Scenario name as a parameter in the Step?如何在步骤中将场景名称作为参数传递?

Starting a recording of the test execution is not something that belongs in your cucumber test.开始记录测试执行不属于您的 cucumber 测试。 And as you have found, it is very difficult to accomplish what you want.正如你所发现的,要完成你想要的事情是非常困难的。

This is where Cucumber Hooks can help:这就是Cucumber Hooks可以提供帮助的地方:

@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();
    }
}

Before the scenario starts, begin the video recording.在场景开始之前,开始录制视频。 The TestRecorder class can declare private fields to hold a reference to the video recorder. TestRecorder class 可以声明私有字段来保存对录像机的引用。 The Scenario object passed in to the before and after scenario hooks (BeginRecording and StopRecording) give you information about the scenario, including the name. Scenario object 传递给场景挂钩(BeginRecording 和 StopRecording)的前后挂钩(BeginRecording 和 StopRecording)为您提供有关场景的信息,包括名称。 This should give you enough to save the video recording to a file using the scenario name as the file name.这应该足以让您将视频录制保存到使用场景名称作为文件名的文件中。

And since this is just a POJO, you could write some unit tests for the video recording functionality too (if you really wanted).由于这只是一个 POJO,你也可以为视频录制功能编写一些单元测试(如果你真的想要的话)。

Now your cucumber tests can stay focused on the system under test, and the not system monitoring the test:现在您的 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

No need to pass the scenario name from the feature file.无需从功能文件中传递场景名称。 It's all done "behind the scenes" in the cucumber hooks.这一切都是在 cucumber 挂钩中“在幕后”完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM