Volodymyr Nabok
Volodymyr Nabok

Reputation: 473

How to return value AND promise in Protractor framework

let us suggest the function _headerSave() save to DB some Text header. We need to know both the Header and operate the correspond promise. How could we do it?

Wrong code example:

header = headerCreate(); //header is the promise but real header needed

function headerCreate(){
    var header_name = "Random Header Created";
    return _headerSave(header_name); //return promise
}

function _headerSave(header_name){
  element(by.css('button[ng-click="HeaderEditCtrl.saveHeader(true)"]')).click().then(function() {
    element.all(by.repeater('button in modal.buttons')).get(1).click(); //press Save
    browser.driver.wait( function(){
      return element(by.css('table[class="grid-table"] span[title="'+header_name+'"]')).isPresent();
    }, 5000);
  });
  return element(by.css('table[class="grid-table"] span[title="'+header_name+'"]'));
}

Upvotes: 0

Views: 381

Answers (2)

Volodymyr Nabok
Volodymyr Nabok

Reputation: 473

The easiest way to fix the problem is make the code simpler. Instead of

headerCreate().then(function(HeaderPromise){
element(by.css('div')).then(function(){
console.log(headerPromise.header_name);
});
});

better to write:

headerPromise=headerCreate();
element(by.css('div'));
console.log(headerPromise.header_name);

theControlFlow object allows to do it in this way.

Upvotes: 0

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276286

Promises are objects. Objects in JavaScript can have properties. You can add those properties dynamically.

function _headerSave(header_name){
  // ...
  var p = element(by.css('table[class="grid-table"] span[title="'+header_name+'"]'));
  p.header_name = header_name;
  return p;
}

Usage:

_headerSave(header_name).header_name; 

Upvotes: 2

Related Questions