是否可以在运行时跳过使用Cucumber-JVM的方案 [英] Is it possible to skip a scenario with Cucumber-JVM at run-time

查看:238
本文介绍了是否可以在运行时跳过使用Cucumber-JVM的方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在方案中添加标签@skiponchrome,当使用Chrome浏览器运行Selenium测试时,应该跳过该方案。这样做的原因是因为某些方案在某些环境中可以工作,而在其他环境中则不能,这甚至可能不是特定于浏览器测试的,并且可以应用于其他情况,例如OS平台。

I want to add a tag @skiponchrome to a scenario, this should skip the scenario when running a Selenium test with the Chrome browser. The reason to-do this is because some scenario's work in some environments and not in others, this might not even be browser testing specific and could be applied in other situation for example OS platforms.

示例钩子:

@Before("@skiponchrome") // this works
public void beforeScenario() {
  if(currentBrowser == 'chrome') { // this works
    // Skip scenario code here
  }
}

我知道可以在黄瓜标签中定义〜@ skiponchrome来跳过该标签,但是我想在运行时跳过一个标签,时间。这样,我不必考虑在特定环境下开始测试时要跳过哪些步骤。

I know it is possible to define ~@skiponchrome in the cucumber tags to skip the tag, but I would like to skip a tag at run-time. This way I don't have to think about which steps to skip in advance when I starting a test run on a certain environment.

我想创建一个钩子来捕获标记并跳过该方案,而不报告失败/错误。

I would like to create a hook that catches the tag and skips the scenario without reporting a fail/error. Is this possible?

推荐答案

我也遇到了同样的挑战,在这种情况下,我需要跳过基于标志运行的方案我可以在运行时动态地从应用程序中获取该信息,它告诉您是否在应用程序上启用了要测试的功能。.

I too had the same challenge, where in I need to skip a scenario from running based on a flag which I obtain from the application dynamically in run-time, which tells whether the feature to be tested is enabled on the application or not..

所以这就是我写的方式我在场景文件中的逻辑,其中有每一步的粘合代码。

so this is how I wrote my logic in the scenarios file, where we have the glue code for each step.

我使用了唯一的标签'@ Feature-01AXX'来标记需要的场景

I have used a unique tag '@Feature-01AXX' to mark my scenarios that need to be run only when that feature(code) is available on the application.

因此,对于每种情况,都将首先检查标记'@ Feature-01XX',如果它的功能(代码)在应用程序上可用。如果存在,则将检查功能的可用性,然后才会选择该方案以运行。否则,它将仅被跳过,并且Junit不会将其标记为失败,而是将其标记为Pass。因此,如果由于该功能不可用而无法进行这些测试,则最终结果将通过,这很酷。

so for every scenario, the tag '@Feature-01XX' is checked first, if its present then the check for the availability of the feature is made, only then the scenario will be picked for running. Else it will be merely skipped, and Junit will not mark this as failure, instead it will me marked as Pass. So the final result if these tests did not run due to the un-availability of the feature will be pass, that's cool...

@Before
public void before(final Scenario scenario) throws Exception {
    /*
        my other pre-setup tasks for each scenario.
    */

    // get all the scenario tags from the scenario head.
    final ArrayList<String> scenarioTags = new ArrayList<>();
    scenarioTags.addAll(scenario.getSourceTagNames());

    // check if the feature is enabled on the appliance, so that the tests can be run.
    if (checkForSkipScenario(scenarioTags)) {
        throw new AssumptionViolatedException("The feature 'Feature-01AXX' is not enabled on this appliance, so skipping");
    }
}

private boolean checkForSkipScenario(final ArrayList<String> scenarioTags) {
    // I use a tag "@Feature-01AXX" on the scenarios which needs to be run when the feature is enabled on the appliance/application
    if (scenarioTags.contains("@Feature-01AXX") && !isTheFeatureEnabled()) { // if feature is not enabled, then we need to skip the scenario.
        return true;
    }
    return false;
}

private boolean isTheFeatureEnabled(){
    /*
        my logic to check if the feature is available/enabled on the application.
        in my case its an REST api call, I parse the JSON and check if the feature is enabled.
        if it is enabled return 'true', else return 'false'
    */
}

这篇关于是否可以在运行时跳过使用Cucumber-JVM的方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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