Running Tests from command-line parameters



We can control running tests using the command-line parameters. Let us take a scenario, where we have four test files within the spec folder in the WebdriverIO project.

The following screen will appear on your computer −

Command-Line Parameters

Suppose we want to trigger only the files testcase1.js and testcase2.js using the command-line parameters. To do this we have to add a parameter called suites in the Configuration file wdio.conf.js file.

The details on how to create a Configuration file are discussed in detail in the Chapter titled Wdio.conf.js file and Chapter titled Configuration File generation.

Let us consider that the files testcase1.js and testcase2.js belong to a suite called the group1 and the files testcase3.js and testcase4.js belong to a suite called the group2. We need to add this information to the wdio.conf.js file under the suite parameter as given below.

suites: {
   group1: ['test/specs/testcase1.js', 'test/specs/testcase2.js'],
   group2: ['test/specs/testcase3.js', 'test/specs/testcase4.js']
},

The following screen will appear on your computer −

Test specs

To trigger the test files testcase1.js and testcase2.js belonging to group1, we have to run the command given below −

npx wdio run wdio.conf.js --suite group1

The following screen will appear on your computer −

Testcase2.js

After the command has been executed successfully, we see only the two test files testcase1.js and testcase2.js under the specs folder have been triggered for execution.

Suppose we want to trigger only the file testcase3.js using the command-line parameters. To trigger only the test file testcase3.js, we have to run the following command −

npx wdio run wdio.conf.js --spec test/specs/testcase3.js

The following screen will appear on your computer −

Testcase3.js

After the command has been executed successfully, we see only the test file testcase3.js under the specs folder has been triggered for execution.

Besides, if we want to trigger multiple test files testcase3.js and testcase4.js, the command should be as follows −

npx wdio run wdio.conf.js --spec test/specs/testcase3.js, test/specs/testcase4.js

Suppose we want to exclude only the file testcase4.js from execution. To do this we have to add a relative path of the file that we want to exclude under the exclude parameter in the Configuration file wdio.conf.js file as given below.

exclude: [
   // 'path/to/excluded/files'
   'test/specs/testcase4.js'
],

The following screen will appear on your computer −

Testcase4.js

Then, we have to run the below command −

npx wdio run wdio.conf.js 

The following screen will appear on your computer −

Executed Successfully Screen

After the command has been executed successfully, we see the test file testcase4.js under the specs folder has been excluded from execution.

Advertisements