【问题标题】:why cucumber protractor does not totally ignore the feature file that does not have the tag to run?为什么黄瓜量角器不完全忽略没有标签运行的特征文件?
【发布时间】:2019-05-03 03:35:16
【问题描述】:

我正在使用黄瓜量角器框架来运行功能文件。

在我的 config.js 中,我有:

specs: [
    "../../features/XXX1.feature",
    "../../features/XXX2.feature",
    ... 
    "../../features/XXXn.feature",                            
],

cucumberOpts: {
    tags: "@mytag",
},

在我的功能文件 XXX1.feature 中,我设置了这个标签“@mytag”:

  @mytag
  Scenario Outline: my Flow
    Given I am running test case one
    ....

但不在任何其他功能文件中,如 XXX2.feature、XXX3.feature 等。

我希望量角器仅运行 XXX1.feature,而不运行 XXX2.feature。确实如此,当涉及到 XXX2.feature 时,它​​启动浏览器,什么也不做,然后输出以下内容:

[14:35:53] I/testLogger - [chrome #01-2] PID: 14272
[chrome #01-2] Specs: D:\ptfbc\ui\features\XXX2.feature
[chrome #01-2]
[chrome #01-2] [14:35:44] I/hosted - Using the selenium server at http://127.0.0.1:4444/wd/hub
[chrome #01-2]
[chrome #01-2]
[chrome #01-2] 0 scenarios
[chrome #01-2] 0 steps
[chrome #01-2] 0m00.000s

但它仍然不够好。由于 XXX2.feature 中没有标签“@mytag”。它不应该跳过功能文件 XXX2.feature 并且根本不启动浏览器吗?

为每个没有“@mytag”的不合格功能文件启动浏览器也很耗时。

有没有可以避免这种情况的配置方式?

编辑

功能和 hook.ts

capabilities: {
    browserName: "chrome",
    shardTestFiles: true,
    maxInstances: 1,
    'chromeOptions': {
        'args': [
            'disable-infobars'//,'headless=true','disable-gpu=true',
        ],
        'prefs': {
            'credentials_enable_service': false,
            'download': {
                'prompt_for_download': false,
                'directory_upgrade': true,
            }
        }
    }
},


const { BeforeAll, After, Status } = require("cucumber");
import * as fs from "fs";
import { browser } from "protractor";
import { config } from "../config/config";  

BeforeAll({timeout: 300 * 1000}, async () => {
    await browser.get(config.baseUrl);
});

After(async function(scenario) {
        // screenShot is a base-64 encoded PNG
         const screenShot = await browser.takeScreenshot();
         this.attach(screenShot, "image/png");
});

【问题讨论】:

  • 你把启动浏览器的代码放在哪里了?如果 beforeAll 钩子中有它的代码,那可能就是原因。
  • 你能把你的钩子代码贴在这里
  • 您是否使用过shardTestFiles: truemaxInstances: <greater than 1>,请在您的量角器conf.js 中发布capabilitiesmultiCapabilities(如果已设置)
  • @yong 是的,我确实使用了 shardTestFiles 和 maxInstances,maxInstances 为 1
  • @KyleFairns 嗨,凯尔,请查看我的更新。 beforeAll 只是在做一个 browser.get()。但我的问题是,如果功能文件没有要运行的标签,应该完全跳过它,甚至不运行 beforeAll

标签: protractor cucumberjs


【解决方案1】:

对于Protractor执行特征文件的过程,可以分为两个阶段。

Protractor 为每个特征文件打开浏览器实例(创建会话)作为第一阶段,然后 Protractor 会将正在运行的任务交给黄瓜作为第二阶段。

在第二阶段,cucumber 会检测特征文件是否满足标签。如果没有,cucumber 将不会为功能文件运行任何场景,您将在控制台中获得0 scenarios, 0 steps

否则,cucumber 将执行那些满足特征文件中标记的场景。

因为量角器不负责在打开浏览器之前检测特征文件是否满足标签,因此正如你所看到的a browser opened and closed without any operation

唯一的解决方案是在 Protractor conf.js 中提供满足标签的 精确 specs。为此,您需要根据标签过滤功能文件,然后将过滤结果分配给specs

我在github做了一个规格过滤器

// general config.js
exports.config = {
   specs:[
      './features/**/*.feature'
   ],

   cucubmerOpts: {
      tags: '@abc'
   }
};


// conf.js use my filter
var config = {
   specs:[
      './features/**/*.feature'
   ],

   cucubmerOpts: {
      tags: '@abc'
   }
};

exports.config = require('./spec.filter.js').filter_by_tag(config);

【讨论】:

  • 这意味着你自己编写'./spec.filter.js',对吗?如果量角器有内置的方法可以做到这一点,那就太好了。但是非常感谢您的解释。
  • 是的,我是根据我的项目需要制作的。据我所知,量角器不提供这样的钩子来做到这一点,像beforeLaunchonPrepare 这样的存在钩子将在 conf.js 加载到运行时后被调用。但是在将 conf.js 加载到运行时之前,我们需要一个钩子来更改规范。 (因为量角器在运行时没有暴露任何API来修改config对象。)
【解决方案2】:

我遇到了同样的问题,删除后问题解决了

shardTestFiles: true

【讨论】:

    【解决方案3】:

    您犯了一个小错误,将其更改为 Before 而不是 beforeAll。

    并确保您有一个给定的小黄瓜,以便每个场景导航到主页或网址。

    Given I am navigating to homepage
    

    这将有你的browser.navigate(url)

    【讨论】:

    • 您可以尝试给我们反馈吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 2019-10-16
    相关资源
    最近更新 更多