Ema.jar
Ema.jar

Reputation: 2424

Protractor: call a step definition inside another step definition

I have this step definition that check if an element, identified by a CSS selector, is present in the page:

this.Then(/The element with selector "([^"]*)" should be present/, function(elementSelector, callback){
    var selectedElement = element(by.css(elementSelector));

    expect(selectedElement.isPresent()).to.eventually.equal(true, "Can't find the element with selector '" + elementSelector + "' that should be present").and.notify(callback);
 });

I would like to know if it's possible to call this step definition inside another step definition. This approach could be useful, for example, in a step definition used to fill a text area. In this case I would like to know if the text area exists in page before trying to fill it.

Thank you in advance!

Upvotes: 3

Views: 245

Answers (1)

Nathaniel C
Nathaniel C

Reputation: 564

Old question, but for people finding this later...

The creators of Cucumber were enthusiastic about nesting steps in the early years, but they later repented themselves of ever creating the feature, and instead suggest composing functions together. This is particularly easy to do in javascript, per Aslak's example:

function x_is_something(x, cb) {
  console.log('x', x);
  cb();
}

Given(/x is (.*)/, x_is_something);

Given(/super x/, function(cb) {
  x_is_something(97, cb);
});

This is why many of the variants of the original Cucumber-Ruby have never supported this feature, and their creators have even discussed removing it from the ones that do.

Upvotes: 1

Related Questions