mardi 17 novembre 2015

Reusable functions for Protractor test framework

I use Protractor Test Framework and need to use some the same operations for different cases. Like Authentication procedure.

The question is: What's the correct way to use own functions in Protractor?

I remember this tool works: 1) asynchronous and 2) its functions return promises.

Must the reusable function return the promise for make possibility do .then() operation or this function may return no value?

An Example (correct or no):

describe('Login procedure', function() {
  it('Login Username', function () {
    browser.get('anurl.com');
    auth('username', 'password').then(function(){console.log('NICE TO MEET YOU')});
});


var auth = function(loginstr, passwordstr) {
  return element(by.css('div[class="login"]')).isDisplayed().then(function (result) {
    if (result) {
      element(by.css('input[name="email"]')).clear().sendKeys(loginstr).then(
        function () {
          element(by.css('input[name="password"]')).clear().sendKeys(passwordstr).then(function () {
            element(by.css('button[class="submit"]')).click().then(function () {
              return element(by.id('welcome')).isPresent();
            });
          });
        });
    }
  });
}

Thanks!

Aucun commentaire:

Enregistrer un commentaire