6

I am using a cucumber protractor framework to run feature files.

In my config.js, i have:

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

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

And in my feature file XXX1.feature, i have this tag '@mytag' set:

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

but not in any other feature files like XXX2.feature, XXX3.feature etc.

I am expecting protractor to run XXX1.feature only, and not running XXX2.feature. It kind of does, when comes to XXX2.feature, it starts the browser, doing nothing, and then output following:

[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

but it's still not good enough. Since there's no tag '@mytag' in XXX2.feature. Shouldn't it skip the feature file XXX2.feature and NOT START THE BROWSER at all?

Starting browser for every ineligible feature file that does not have '@mytag' is also time consuming.

Is there a way of configuration that can avoid this?

Edit

capabilities and 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");
});
halfer
  • 19,824
  • 17
  • 99
  • 186
user1559625
  • 2,583
  • 5
  • 37
  • 75

3 Answers3

1

For the process of Protractor executing a feature file, it can be split into two stages.

Protractor open browser instance (create session) for each feature file as the first stage, then Protractor will hand over the running task to cucumber as the second stage.

In the second stage, cucumber will detect the feature file whether satisfy the tag. If not, cucumber won't run any scenario for the feature file and you will get 0 scenarios, 0 steps in console.

Otherwise, cucumber will execute those scenarios satisfy the tag in feature file.

Because Protractor isn't responsible for detecting feature file satisfy the tag before it open browser, therefor as what you see a browser opened and closed without any operation.

The only solution is to give the precise specs in Protractor conf.js which satisfied the tag. In order to do that, you need to filter the feature files against tag then assign the filter result to specs.

I made a spec filter at 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);
yong
  • 13,357
  • 1
  • 16
  • 27
  • that means you write the './spec.filter.js' yourself, right? if there's an inbuilt way from protractor to do this, it would be great. But many thanks for the explanation. – user1559625 Jul 25 '18 at 07:05
  • Yes, I made it for my project need. As i know, protractor not supply such a hook to do that, the exists hook like `beforeLaunch` and `onPrepare` will be invoked after the conf.js be loaded into runtime. But we need a hook to change the specs before the conf.js be loaded into runtime. (Because protractor not expose any API to modify the `config` object in runtime.) – yong Jul 25 '18 at 07:58
1

I had the exact same issue, problem solved after removing

shardTestFiles: true

Wicky
  • 118
  • 2
  • 13
0

You are doing a small mistake change it to Before instead of beforeAll.

And make sure you have a given gherkin so that each scenario navigates to home page or url.

Given I am navigating to homepage

This will have your browser.navigate(url)

Bharath Kumar S
  • 1,410
  • 2
  • 10
  • 29