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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With