-1

I'm working on some Jasmine end-to-end testing, using Protractor test runner. The application I am testing is a simple webpage. I already have a test scenario that works fine.

Now I'd like to improve my code so that I can use the same script to run the testing scenario twice.

  • The first time: the test would be performed on the English version of the page
  • The second time: on a translated version of the same page.

Here is my code:

var RandomSentenceInThePage = ["Sentence in English", "Phrase en Francais"];
var i;
var signInButton;
var TranslationButton;
var RandomSentenceInThePageBis;
i = 0;
//Runs the testing scenario twice
while (i < 2) {
  describe('TC1 - The registration Page', function() {
    //the translation is done on the second iteration
    if (i != 0) {
      beforeEach(function() {
        browser.ignoreSynchronization = true;
        browser.get('https://Mywebsite.url.us/');
        //we get the translation button then click on it
        TranslationButton = element(by.css('.TranslationButtonClass'));
        TranslationButton.click();
      });
    }
    //On the first iteration, we run the test on the not translated page…
    Else {
      beforeEach(function() {
        browser.ignoreSynchronization = true; //Necessary for the browser.get() method to work inside the it statements.
        browser.get('https://Mywebsite.url.us/');
      });
    }

    it('should display the log in page', function() {
      //Accessing the browser is done in the before each section
      signInButton = element(by.css('.SignInButtonClass'));
      signInButton.click();

      RandomSentenceInThePageBis = element(by.css('.mt-4.text-center.signin-header')).getText();
      /*******************[HERE IS WHERE THE PROBLEM IS]*******************/
      expect(RandomSentenceInThePageBis.getText()).toEqual(RandomSentenceInThePage[i]);
    });
    /*******************************************************************/
  });
}

I have highlighted the problematic section. The code keeps running even before the comparison between RandomSentenceInThePage[i] and RandomSentenceInThePageBis are compared. And when they are finally compared, the loop is already done.

According to what I have seen on the other related topics, because of the use of expect statements and getText() methods, I am dealing with promises and I have to wait for them to be resolved. After trying for the whole day, I think I could use a hint on how to deal with this promise resolution. Let me know if you need more information.

karel
  • 5,489
  • 46
  • 45
  • 50
BumpyRide
  • 59
  • 6
  • Possible duplicate of [JavaScript closure inside loops – simple practical example](https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – CertainPerformance Mar 20 '19 at 04:47

1 Answers1

0

Change while loop to for loop and declare the variable: i by let, rather than var

let can declare variable at code block scope like for, if block etc. But var can't.

Because protractor api execute async, thus when the expect()... execute for the second time. the value of i has become 2, not 1

for(let i=0;i<2;i++) {

  describe('TC1 - The registration Page', function() {
     ....
  })
}
yong
  • 13,357
  • 1
  • 16
  • 27