Acceptance testing of Meteor Application using Selenium-Nightwatch and Velocity

In my previous tutorial, I presented how to develop Meteor applications in BDD style with unit testing and integration testing. It is time to explore, how to perform acceptance testing of your app. For this, we have selected the selenium-nightwatch package and velocity.

Since this is my first time using this test suite, I plan to learn about it as I write. If you have any comments, please let me know if I am using it incorrectly

I started by reading the documentation at the Github page of the Selenium-Nightwatch project and tried to install the package according to following instructions from the author. This is where my problems begun, since author stressed that it is required to run meteor with administrator privileges, what completely messed up build of all my packages and from this moment on I had to run all my meteor projects with administrator privileges. I shrieked in horror. After several hours of trial and error I messed up my whole Meteor installation and had to reinstall Node.js, Meteor and all Meteorite packages (I deleted them from ~/.meteorite directory).

I really did not want to run Meteor with admin privileges, so I spent quite some time figuring it out. Following is a bash script which will safely bring selenium-nightwatch into your project, without requiring to run meteor as administrator (yet nightwatch still has to be run with administrator privileges). Also, in my approach I install night watch globally, sine the provided scripts try to install night watch into project directory, what many times failed and also required admin privileges to run meteor.

// in terminal
$ cd toYourProjectRoot
$ mrt add selenium-nightwatch
$ sudo -H npm install -g nightwatch
$ ln -s packages/selenium-nightwatch/launch_nightwatch_from_app_root.sh run_velocity_from_app_root.sh
$ ln -s packages/selenium-nightwatch/launch_nightwatch_from_app_root.sh run_root_nightwatch.sh

Now we need to modify the launcher scripts, so that they do not call the local night watch instance, but the global one (I have commented out original functionality and added mine):

# file: packages/selenium-nightwatch/launch_nightwatch_from_app_root.sh
#!/bin//bash
#echo "installing nightwatch in .meteor/local/build"
#  cd .meteor/local/build
#  sudo npm install nightwatch@0.5.3
#  cd ../../../

echo "running nightwatch from app root"
#   sudo .meteor/local/build/node_modules/nightwatch/bin/nightwatch -c packages/selenium-nightwatch/nightwatch_from_app_root.json $1 $2
sudo nightwatch -c packages/selenium-nightwatch/nightwatch_from_app_root.json $1 $2

Also we modify the launcher file for velocity:

# file: packages/selenium-nightwatch/launch_nightwatch_from_velocity.sh
#!/bin//bash
# echo "installing night watch in .meteor/local/build"
#  cd .meteor/local/build
#  sudo npm install nightwatch@0.5.3
#  cd ../../../

echo "velocity is launching nightwatch"
#   sudo .meteor/local/build/node_modules/nightwatch/bin/nightwatch -c packages/selenium-nightwatch/nightwatch_from_velocity.json $1 $2
sudo nightwatch -c packages/selenium-nightwatch/nightwatch_from_velocity.json $1 $2

WARNING! Up to date I was not able to run selenium-nightwatch with Velocity due to the possible bug in parsing the selenium report files. I had to delete all selenium-nightwatch report files to restore the functionality. I will update this tutorial once I will be sure that it works.

We are ready to run your acceptance tests! To run tests for velocity call simply ./run_velocity_nightwatch.sh in your application root.

To run tests without velocity run ./run_root_nightwatch.sh -t path/to/testfile.js. In this tutorial we will be running the test only in terminal, since velocity was crashing our app when parsing selenium report.

We are ready to write our first test! The documentation makes it look dead simple, so let’s get right to it. First, I checked in smart.json file how velocity identifies test suites. In case of selenium-nightwatch package, this information resides in nightwatch_from_velocity.json file and tells us following "src_folders" : ["tests/nightwatch"]. So, we create a directory in /tests/nightwatch and try to code our first acceptance test.

// file: tests/nightwatch/helloworld-test.js
module.exports = {
"Hello World" : function (client) {
client
.url("http://127.0.0.1:3000")
.waitForElementVisible("body", 1000)
.assert.title("Hello World")
.end();
}
};

We run this test in terminal with ./run_root_nightwatch.sh -t tests/nightwatch/admin-test.js and obtain following result:

Terminal

Since in our template we did not set the page title our test fails. Great, we got it working! Let’s try to write something more meaningful related to our application! In this test, we expand the example from our previous tutorial and we perform following checks:

  1. Login as admin in the introduction (home) page
  2. We navigate to “/createTutorial” page
  3. We create a new tutorial
  4. We check that after this tutorial was created, application redirects to “tutorials” page

And here is the code of the test:

//file:tests/nightwatch/admin-test.js
module.exports = {
    "Test admin functionality - create, modify and delete tutorial" : function (client) {
        client
            .url("http://localhost:3000/createTutorial")
            .waitForElementVisible("body", 1000)
            .click("li#login-dropdown-list a")
            .pause(100)
            .assert.visible("input#login-email")
            .assert.visible("input#login-password")
            .setValue("input#login-email", "admin@example.com")
            .setValue("input#login-password", "apple1")
            .click("button#login-buttons-password")
            .assert.visible("input#tutorialName")
            .assert.visible("input#tutorialCapacity")
            .assert.visible("button#modifyTutorialButton")
            .setValue("input#tutorialName", "NightWatchTutorial")
            .setValue("input#tutorialCapacity", "10")
            .click("button#modifyTutorialButton")
            .pause(2000)
            .assert.urlEquals('http://localhost:3000/tutorials')
            .end();
    }
};

We run this test as ./run_root_nightwatch.sh -t tests/nightwatch/admin-test.js and following is the output we obtain in console:

Terminal

Mission accomplished, well, at least part of it! You can download the code from GitHub.

Advertisements

4 thoughts on “Acceptance testing of Meteor Application using Selenium-Nightwatch and Velocity

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s