Creating a before hook in webdriverio

Viewed 12

I'm (very) new to webdriverio, and I'm struggling with creating a before hook.

My setup includes the latest webdriverio using Mocha as my testing fromework.

So, in essence, what I'd like to do is create a before hook for the following code (which would be used for each test case, hence why I thought a before hook would be suitable);

it('close cmp', async () => {
        await browser.pause(5000);
        const cmpDismissal = await $('~ACCEPT AND CLOSE');
        if (await cmpDismissal.isExisting()) {
            await cmpDismissal.click();
            await cmpDismissal.waitForExist({ reverse: true });
        }
    });
    it('allow notifications', async() => {
        const notificationsDismissal = await $('~Allow');
        if (await notificationsDismissal.isExisting()) {
            await notificationsDismissal.click();
            await notificationsDismissal.waitForExist({ reverse: true });
        }
    });
    it('click on skip button', async() => {
        const skipToContent = await $('~SKIP');
        await skipToContent.waitForExist({timeout: 50000});
        await skipToContent.isExisting();
        await skipToContent.click();
    });

    it('allow', async() => {
        const useData = await $('~Allow');
        if (await useData.isExisting()) {
            await useData.click();
            await useData.waitForExist({ reverse: true });
        }
    });

So in my wdio.conf.js config file I've added;

before: function () {
        const cmpDismissal = await ('~ACCEPT AND CLOSE');
        if (await cmpDismissal.isExisting()) {
            await cmpDismissal.click();
            await cmpDismissal.waitForExist({ reverse: true });
        }
        //
        const notificationsDismissal = await $('~Allow');
        if (await notificationsDismissal.isExisting()) {
            await notificationsDismissal.click();
            await notificationsDismissal.waitForExist({ reverse: true });
        }
        //
        const skipToContent = await $('~SKIP');
        await skipToContent.waitForExist({timeout: 50000});
        await skipToContent.isExisting();
        await skipToContent.click();
        //
        const useData = await $('~Allow');
        if (await useData.isExisting()) {
            await useData.click();
            await useData.waitForExist({ reverse: true });
        }
    }

But this doesn't work.

I'm presuming it's possible to create a before hook for this kind of functionality?

Or is there a better way of doing this?

Any help would be appreciated.

0 Answers
Related