Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

End to end test in angular 2 using protractor

I am working on angular 2 e2e test. I need to add it block conditionally. Have to check a count value If its value is greater than 0 then should run a test otherwise it should run the other. Here is the code:

describe('Check insiders:', function () {
  beforeEach(function (done) {
    element(by.css('span.insider-count')).getText().then(function(total){
      count = total;
      console.log(count);
      //browser.pause();
      done();
    });
  });
  if (count > 0) {
    it('insiders found', () => {
      expect(count).toBeGreaterThan(0);
    });
  } else {
    it('No insiders found', () => {
      // do nothing
      //expect(count).toBe(0);
    });
  }
});

It always runs the else part even if the value of count variable is greater than zero. Any help will be much appreciated.

like image 636
Dev001 Avatar asked Jul 30 '26 21:07

Dev001


1 Answers

That's because it, describe and all jasmine describers are processed when you start the tests, this means that your condition will be tested beforethe test has fetched the value, that's why it's always the same value processed, hence the same result.

You can avoid it by moving your condition inside a common it block:

    it('insiders test', () => {
      if(count > 0){
          expect(count).toBeGreaterThan(0);
      } else {
          expect(true).toBe(true);
      }
    });

That being said, I don't understand your logic, as you're basically doing "if count is > 0, assert that it is > 0, else don't" which is a test that can't fail.

like image 109
Supamiu Avatar answered Aug 02 '26 15:08

Supamiu