How to use Jasmine and CucumberJS with Protractor

后端 未结 2 445
北荒
北荒 2020-12-08 21:51

I\'m looking to use Protractor, CucumberJS, and Jasmine for testing my project. How do I use both Jasmine and CucumberJS with Protractor? Here\'s the project setup I\'ve cre

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 22:21

    CucumberJS and Jasmine are mutually exclusive; you won't be able to use Jasmine's expects in Cucumber steps. What you have to do instead is load a separate expectation module. I would suggest Chai with the chai-as-promised plugin. (chai-as-promised simplifies the process of writing expectations around promises. Protractor overrides the expect() function in Jasmine to do this for you behind the scenes) Most likely you'll want to do this in your World as that's the easiest way to provide access to it in your Step Definitions. Your World would look something like this:

    var World, chai, chaiAsPromised;
    chai = require('chai');
    chaiAsPromised = require('chai-as-promised');
    
    World = function World(callback) {
      chai.use(chaiAsPromised);
      this.expect = chai.expect;
      callback();
    }
    
    module.exports.World = World;
    

    Then in your Step Definitions file you just load in the World per the CucumberJS documentation and you're Step Definitions will be scoped to provide access to all properties of the World:

    module.exports = function() {
    
      this.World = require("path/to/world.js").World;
    
      this.Given(/^some precondition$/, function (callback) {
        this.expect(true).to.equal(true);
        callback();
      });
    };
    

    Now, for some shameless self-promoting: if you're using Protractor with CucumberJS, I would recommend looking at a module I helped build called CukeFarm. It comes preconfigured with a few modules you'll find useful and it provides a number of general Step Definitions that can be used on most any project.

提交回复
热议问题