Skip to content

Latest commit

 

History

History
123 lines (77 loc) · 6.14 KB

chapter_03.md

File metadata and controls

123 lines (77 loc) · 6.14 KB

WDIO Testrunner

If you have finished the last chapter successfully, good job 👍! If you just started with this chapter then no worries.

We are going to set up everything to automate Chrome on our machine with the WDIO testrunner (and write our first automated script). The testrunner helps you with various things that you need in your day to day automation life. It provides you with useful reporters, services and other neat features that will make your life easier.

In this chapter, we will port the Node.js automation script into a test suite that is using the testrunner. If you haven't written the script, then the objective for the script is as follows: write a Node.js script that does the following things:

  1. Open the Chrome browser
  2. Go to the following page: https://vue-todomvc.webdriver.io/
  3. Enter 3 items into the ToDo list
  4. Mark the second item as completed
  5. Print out the amount of items left

The objectives for this chapter are as follows:

  1. Initiate a wdio.conf.ts file using the WDIO setup wizard
  2. Create a test directory where we put all our e2e test files
  3. Port the code in your test.ts file into an actual test
  4. Add an assertion library to make an assertion in the test
  5. Create a simple entry in package.json to run test through NPM script

The WDIO testrunner allows you to scale up your automation tests. It takes on a lot of work that you would otherwise need to setup up manually. The features and advantages of it are the following:

  • runs test in a parallel automatically
  • creates and tears down sessions for you
  • comes with a lot of official and 3rd party plugins
  • handles log management
  • and much more

To get up and running with the testrunner, execute the following in your terminal:

$ npm init wdio@latest .

This will install the CLI interface that you can use to run your tests with. Instead of running the node command directly on a file, you are now using the newly installed CLI tool wdio to run your tests. By calling this command, a configuration wizard is automatically started to walk you through the setup.

You will be asked a few questions that you can answer as follows:

A project named "..." was detected at "...", correct?

It will show the current project and project directory, therefore you can confirm as we do not need to change it for this workshop.

What type of testing would you like to do?

WebdriverIO provides a variety of use cases listed in the selection. For this workshop we will start with the most common option which is E2E Testing - of Web or Mobile Applications but feel free to explore the other options after the workshop.

Where is your automation backend located?

As we are getting started we want to run the test, which will be running on your local machine. We will integrate cloud vendors in a later chapter.

Which environment you would like to automate?

Running end-to-end tests requires different setups depending on whether you run a browser or mobile test. Here WebdriverIO needs to know your desired environment to properly configure the test. For this workshop we use the Web - web applications in the browser environment.

With which browser should we start?

Let's keep it simple and only select Chrome. Later on we can add more browsers to our matrix.

Which framework do you want to use?

Mocha or Jasmine are the framework of choice for this workshop.

Are you using a compiler?

We recommend you to use TypeScript as it offers a lot of great features like type safety. For this workshop we will use TypeScript but you can also select "No!" and run tests in JavaScript.

Do you want WebdriverIO to autogenerate some test files?

Since we've already written our automation script, we can press n here. We'll reserve the task of crafting our own page objects for a subsequent chapter.

Note

After creating the test file, store it in your chosen test folder. Then, you need to adjust the specs array in the wdio.conf.ts file to point to your test files. For example, if you've stored your test files in a folder named tests, update the specs array like this: specs: ['./tests/**/*.ts']. This pattern **/*.ts will include all TypeScript files in the tests folder and its sub-folders. Remember to replace ./tests with the actual path to your test folder. Here are a couple of examples for different structures:

  • For a flat structure: specs: ['./tests/*.ts']
  • For nested folders: specs: ['./tests/**/*.ts']

Which reporter do you want to use?

The spec reporter is the reporter which we will be using for this workshop and we'll add another one in the next chapter.

Do you want to add a plugin to your test setup?

No plugins needed for this workshop, continue without selection.

Would you like to include Visual Testing to your setup?

We will visit Visual Testing later on in Chapter 10, for now we don't want to add this to our setup.

Do you want to add a service to your test setup?

Hit enter without any selection as we want to add service integrations later on.

Do you want me to run npm install

Select Y and the project will be created and dependencies will be installed.

After all these questions your config file is created. Next try to solve the objectives mention at the top of this file. If you port the test.js code to an actual test, make sure you follow the Mocha conventions of writing test files. Simple setup would like this:

describe("My Vue.js Example Application", () => {
  it("should be able to complete ToDos", async () => {
    // add your automation code here
    // ...
  });
});

To automate the browser through the browser object you can either import it via:

import { browser } from "@wdio/globals";

After you've ported/written the tests you can run it using the WDIO testrunner by calling:

$ npx wdio run ./wdio.conf.ts

Note

If you have Chrome installed on your machine then WebdriverIO will automatically download and install a matching ChromeDriver version. This will only happen the first time you run your script.