Search code examples
javascriptprotractorcucumberjs

Exit from function after resolving protractor promise


I am fairly new to JavaScript and Protractor. I have simple task in my test that I am unable to complete.

  1. Check on available Tab on web page.
  2. Check if element is visible on web page.
  3. a) If Yes, return deffered.fullfil(true)
    b) If No,
          - Click on In-Progress Tab
          - Click on Available Tab.
          - Go to Step 1.

I am trying to do this recursively and below is my code. It is printing Element found but never exits the function after that and times out.

var check_availability = function(counter, totalCount, element){
var deferred = potractor.promise.defer()
if(counter <= totalCount){
    browser.wait(function(){
        browser.wait(EC.visibilityOf(element),2000)
        return element
    }).then(function(success){
        console.log('Element found.')
        return deferred.fulfill(true)
    }, function(err){
       inprogressTab.click()
       .then(() => availableTab .click())
       .then(() => check_availability (counter+1 , totalCount, element))
    })
   } else{
   return deferred.reject(false)
  }
  return deferred.promise
}

PS: This is a sample code that I am using, corrected some spelling mistakes and syntax.


Solution

  • I see several syntax errors on your shared code. Below i tried to fix those errors and provided the expected behavior but still don't know from where the inprogressTab is coming from.

    const check_availibility = function(counter, totalCount, element) {
      const deferred = protractor.promise.defer();
        if (counter <= totalCount) {
           browser
            .wait(() => browser.wait(EC.visibilityOf(element), 2000))
       .then(
        element => {
          console.log("Element found.");
          return deferred.fulfill(true);
        },
        err => {
          inprogressTab
            .click()
            .then(() => availableTab.click())
            .then(() => check_availibility(counter + 1, totalCount, element));
        }
      );
    } else {
       return deferred.reject(false);
    }
      return deferred.promise;
    };