2

In my Protractor Test suite I have some problem with parametrizing number of tests .

  it('Login and Count', function () {
    PageObject.login();
    var number = element.all(by.something()).count().then(function(n){
      //here i have my number which i want to use to run next 'it' n times
     });
  });

  it('Run number '+ m,function () {
    PageObject.Run(m);
  })

My problem is that I am able to reach my count result only inside then block when promise is resolved. How to use that to execute second it as many times as i want ? I am not able to write this result to global variable.

Sorry I am JS newbie and async promises are still a hell for me

1
  • I think there is much better way...what are you trying to achieve in application by doing this? Mar 27, 2019 at 23:30

1 Answer 1

1

You can use global variables as described in this question on SE.

Wonder if you really want to store a value in a global variable. Each it() in Protractor is a test. Tests should be run in isolation from other tests. If tests are not isolated and depended on each other it could become harder and time consuming in the future to maintain them. Since one failing tests could block all other tests. Also you cannot expect all the it() functions to run in the order of the file, certainly when running tests in parallel this could become an issue.

Have a look at the AAA test pattern, your test would look something like this:

it('my test', function() {
  //ARRANGE
  PageObject.login();
  var number;
  element.all(by.something()).count().then(function(n){
    number = n;
  });

  //ACT
  //Loop and execute Run() number of times the element exists.
  for (step = 0; step < number; step++) {
    PageObject.Run(step);
  }

  //ASSERT
  expect(something).toEqual(something_else);

});
4
  • My problem is that I have to run my 'it' multiple times with different suffix, couse I save my test results to the database and I should be able to read from results from each run :/ it('LOGIN', function () { PageObject.login() }); function openelement(i) { it('Open element Nr' + i, function () { PageObject.opensingleelementnr(i); }); } for (var i = 0; i < n; i++) { opensingleelement(i); } This is the only way that it works, but I have to hardcode my n number . This should be result of PageObject.listElem.count() . Jul 1, 2016 at 13:56
  • Then you have to set a global variable and loop tests, have a look at: stackoverflow.com/questions/21634558/… Jul 1, 2016 at 14:00
  • But how can save sth to global variable if i get it after count() promise is resolved ? It just doesn't work Jul 1, 2016 at 14:25
  • Hmm according to this: github.com/angular/protractor/issues/997 Each it() runs on their own, meaning with protractor it() functions cannot communicate with each other, the global example is only for a setting, not to store data between tests. You will need something to store the value in callback function of the promise. Maybe to file: docs.nodejitsu.com/articles/file-system/… In the Run() test load the data from file again: docs.nodejitsu.com/articles/file-system/… and use the number you stored. Jul 1, 2016 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.