user2233949
user2233949

Reputation: 2233

How to exit a testcase in protractor if element contains value

I've been writing a suite of tests in Protractor, and am almost finished. I'm having difficulty figuring out how to do something fairly common though. I want to be able to check if a textbox contains a value, and if so, exit the testcase with a failure. (If the textbox contains a value, I know there's no chance the test case can pass anyway)

I'm currently trying something like this:

    tasksPage.generalInformationDateOfBirthTextbox.getAttribute('value').then(function(attr){
        //attr contains the correct value in the textbox here but how do I return it to parent scope so I can exit the test case?
        console.log("attr length is " + attr.length);
        expect(attr.length).toBe(0);
    },function(err){
        console.log("An error was caught while evaluating Date of Birth text value: " + err);
    });

The expect statement fails as I'd expect it to, but the testcase keeps going, which seems to be the intended behavior of expect. So I tried returning a true/false from within the 'then' block, but I can't seem to figure out how to return that value to the parent scope to make a determination on. In other words, if I change the above to:

    var trueFalse = tasksPage.generalInformationDateOfBirthTextbox.getAttribute('value').then(function(attr){
        if(attr === ""){
            return true;
        }else{
            return false;
        }
    },function(err){
        console.log("An error was caught while evaluating Date of Birth text value: " + err);
    });

    //This is the 'it' block's scope
    if(trueFalse == true){
        return;
    }

I know my inexperience with promises is probably to blame for this trouble. Basically, I just want a way of saying, 'if such and such textbox contains text, stop the test case with a failure'.

Thanks,

Upvotes: 1

Views: 1531

Answers (2)

Sakshi Singla
Sakshi Singla

Reputation: 2502

The issue with the code above is that you are trying to compare a 'Promise' and a 'value'.

trueFalse.then(function(value) { if(value) .... });

OR compare it via an expect statement. Something like expect(trueFalse).toBe(false);

Upvotes: 0

alecxe
alecxe

Reputation: 473833

This is not a protractor issue, it is a testing framework issue (jasmine, mocha etc).

Though, there was an issue on protractor's issue tracker:

which was closed with a reference to an opened issue in jasmine issue tracker:

While this is not implemented, there is a workaround (originally mentioned here):

After installing the module with npm, when you want your test to fail and exit, add:

jasmine.getEnv().bailFast();

(don't forget to load the dependency require('jasmine-bail-fast');)


Also see:

Upvotes: 1

Related Questions