2
votes

I am new to protractor, because i dont have much knowledge on Javascript its becoming difficult to predict the script execution flow. I have very basic question on the behavior of the protractor promises and flow of data between js files.

I have one commonTC.js which has describe(),it() block. In this 'it' block i call functions at runtime. Means, i have functions such as open(),click(),enter() written in another js file which gets called dynamically at run time as per function call data is read from excel.

In protractor most browser interaction returns promise, thus when i do logic of browser calls in click()/enter() these return promise, i wanted to get the details of browser calls in commonTC.js which is the calling file.

So i made function call as promise() in commonTC.js

              dynamicActions[enter()].then(()=>{
                  console.log("Success Activity:")
              }).catch((err)=>{              
                  console.log("Error on Activity():")
                  throw err;
              });

In ActivityAction.js(), the function definition is as below

this.dynamicActions.enter = function () {
    var deferred = protractor.promise.defer();
    element(by.id("username")).sendKeys("abc").then(
            function success() {                        
                deferred.fulfill();
            },
            function error(reason) {
                deferred.reject(reason);
            });
    return deferred.promise;
    }

My question is- 1. Because of this architecture all the function calls are required to be returned as promise.If i do not return the promise, does the browser promise result will i be knowing in commonTC.js? 2. Also by this architecture, is all the function calls are synchronous. Because all the functions are made to return as promise, so will those promise execute serially? i mean after 1st promise resolve, is it guaranteed 2nd promise will be executed/ or it might pick 5th also? 3. if it picks promise execution randomly then how can i achieve serial execution of promises? 4. If am required to remove promise architecture in commonTC.js then how to make sure each function open()/enter() returns failure/success?

1

1 Answers

0
votes

Protractor has a build-in promise manager named Control Flow which keep the promises in a list and guarantee promises in the list executed serially as the order as they appeared in your script.

it('promise execute order', ()=>{
   browser.get('...')               // promise 1
   element(by.xxx(...)).sendKeys(); // promise 2
   element(by.xxx(...)).sendKeys()
                       .then(()=>{ yyyy.then()....}) // nested `then()`
                       .then()
                       .catch()  // promise 3, no matter how many `then()` followed 
                                 // and nested. They are treated as single promise

   element(by.xxx(...)).click();    // promise 4
})

// There are 4 lines in above `it`, every protractor api return a promise
// thus there will be a list includes 4 promises:
// [promise 1, promise 2, promise 3, promise 4]
// Control Flow makes sure promise in list executed one by one.