Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create conditional test cases using Protractor?

Currently, I have some test cases that I only want to execute in certain conditions.

it ('user is able to log in', function() {
   if(siteAllowsLogin) {
       .....
   }

Using the above syntax results in sitesNotAllowingLogin to PASS this test. I know there is a solution to mark the test as PENDING, but I would rather the test did not show, if it was not applicable.

I also want to keep the logic inside of the test case, if possible. So keeping the if block inside the test case.

Any suggestions on how to skip this test if the condition is not met so that it does not display on the results as PENDING or PASSED.

Help would be greatly appreciated.

like image 241
fuzzi Avatar asked Apr 18 '16 18:04

fuzzi


1 Answers

You could use an ignore function in front to suppress the test if the provided predicate/state is true:

var ignore = function(exp){return{it:((typeof exp==='function')?exp():exp)?function(){}:it}};

describe('Suite 1', function() {

    it("test a", function() {
        expect(1).toEqual(1);
    });

    ignore(true).it("test b", function() {
        expect(1).toEqual(1);
    });

    ignore(skip).it("test c", function() {
        expect(1).toEqual(1);
    });

    function skip(){
      return true;
    }
});
like image 145
Florent B. Avatar answered Oct 16 '22 18:10

Florent B.