Environment: Protractor, jasmine, typescript
I have several tests that have different setup and the same assertion.
Is it a good practice to enter different steps in each it inside a describe and the expect assertion in the afterEach block?
Example:
Form contains phoneNumberField and a submitBtn;
Test 1: Enter too short number into phoneNumberField => Expect submitBtn to be disabled.
Test 2: Enter text into phoneNumberField => Expect submitBtn to be disabled
Example code:
describe('Invalid phone number => submitBtn disabled', () => {
afterEach(() => {
expect(submitBtn.isEnabled()).toBeFalsy();
});
it('Too short number input', () => {
phoneNumberField.sendKeys('012');
});
it('Text input', () => {
phoneNumberField.sendKeys('asdf');
});
});
Honestly I'm surprised it works this way... I was going to say "no, it's not a good practice..." but I didn't find any disadvantages of doing it this way after playing around for a few minutes with it.
But still I wouldn't do it, because if you place
expect()init()blocks, it's easy to see the sequence of the logic in your tests (e.g. open page->expect smtng; click button->expect smtng; etc)But you can just implement it, use it for a bit and see if it works out well for yourself
P.S. just to visualize why I think it's not a terrible idea as it may seem to be: having below code
will produce following output
As it can be seen report is being printed normally where every failure is shown as individual a failure of
it()test case