userMod2
userMod2

Reputation: 8960

Mocha + WebDriverIO - Common Functions

I have a test which contains steps that I'll want to reuse in multiple files.

I'm thinking I could create a file called common.js, list all the functions in there and just call as and when I need.

Is this a recommended approach?

The only issue I feel is having a super long file of common methods and if i seperate then I'd need to use lots of require statements.

Upvotes: 0

Views: 545

Answers (2)

Kevin Lamping
Kevin Lamping

Reputation: 2269

One option is to set up Page Objects, as defined in the official docs. I also have a video covering subject on YouTube.

If you don't want to do page objects, you can add custom commands to WebdriverIO using the 'addCommand' command.

Upvotes: 0

T Gurung
T Gurung

Reputation: 363

The simplest would be to do what you have hinted to make a commonSpec.js file and use it anywhere by importTest() which would be something like this :

commonSpec.js

describe('Common Steps that will be used by all', () => {
    it('Can log in', () => {
       //log in code
    });

    it('land on a particular page', () => {
        // assertion code for the particular page
    });
});

commonSpecUsed.js

describe("Common Specs", () => {
    importTest("common specs", './commonSpec.js');
});

The above approach is best and simple when they share the same specs and assert the same values. But when the assertions are different for e.g

  • A normal user will land on a simple user page
  • Registered user will land on their "my account" page
  • Admin will land on their dashboard page ..etc..etc..

Then you might want to make your commonSpec.js file more dynamic by enabling it to accept parameters. This would be entirely based on your test requirements. Can be more helpful if you could please share some code snippet.

Hope this helps.

Upvotes: 1

Related Questions