为Cucumber Java中的方案大纲示例运行一次背景 [英] Running background once for scenario outline examples in Cucumber Java

查看:104
本文介绍了为Cucumber Java中的方案大纲示例运行一次背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在以下情况下运行一次背景,以便在执行背景时,它不应使用户再次登录屏幕。

I want to run the background once for the below scenario so that while executing it, it should not go back the user to login screen again.

我发现了一些答复说这是黄瓜的工作原理,但找不到其他替代方法。

I found some replies where it says this is how cucumber works but could not find any alternative to do this.

处理此问题的最佳方法是什么?如果有人可以为此共享示例代码。

What is the best way to handle this and how? if someone can share example code for this.

例如

Background:
    Given User logs into the application and on the home page

Scenario outline:
    When user fills the form "<FName>" and "<LName>"
    And click on submit button
    Then Result should display

    Examples:
    |FName|LName    | 
    |Abc | XYZ      |
    |Tom | Anderson |


推荐答案

您将需要在该类中设置一个静态标志包含匹配的背景步骤定义。最初将其设置为false(如果愿意,则设置为true)。在步骤定义中创建一个if条件,以检查标志的值。将其设置为相反的值,并将所需的动作放在if条件内。

You will need to setup a static flag in the class containing the matching background step definition. Initially set it to false (or true if you prefer). Create a if condition in the step definition to check the value of the flag. Set it to the opposite value and place the desired action inside the if condition. This should execute only the first time.

private static boolean flag = false;

@Given("^User Logs In$")
public void userLogsIn() {
    if(flag==false) {
        flag=true;
        //Place the code you want to run only for first time
    }
}

这篇关于为Cucumber Java中的方案大纲示例运行一次背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆