Automate testing with Behat, Selenium, PhantomJS, and Nightwatch.js

This blog post revolves around the notion of testing, why it is required and the tools that facilitate it.

Let’s  start with the basics - Why test?

The answer to this is to ensure what we have developed and implemented is working the way it is supposed to work. An application or software that works when one person is using, may not work for others. Moreover, there are various different devices, browsers, and operating systems in use.  These and many other scenarios could arise, and thus in order to deliver the best application to our end users, testing is a crucial phase.

 

Here is a quick peek at how testing is done:

behat and selenium

Testing is further segregated into 3 categories:

 

behat and selenium

 

MANUAL VS AUTOMATED TESTING – which is a safer bet?

In spite of thorough manual testing, software can still have defects. With all the efforts undertaken to catch them before a product is released, few still remain, even after following the best manual testing processes.

Automated testing is the best way to increase effectiveness, efficiency and coverage of software testing minimizing risk of defects automated testing is done on two types of browsers, the Real Browser and the Headless Browser.

 

REAL BROWSER TESTING

On Real Browsers functions or commands are performed and run over the UI. Examples of Real browsers are Google Chrome, Firefox, and Internet Explorer etc.

 

HEADLESS BROWSER TESTING

Headless browsers are basically web browsers without a graphical user interface. Examples of Headless browsers are PhantomJS, ZombieJS etc.

 

REAL VS HEADLESS BROWSER TESTING

  • Headless Browsers are faster than Real Browsers

Real browsers take time in opening, rendering HTML, CSS, Javascript and images as compared to headless browsers. Headless browsers start performing functions without waiting for page to load completely.

  • Headless browsers do not represent real users

Bugs like images crashing or the UI not showing up are difficult to detect in headless browsers.

  • Error detection in Real Browsers is effortless

Since the functions are performed right in front of the user and he can view them, it is easier to detect where the tests are failing, and can be easily debugged.

  • Headless browsers do not support Ajax

Headless browsers do not support Ajax while Real browsers do.

 

Now that we have traversed through the need and types of testing, let us have a look at the tools used for testing:

  • SELENIUM WEB DRIVER

Selenium WebDriver is a collection of open source APIs that automate testing of web applications. It is platform-independent.

 

  • BEHAT

Behat is an open source Behavior-Driven Development framework for PHP. It tests the behavior of applications, and is described in a special language called Gherkin.

Gherkin is a business readable, domain specific language created for behavior descriptions. It enables you to remove logic details from behavior tests.

 

  • MINK

Mink is the browser simulator for Behat. It is a browser emulator abstraction layer. It hides emulator differences behind a single, consistent API. Mink supports both in-browser and headless browser emulators. It is a PHP 5.3+ library.

You can download and install Behat and Mink: http://docs.behat.org/en/v2.5/cookbook/behat_and_mink.html

 

  • PHANTOMJS

PhantomJS is a scripted, headless browser used for automating web page interaction. PhantomJS provides a JavaScript API enabling automated navigation, screenshots, user behavior and assertions making it a common tool used to run browser-based unit tests in a headless system like a continuous integration environment.” – Wikipedia

You can download and install PhantomJS: http://phantomjs.org/download.html

PhantomJS is an optimal solution for -

  • Headless website testing:

Run functional tests with frameworks such as Jasmine, QUnit, Mocha, Capybara, WebDriver, and many others

  • Screen capture:

Programmatically capture web contents, including SVG and Canvas. Create web site screenshots with thumbnail preview.

  • Development:

While developing UI, you can write the code by selecting the HTML element wherever you wish.

 

  • CASPERJS

CasperJS is a navigation scripting and testing utility for PhantomJS headless browser, written in JavaScript.

 

  • NIGHTWATCH.JS

A Browser Automation tool that is an easy to use Node.js based End-to-End (E2E) testing solution for browser based apps and websites.

It uses the powerful W3C WebDriver API to perform commands and assertions on DOM elements.

You can download and install NightWatch.js:

 http://nightwatchjs.org/getingstarted#installation

 

Now the tricky part is, to know ‘What to use and When’

 

behat gif

 

 

 

 

 

 

 

 

Drupal
Selenium
Behat
PhantomJS
Nightwatch.js
CasperJS
Mink
Manual Testing
Automated Testing
Headless Browser

Get in Touch

Get Aggregated Monthly Industry News