Getting Started with UI Automation Testing Using WebdriverIO 8 and Mocha

Piyathida San-aoun
4 min readApr 16, 2024

--

In the realm of automated testing, combining WebdriverIO (WDIO) with the Mocha test framework offers a robust solution for performing UI tests on web applications. WebdriverIO is well-regarded for its flexibility and compatibility with modern web technologies, while Mocha provides a straightforward and feature-rich environment for writing tests. This guide will walk you through setting up and implementing UI automation testing using WebdriverIO version 8 with Mocha, ensuring you have a solid foundation to build upon.

Step 1: Environment Setup

First things first, let’s prepare the environment necessary for running our UI tests.

1.1 Install Node.js

Ensure Node.js is installed on your system as WebdriverIO runs on Node.js. You can download it from the official Node.js website.

1.2 Initialize Your Project

Create a new directory for your test project and initialize a Node.js project:

mkdir my-wdio-tests
cd my-wdio-tests
npm init -y
npm install @wdio/cli --save-dev
npx wdio config

This will launch an interactive setup wizard. When prompted:

  • Select On my local machine for where you want to run your tests.
  • Choose mocha as the framework.
  • For the reporter, spec is a good default choice.
  • Optionally, select services such as chromedriver for running tests directly in the Chrome browser.

2.2 Review the Configuration

After the setup, wdio.conf.js will be created in your project directory. Open it to make any adjustments as necessary, ensuring it's configured for Mocha. The setup wizard typically handles this, but it's good to verify.

Step 3: Writing Tests with Mocha

Now, let’s move on to the exciting part — writing your tests.

3.1 Create a Test File

Organize your tests in a clear structure:

mkdir -p ./test/specs
touch ./test/specs/example.test.js

Getting Started with UI Automation Testing Using WebdriverIO 8 and Mocha

In the realm of automated testing, combining WebdriverIO (WDIO) with the Mocha test framework offers a robust solution for performing UI tests on web applications. WebdriverIO is well-regarded for its flexibility and compatibility with modern web technologies, while Mocha provides a straightforward and feature-rich environment for writing tests. This guide will walk you through setting up and implementing UI automation testing using WebdriverIO version 8 with Mocha, ensuring you have a solid foundation to build upon.

Step 1: Environment Setup

First things first, let’s prepare the environment necessary for running our UI tests.

1.1 Install Node.js

Ensure Node.js is installed on your system as WebdriverIO runs on Node.js. You can download it from the official Node.js website.

1.2 Initialize Your Project

Create a new directory for your test project and initialize a Node.js project:

mkdir my-wdio-tests
cd my-wdio-tests
npm init -y

1.3 Install WebdriverIO CLI

Install the WebdriverIO command-line interface which is used to manage your test suite:

npm install @wdio/cli --save-dev

Step 2: Configure WebdriverIO with Mocha

Configuration is crucial as it tailors the testing environment to your needs.

2.1 Generate Configuration File

Set up your WebdriverIO project by generating a configuration file:

npx wdio config

This will launch an interactive setup wizard. When prompted:

  • Select On my local machine for where you want to run your tests.
  • Choose mocha as the framework.
  • For the reporter, spec is a good default choice.
  • Optionally, select services such as chromedriver for running tests directly in the Chrome browser.

2.2 Review the Configuration

After the setup, wdio.conf.js will be created in your project directory. Open it to make any adjustments as necessary, ensuring it's configured for Mocha. The setup wizard typically handles this, but it's good to verify.

Step 3: Writing Tests with Mocha

Now, let’s move on to the exciting part — writing your tests.

3.1 Create a Test File

Organize your tests in a clear structure:

mkdir -p ./test/specs
touch ./test/specs/example.test.js

3.2 Sample Test Code

Open example.test.js and write a simple test. For instance, let's verify the title of a webpage:

describe('Home Page', () => {
it('should have the right title', async () => {
await browser.url('https://google.com');
const title = await browser.getUrl();
expect(title).to.include('Google');
});
});

Step 4: Running Your Tests

Execute your tests to see them in action:

npx wdio wdio.conf.js

This command will run the test suite as per the configurations set in wdio.conf.js. You will see output in the terminal indicating whether the tests passed or failed.

Step 5: Expanding Your Test Suite

As you get comfortable with the basic setup, consider enhancing your test suite:

  • Add More Complex Tests: Start adding more complex tests, including those that interact with various UI elements.
  • Page Object Model: Implement the Page Object Model (POM) to organize your test code better and make it reusable.
  • Continuous Integration: Configure your setup to run tests automatically using CI/CD tools like Jenkins or GitHub Actions.

Conclusion

Setting up UI automation tests with WebdriverIO and Mocha is a straightforward process that brings powerful testing capabilities to your development workflow. By following these steps, you’re well on your way to creating a comprehensive and maintainable test suite that can improve the quality and reliability of your web applications. As you progress, keep exploring additional features and integrations that WebdriverIO offers to maximize your testing effectiveness.

Happy testing!

--

--

Piyathida San-aoun
Piyathida San-aoun

Written by Piyathida San-aoun

Software Developer Engineer In Test

No responses yet