protractorautomated-testswebautomationprotractor-net

Is it a good practice to place an assertion in the afterEach block?


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');
    });
});

Solution

  • 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() in it() 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

    beforeAll(async () => {expect(true).toBe(false)});
    
    it("1", async () => {
    });
    
    it("2", async () => {
    });
    
    it("3", async () => {
    });
    

    will produce following output

    Suite: UCare - Provider Search - 'Places' tab
        ✗ 1 (0.002 sec)
          - Expected true to be false.
              at UserContext.beforeAll (/Users/spleshakov/Documents/ui-automation/protractor/custom_implementation/test.spec.js:11:38)
              at /Users/spleshakov/Documents/ui-automation/node_modules/jasminewd2/index.js:112:25
              at new Promise (<anonymous>)
              at SimpleScheduler.promise (/Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:2242:12)
              at schedulerExecute (/Users/spleshakov/Documents/ui-automation/node_modules/jasminewd2/index.js:95:18)
              at promise (/Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:2232:22)
              at new Promise (<anonymous>)
    
        ✗ 2 (1 sec)
          - Expected true to be false.
              at UserContext.beforeAll (/Users/spleshakov/Documents/ui-automation/protractor/custom_implementation/test.spec.js:11:38)
              at /Users/spleshakov/Documents/ui-automation/node_modules/jasminewd2/index.js:112:25
              at new Promise (<anonymous>)
              at SimpleScheduler.promise (/Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:2242:12)
              at schedulerExecute (/Users/spleshakov/Documents/ui-automation/node_modules/jasminewd2/index.js:95:18)
              at promise (/Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:2232:22)
              at new Promise (<anonymous>)
    
        ✗ 3 (0.907 sec)
          - Expected true to be false.
              at UserContext.beforeAll (/Users/spleshakov/Documents/ui-automation/protractor/custom_implementation/test.spec.js:11:38)
              at /Users/spleshakov/Documents/ui-automation/node_modules/jasminewd2/index.js:112:25
              at new Promise (<anonymous>)
              at SimpleScheduler.promise (/Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:2242:12)
              at schedulerExecute (/Users/spleshakov/Documents/ui-automation/node_modules/jasminewd2/index.js:95:18)
              at promise (/Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:2232:22)
              at new Promise (<anonymous>)
    

    As it can be seen report is being printed normally where every failure is shown as individual a failure of it() test case