简体   繁体   中英

How to run mocha test modules one by one?

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

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(...)
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM