1

I got a function which enter some values into a form. This should return a promise, so my test waits till the function is finished and the values are entered in the form. How do I do this in a protractor test?

function enterSomeValuesIntoForm() {
    // do some stuff
    element(by.id('value1')).sendKeys('hello');
    element(by.id('value2')).sendKeys('is it me you looking for?');
    element(by.id('submitButton')).click();
});
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Tim
  • 193
  • 4
  • 15
  • Possible duplicate of [creating and resolving promises in protractor](http://stackoverflow.com/questions/24289431/creating-and-resolving-promises-in-protractor) – giri-sh Dec 08 '15 at 10:03

1 Answers1

1

From what I understand, you don't actually need to return a promise from this function. All of the actions performed during the function call would be controlled and queued by the Control Flow:

WebDriverJS (and thus, Protractor) APIs are entirely asynchronous. All functions return promises.

WebDriverJS maintains a queue of pending promises, called the control flow, to keep execution organized.

You can still though return the promise returned by click():

function enterSomeValuesIntoForm() {
    // do some stuff
    element(by.id('value1')).sendKeys('hello');
    element(by.id('value2')).sendKeys('is it me you looking for?');
    return element(by.id('submitButton')).click();
});

so that you can later explicitly resolve:

enterSomeValuesIntoForm().then(function () {
    // form is submitted at this point
});
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195