Set up and Develop Selenium WebDriver Mocha Test Scripts with TestWise IDE

A step by step guide (with video) to create a test project for a Selenium WebDriver + Mocha test script in TestWise IDE.

Courtney Zhan
5 min readOct 24, 2023

--

The video below uses an Appium + WinAppDriver script to:

  • Launch the TestWise IDE.
  • Create a new Selenium WebDriver +Mocha test project in TestWise IDE.
  • Script a complete user login test script.
  • Execute the newly created Selenium+Mocha test.

All within 53 seconds. (fully automated, no human intervention)

This article shows how you do it step by step.

1. Prerequisite: Install Node.js and Test libraries

Mocha test scripts are JavaScript files, so we need Node.js language runtime installed first.

Install the test libraries.

% npm install -G selenium-webdriver mocha

Besides the Chrome browser, we also need chromedriver. Refer to this quick installation guide.

2. Verify Test Execution Setup

I have a habit of immediately verifying the operation. Execute the following commands in a terminal.

% cd 
% git clone https://github.com/testwisely/agiletravel-ui-tests

If you don’t use Git (for version control), I highly encourage learning it, and it is quite easy to use. Check out the 10-Minute Guide to Git Version Control for Testers.

You will find a set of folders and files under ~/agiletravel-ui-tests . In the same terminal window, run these two commands (one by one).

% cd agiletravel-ui-tests/selenium-webdriver-nodejs-mocha/spec
% mocha 02_flight_spec.js

You shall see a Chrome browser open and run two test cases (within one test script file) in it.

3. Install TestWise IDE

Since the Selenium Mocha test scripts are JavaScript files, you may use any programming editor, such as Visual Studio Code or Java-specific IDEs, such as WebStorm.

TestWise IDE is a Next-Gen Functional Testing IDE designed for E2E testing. I learned Test Automation with TestWise (Disclaimer: My father created TestWise). You may use TestWise in free mode with just minor constraints (relaunch the app after 15 test executions).

You may choose any tool to develop Selenium Mocha test scripts (in plain text). That’s the beauty of being open-source and in a well-known language.

If you are not using TestWise, I encourage you to explore the test project structure that TestWise uses. It is simple and embraces the Maintainable Automated Test Design, which has been well-proven in many successful test automation projects. You can take advantage of the proven structure and supporting files (e.g. helper and page classes) even using a different testing tool.

4. Create a Test Project and your first Selenium Mocha test script in TestWise IDE.

TestWise uses the concept of ‘Project’ to confine the test scripts and supporting files.

1. Click menu ‘File’ > “New Project”.

Fill in the following information:

Project name: <any text>
Location: <an empty folder>
Automation Driver: Selenium WebDriver
Test Script Syntax: Mocha
Website URL: <a website base URL>

2. Click the “OK” button to create the test project.

Here is how the test project’s skeleton looks like in TestWise.

A brief explanation of the folder and files:

  • spec Folder:
    Contains test script files in the format of XXX_spec.js
  • pages Folder:
    Contains the page classes (see Page Object Model), the abstract_page.js is already created.
  • test_helper.js
    Shared test helper (see Maintainable Automated Test Design)
  • Rakefile & buildwise.rake
    For integrating with the BuildWise CT Server.
  • XXX.tpr File
    TestWise project file.

5. Create your first Selenium RSpec test in TestWise IDE.

Now, let’s create a raw Selenium Mocha test script quickly in TestWise.

3. Run the empty test script

Click new_spec.js (in the PROJECT EXPLORER pane on the left) to open in an editor.

Right-click any line in the test case it( "Test Case Name" do and select “Run “Test Case Name”” to run this individual test case.

You shall see a Chrome browser launch, open our target website and leave it open.

“Leaving the browser open” is a very useful feature in developing web test scripts. For more, check out this article, Innovative Solution to Test Automation: Keep the Browser Open after Executing an Individual Test.

4. Write test statements in TestWise.

By inspecting the page (right-click the page in Chrome and select ‘Inspect’), I have identified the locator for the controls on the page, such as “#username”, “#password”, and “//input[@value=‘Sign in’]”.

In the login example, the first Selenium statement is:

await driver.findElement(By.id(“username”)).sendKeys(“agileway”);

We can type it character by character, but there is a more efficient way (see video above): Snippets. Snippets are quick shortcuts (expanded to an actual statement by the Tab key).

Type aw to expand to await <CURSOR MOVES HERE>).

Type dfei to expand to driver.findElement(By.id("<CURSOR MOVES HERE>")). Since the cursor moves to the identifier, you can then enter "username" and Tab to go to the end of the line. Then, use the .sk snippet to expand to .sendKeys("<CURSOR>"). Finally, enter the text you want to send, "agileway"and Tab to complete.

Repeat a similar process for the remaining test steps. Then finish by adding an assertion step.

    await driver.getPageSource().then(function(page_source) {
assert(page_source.contains("Signed in!"))
});

5. Rerun the test case (in TestWise).

The test passed in TestWise.

This is only an introductory guide. There are many more handy TestWise features that can improve your productivity, such as “Run selected steps against the current browser” and “Functional Test Refactoring

6. A Complete Test Script

var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
// var test = require('selenium-webdriver/testing'); // deprecated in Selenium 4
var assert = require('assert');

var driver;
const timeOut = 15000;

String.prototype.contains = function(it) {
return this.indexOf(it) != -1;
};

const chrome = require("selenium-webdriver/chrome");

var helper = require('../test_helper');

// An example in below: var FlightPage = require('../pages/flight_page.js')
// BEGIN: import pages

// END: import pages


describe('Test Suite', function() {

before(async function() {
this.timeout(timeOut);
driver = new webdriver.Builder().forBrowser(helper.browserType()).setChromeOptions(helper.chromeOptions()).build();
// driver.manage().window().setSize(1280, 720);
// driver.manage().window().setPosition(30, 78);
});

beforeEach(async function() {
this.timeout(timeOut);
await driver.get(helper.site_url());
});

after(async function() {
if (!helper.is_debugging()) {
driver.quit();
}
});

it('Can log in successfully', async function() {
this.timeout(timeOut);
await driver.findElement(By.id("username")).sendKeys("agileway");
await driver.findElement(By.name("password")).sendKeys("testwise");
await driver.findElement(By.xpath("//input[@value='Sign in']")).click();
await driver.findElement(By.id("flash_notice")).getText().then(function(element_text) {
assert.equal(element_text, "Signed in!");
});

});

});

You can find the above test scripts and more on this Github repository.

--

--