0

I am implementing a mocha test script to login and logout a specific webpage and my purpose is to make this test script modular.

Actually my main test script like the following;

describe('Test is being started for user : ' + 
    currentUserInfo.email , function () {
    it('Login Test', async function () {
        await loginTest(page, currentUserInfo.email, 
    currentUserInfo.password);  
    });

    it('Logout Test', async function () {
        await logoutTest(page);
    });
});

And the logintest.js like the following;

module.exports = function(page, userName, userPass){

    before (async function () {
    });

    after (async function () {
        console.log("Login test is finished");
    })

    describe('Login Test is being started for user : ' + 
userName , function () {
        it('Enter Email', async function () {
            await page.focus('#username_box')
            await page.keyboard.type(userName)
        });

        it('Enter Password', async function () {
            await page.focus('#password_box')
            await page.keyboard.type(userPass)
        });

        it('Click "Login in Here" button', async 
function () {
            await page.click('input[value="Log in 
Here"]'); // With type
            await page.waitForNavigation();     
        });

};

In the main test runtime the logoutTest function doesn't wait to finish the loginTest. Also, I have tried to use Promise object but in this case, my scripts don't run that under the LoginTest.

     module.exports = async function(page, userName, userPass){
  return new Promise(resolve => {
    before (async function () {
    });

    after (async function () {
        console.log("Login test is finished");
        resolve(10);
    })

    describe('Login Test is being started for user : ' + 
userName , function () {
        it('Enter Email', async function () {
            await page.focus('#username_box')
            await page.keyboard.type(userName)
        });

        it('Enter Password', async function () {
            await page.focus('#password_box')
            await page.keyboard.type(userPass)
        });

        it('Click "Login in Here" button', async function () {
            await page.click('input[value="Log in Here"]'); // With type
            await page.waitForNavigation();     
        });
    });
  });
};

thanks

SayMyName
  • 461
  • 5
  • 17

1 Answers1

0

Mocha does run sequentially.

your logintest.js is exporting a non-async function. So, the await on your main test is not blocking as expected and the logout test will start before the logintest.js finishes.

Besides, I would recommend you to nest the logintest.js and loginouttest.js in the main.js inside a describe block.

describe('main', function() {
  describe('login', function() {
    before(...)
    after(...)
    it(...)
    it(...)
  }
  describe('logout', function() {
    before(...)
    after(...)
    it(...)
    it(...)
  }
}
chakwok
  • 980
  • 8
  • 21
  • But when other test cases are added in the main test, isn't it decrease the readability of the code? – SayMyName Jun 26 '19 at 06:32
  • It depends on the number of your test cases. If you have a large number of test cases and want to separate them in different .js, you should still follow the nested describe pattern for better grouping. For the actual coding style, you can refer to [here](https://stackoverflow.com/questions/24153261/joining-tests-from-multiple-files-with-mocha-js). – chakwok Jun 26 '19 at 06:46