0

I have the following code:

.then(function() {
    return buttonClick();
})
// BEGIN
.then(function() {
    return browser.wait(EC.elementToBeClickable(anotherButton), timeOut)
        .then(function() {
            anotherButton.click();
        })
})
.then(function() {
    return browser.wait(EC.elementToBeClickable(yetAnotherButton), timeOut)
        .then(function() {
            yetAnotherButton.click();
        })
})
// END
.then(function() {
    browser.wait(do some things);
})

I need to make the code in between BEGIN and END execute only when return browser.wait(EC.elementToBeClickable(anotherButton), timeOut) succeeds. If the above does not succeed, I would just like to resume the chain at the browser.wait(do some things);

I've been trying several different ways of achieving this and have found some luck in doing something similar to this but have yet to actually get the results I'm looking for.

Any suggestions on how I can accomplish this?

Thanks

User 5842
  • 2,849
  • 7
  • 33
  • 51

1 Answers1

0

EDIT: According to Protractor FAQ you can run a second function in an error case.

.then(function() {
    return buttonClick();
})
//wrap your BEGIN-END-part into a new function like this:
// then(function(){BEGIN-END}, function(err){failcase-execution})
.then(function(){
    // BEGIN
    .then(function() {
        return browser.wait(EC.elementToBeClickable(anotherButton), timeOut)
            .then(function() {
                anotherButton.click();
            })
    })
    .then(function() {
        return browser.wait(EC.elementToBeClickable(yetAnotherButton), timeout)
            .then(function() {
                yetAnotherButton.click();
            })
    })
}, function(err){
    //Here is your execution in error-case.
    browser.wait(do some things);
})
.then(function() {
    browser.wait(do some things);
})

But to me it sounds, like you're trying to test two different scopes. Is there a reason, these two tests have to be executed within the same test case (it-block)?

Something like:

describe ("test", function(){
    it("executes the first part incl. Begin-End", function(){
        //eventually some steps to come here first.
        .then(function() {
            return buttonClick();
        })
        // BEGIN
        .then(function() {
            return browser.wait(EC.elementToBeClickable(anotherButton), timeOut)
                .then(function() {
                    anotherButton.click();
                })
        })
        .then(function() {
            return browser.wait(EC.elementToBeClickable(yetAnotherButton), timeOut)
                .then(function() {
                    yetAnotherButton.click();
                })
        })
    });
    it("executes the second part, which resumes anyway", function(){
        //if necessary, repeat some of the steps from before test
        .then(function() {
            return buttonClick();
        })
        .then(function() {
            browser.wait(do some things);
        })
    });
});

Or if you try to just figure out, if you could click the button, you could execute the clicks between BEGIN/END in a separate function, which just returns true or false (similar to what you linked to):

//existing part
.then(function() {
    return buttonClick();
})
.then(
    bool clicksWorked = this.optionalExecution();
)
.then(function() {
    browser.wait(do some things);
    if(!clicksWorked){console.log("Some Buttons couldn't be clicked")} 
})

//as separate function, which just returns either true or false
this.optionalExecution = function() {
    try {
        // BEGIN
        browser.wait(EC.elementToBeClickable(anotherButton), timeOut)
            .then(function() {
                anotherButton.click();
            })
        browser.wait(EC.elementToBeClickable(yetAnotherButton), timeOut)
            .then(function() {
                yetAnotherButton.click();
            })
        return true
    } 
    catch (Exception e) {
       return false
    }
}

As a side remark: Can you add a bit more context to your .then()-list as it's not clear to me, if your .then()'s all refer to an entering condition or how they're embedded in the test case. In your current example it seems not necessary to put your actions within .then()-blocks. Protractor would execute line by line synchronously, also without the .then()'s. See also here

Ernst Zwingli
  • 1,402
  • 1
  • 8
  • 24
  • Just re-read the Protractor FAQ and figured a nicer way. Added it via Edit to my answer: https://github.com/angular/protractor/blob/master/docs/faq.md#how-can-i-catch-errors-such-as-elementnotfound – Ernst Zwingli Oct 03 '17 at 07:40