0

I'm currently trying to get WebDriverIO working with Appium and Mocha (BDD) towards an Android device. Previously when I've worked with Mocha the behavior has been that testcases are isolated from eachother and should be built to be independent from eachother. When doing this in WDIO however I can not seem to figure out how to reset state between individual tests.

See following example:

// Capabilities will have started app at this point and this is not real code
describe('Auth', async () => {
    it('Login', async () => {
        const username = await $('#username');
        const password = await $('#password');
        const loginButton = await $('#loginButton');

        await username.addValue('user');
        await password.addValue('pass');
        await loginButton.click();
    });

    // I want the app to reset here.
    // Otherwise I will have to create this test
    // based on the result of the previous test
    // which will cascade across every single test
    // in this describe-block and offer no flexibility in
    // adding more tests here.
    it('Logout', async () => {
        // Reading the code inside here provides no clarity in
        // how it works since the majority of this test is done
        // in the login test since it doesn't reset
        const logoutButton = await $('#logoutButton');
        await logoutButton.click();
    });

    // I want the app to reset here again
    // Otherwise this test NEEDS to be below
    // the logout test or above the login test
    // OR I can create this test to logout from
    // the state where previous test left app
    // but it will be really hard to understand
    // what this code does without following every
    // single test before it.
    it('Forgotten Password', async () => {
        // Doing things
    });
}); 

What is causing my issue here? Am I fundamentally misunderstanding how to structure these tests and make it work properly in practice? Do I have to structure the individual tests in a describe-block in such a way that they "follow the flow"? I have several things I would want to do in this describe block that are related to auth but follow a different approach, and creating them here would just be taking a really weird path around the app to press things.

Please let me know if you want me to clarify my thoughts!

1 Answer 1

0

Based on what code you've provided, I'd say how you're setting it up is incorrect.

it are used for the test itself, where you perform actions and make assertions. While you have actions, like click() or addValue(), there are no assertions. Are you wanting to test (assert) something for the login/logout behavior? Or is that just code to have for setup?

Mocha, like other test libraries, have hooks you should use. before(), after(), beforeEach(), afterEach().

before(), after() are used before all and after all. So, these are ran once for setup and clean-up methods. If you want to login/out once, you can add your login code to before() and your logout to after().

beforeEach(), afterEach() are ran before and after every test (it) method. So, if you want to login/out for each test, you can add your login code to beforeEach() and your logout to afterEach()

More info can be found in the docs: https://mochajs.org/#hooks

1
  • This does not really approach my question. This is not real code for real tests, doing the login in real life would have assertions but that does still not change the flow of the running suite. And using the hooks would be a valid way to construct/destruct but what would be the proper way of doing that in those hooks? May 3, 2023 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.