11

As of 0.20.1 Cucumber is now fully supported in Protractor but I'm battling to find any documentation on how to configure it properly. Any idea how you would setup world.js?

I have found this example at https://github.com/whyvez/angular-cucumber-example/blob/master/features/support/world.coffee but I'm not sure if you would still need to specify all the require modules and configuration as the protractor config file (referenceConf.js) would have all this info already.

assert = require 'assert'
path = require 'path'

protractor = require 'protractor'
webdriver = require 'selenium-webdriver'

driver = new webdriver.Builder().
  usingServer('http://localhost:4444/wd/hub').
  withCapabilities(webdriver.Capabilities.chrome()).
  build()

driver.manage().timeouts().setScriptTimeout(100000)

ptor = protractor.wrapDriver driver

class World
  constructor: (callback) ->
    @browser = ptor
    @By = protractor.By
    @assert = assert
    callback()

module.exports.World = World
dex
  • 275
  • 1
  • 4
  • 10

4 Answers4

13

I have created a sample project to show how to configure Protractor with Cucumber and make use of the World.

The World is a place to share commonalities between different scenarios so that you can keep you code organised.

Actually, all you need is to create your world.js file in a folder called /support under /features. You would place there your hooks as well. Every property or function there will be available in your step definitions.

world.js:

module.exports = function() {

  this.World = function World(callback) {
    this.prop = "Hello from the World!";

    this.greetings = function(name, callback) {
      console.log("\n----Hello " + name);
      callback();
    };

    callback();
}

And then in your steps:

var sampleSteps = function() {

    this.Given(/^this is the first sample$/, function (callback) {
      console.log("\n----" + this.prop);
      callback();
    });

    this.Given(/^this is the second sample$/, function (callback) {
      this.greetings("everybody", callback);
    });

};

module.exports = sampleSteps;

Your protractor.js configuration file would look something like this:

exports.config = {

  specs: [
    'e2e/features/*.feature'
  ],

  capabilities: {
    'browserName': 'chrome'
  },

  baseUrl: 'http://localhost:8081/',

  framework: 'cucumber',

};

This the GitHub repository.

https://github.com/plopcas/st-protractor-cucumber

Hope this helps.

Pedro Lopez
  • 2,236
  • 2
  • 19
  • 27
  • Pedro, i have used your example to make my test work but when i use expect it fails. which library shall i use for expect in this case? – Kamran Pervaiz May 26 '15 at 10:23
  • Difficult to tell without seeing the error, but I guess you are trying to use the "expect" which is a Jasmine global function. But given that you are using Cucumber here, you do not have that function. I like to use Chai.js. It is pretty good assertion library. All you need is var chai = require('chai'); var expect = chai.expect; and then use it in your steps. You might need to use "chai-as-promised" if you are using promises. Take into account that the syntax is different than the Jasmine-based expect function. – Pedro Lopez May 26 '15 at 21:31
  • thanks Pedro, i got away from using expect and using if condition to meet the criteria and then callback(). e.g. if (data && data.indexOf("World") > 0) callback(); else callback.fail(new Error("Expected to be on page with searchTerm " + searchTerm)); – Kamran Pervaiz May 27 '15 at 09:32
  • Hello All! I've been trying to run the tests, but I keep getting this: ``` launcher] Error: TypeError: undefined is not a function at /usr/local/lib/node_modules/protractor/lib/frameworks/cucumber.js:132:36 at Function.promise (/usr/local/lib/node_modules/protractor/node_modules/q/q.js:650:9) ``` – Bruno Soko Oct 25 '15 at 23:18
  • For my previous comment, I'm trying to run with protractor path/to/config.js – Bruno Soko Oct 25 '15 at 23:44
1

Take a look at protractor-cucumbe -- it comes with selenium-webdriver, supports Promises, and is well documented.

It seems to require minimal configuration, and what is required is clearly documented.

Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
0

I've gotten good milage from this setup

  class ChtWorld
    chai = require('chai');
    chaiAsPromised = require('chai-as-promised');

    constructor:  ->
      @browser = @protractor = require('protractor').getInstance()
      @By = @protractor.By
      chai.use(chaiAsPromised)
      @expect= chai.expect


  module.exports= ->
    this.World= (callback) ->
      w = new ChtWorld()
      callback(w)

Since protractor is already setup, just getting a reference to it is sufficient (note that for Cucumber to correctly load up the new world, the modules.exports has to be just right).

As a side note, this is located in features/support/world.coffee, and not explicitly added to the 'requires' list (trying to do that got me into Gherkin Lexing error problems).

fourpastmidnight
  • 4,032
  • 1
  • 35
  • 48
aabes
  • 208
  • 1
  • 12
0

Add it as a framework in the configuration file:

exports.config = {
  // set to "custom" instead of cucumber.
  framework: 'custom',

  // path relative to the current config file
  frameworkPath: 'protractor-cucumber-framework'

  // relevant cucumber command line options
  cucumberOpts: {
    format: "summary"
  }
};

More information here: Protractor Frameworks

Lee Goddard
  • 10,680
  • 4
  • 46
  • 63