REST testing with jMeter

Testing RESTful application is very simple with jMeter. In the first part of the tutorial in this series, the basics are shown. As requests can be created and evaluated. The request going to http://httpbin.org.

Preconditions

  • JAVA is installed
  • jMeter is installed

Steps

Create new “Thread-Group” on “Test Plan” (Add – Threads (User) – Thread Group)

jmeter thread group

Now add one “HTTP Request” inside the “Thread Group” (Add – Sampler – HTTP Request)

jmeter http request

Inside the “HTTP Request” we add now a “HTTP Header Manager” (Add – Config Element – HTTP Header Manager). For configuring we add the Name/Value – “Content-Type”/”application/json”.

jmeter http header manager

At the end we add the “View Results Tree” on “Thread Group” (Add – Listener – View Results Tree)

jmeter view results tree

Our first request example will be a “GET”. On HTTP Request we add following values.

  • Server Name or IP: httpbin.org
  • Method: GET
  • Path: /ip
  • Implementation: HttpClient4
  • Follow Redirects: yes
  • Use KeepAlive: yes
jmeter get request

After save we can start the first run. How to see result will show later. Now second request with POST example. For this we modify the HTTP Request with following values.

  • Method: POST
  • Path: /post
  • Send Parameters With the Request: {“firstName”:”harry”;”lastName”:”Hirsch”}
  • Include Equals?: yes
jmeter post request

Now save and run again. On View Results Tree we can check the results for every http request (Sampler results, Request, Response data).

jmeter rest results

For the JSON response, switching from Text to JSON view. The same way can used for PUT, DELETE or other requests.

Layer 3 discovery with Ping

If ICMP (Internet Control Message Protocol) is not blocked – the ping command is one of my most used tools.

Example

# Syntax
$ ping -c <number> <target>

# Example
$ ping -c 1 192.168.0.1

Usage

With a small bash-script it is possible to identify all hosts that respond to ICMP requests.

#!/usr/bin/env bash

# define shell options
set -e
set -u

# define magic variables
declare -r FILE_NAME=$(basename "$0")
declare -r -i SUCCESS=0
declare -r -i NO_ARGS=84
declare -r -i BAD_ARGS=85

# usage function
function fc_usage() {
  printf "Usage: %s -i 192.168.1.0" "$FILE_NAME"
  printf " [-h]\n"
}

# help function
function fc_help() {
  fc_usage
}

# error communication functions
function fc_no_args() {
  printf "Error: no arguments supplied\n"
  exit "$NO_ARGS"
}

function fc_bad_args() {
  printf "Error: wrong arguments supplied\n"
}

# check script arguments
if [ "$#" -eq 0 ]; then
  fc_no_args
fi

while getopts "hi:" OPTION; do
  case "$OPTION" in
      h)
        fc_help exit "$SUCCESS" ;;
      i)
        PREFIX=$(echo "$OPTARG" | cut -d '.' -f 1-3) ;;
      *)
        fc_bad_args
        fc_usage exit "$BAD_ARGS" ;;
  esac
done 

# main
for ADDR in $(seq 1 254); do
  ping -c 1 "$PREFIX"."$ADDR" | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1 &
done

# exit
exit "$SUCCESS"

Jenkins and jslint4java

This tutorial shows a very simple Jenkins – jslint4java integration over Shell.

Preconditions

Jenkins is installed

Steps

Start Jenkins, open the browser (http://<host>:8080) and setup a new “Free Style Project” (JS-Lint-Example).

# start jenkins
$ service jenkins start

Back to Jenkins… here you create a “Freestyle-Project”

Jenkins Freestyle Project

The tutorial does not use any VCS, so we press the “Build Now” button to create the project “workspace” folder. After successful creation, we create 3 new folders (src, lib, build) inside the workspace folder.

# create new folders inside the workspace
$ cd /var/lib/jenkins/workspace/JS-Lint-Example/
$ mkdir {src,lib,build}

# change owner of folders
$ chown jenkins:jenkins src/ lib/ build/

Now upload the jslint4java into the “lib” folder.

# upload jar to lib folder
$ scp jslint4java-2.0.5.jar <user>@<host>:/var/lib/jenkins/workspace/JS-Lint-Example/lib

# change owner and make executable
$ chown jenkins:jenkins jslint4java-2.0.5.jar
$ chmod u+x jslint4java-2.0.5.jar

It`s time to test if jslint4java is running. The 1st test can be done inside the terminal self.

$ java -jar jslint4java-2.0.5.jar --help

The 2nd test direct on Jenkins. Open the “JS-Lint-Example” job in browser – press link “Configure” and we create a “Build-Step” with “Execute Shell”.

Jenkins JSLint Test

After save and build the “Console Output” of the respective build should show the help. If there is a problem here, please check if Java is installed and the file permissions are correct. If everything works fine we create a simple JavaScript file and upload this into the “src” folder.

function myFunction(p1, p2) {
  return p1 * p2;
}

var person = {
  firstName:"John",
  lastName:"Doe",
  age:50,
  eyeColor:"blue"
};

test = myFunction(10, 10);

document.write('Hello World');
document.write(test);
document.write(person.firstName);
# upload example.js to src folder
$ scp example.js <user>@<host>:/var/lib/jenkins/workspace/JS-Lint-Example/src

Now we change the “Execute Shell” command and add the “Post-Build-Action” – “Publish JUnit Test Results” with value “build/js_report.xml”.

Jslint Jenkins Configuration
java -jar ${WORKSPACE}/lib/jslint4java-2.0.5.jar ${WORKSPACE}/src/example.js --report junit > ${WORKSPACE}/build/js_report.xml

Ready,… after save and new build we can see the file “js_report.xml” into “build” folder and the jUnit  report into our project.

Jslint Jenkins jUnit Report

Wireshark and OS X Yosemite

Since X11 is no more included, you need XQuartz to run Wireshark on Yosemite. Wireshark is expecting XQuartz (X11) to be inside the folder “/usr” but it is now under “/opt”. With a symbolic link you can solve it easily.

# create symbolic link
$ sudo ln -s /opt/X11 /usr/X11

After install and create symbolic link you download can install Wireshark. Now you can start Wireshark.

# start wireshark
$ sudo wireshark

Note: The start may take some time.

Robot Framework and Selenium-Grid 2.0

This small tutorial shows, how to run a generic Robot Framework test with Selenium Grid.

Preparation

Steps

Now we create a very simple test

*** Settings ***
Documentation         This is just a tutorial
...
Metadata              VERSION    0.1
Library               Selenium2Library
Suite Setup           Start Browser
Suite Teardown        Close Browser

*** Variables ***
${SERVER}             https://www.google.ch
${BROWSER}            firefox

*** Keywords ***
Start Browser
    [Documentation]   Start firefox browser on Selenium Grid
    Open Browser      ${SERVER}   ${BROWSER}   None    http://127.0.0.1:4444/wd/hub

*** Test Cases ***
Check something
    [Documentation]   Check the page title
    Title Should Be   Google

Now start the Selenium Grid (Hub and Node)

# start the hub (in terminal 1)
$ java -jar selenium-server-standalone-2.44.0.jar -role hub -port 4444 

# start the node (in terminal 2)
$ java -jar selenium-server-standalone-2.44.0.jar -role node -hub http://localhost:4444/grid/register

Note: The actual version for selenium-server-standalone may be different!

Open the browser http://localhost:4444/grid/console and check the node is registered. This step can be skipped, just to be sure.

In the last step we start the test. Open a 3rd terminal, browse to the folder and start the Robot Framework.

# run pybot
$ pybot tutorial.robot

If everything works well, the output should look like this:

================================================================
Tutorial :: This is just a tutorial
================================================================
Check something :: Check the page title                 | PASS |
----------------------------------------------------------------
Tutorial:: This is just a tutorial                      | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
================================================================
Output: /output.xml
Log: /log.html
Report: /report.html

Command line SQLite recipes

As a software developer or tester you get in touch with SQLite? Here are a few recipes that make your work easier.

Backup SQLite

Create a copy of the entire database

# change directory (where Database is located)
$ cd /path/to/folder

# start full dump of entire database
$ sqlite3 myDatabase.db .dump > myFullBackup.sql

Create copy of a specific table

# change directory (where Database is located)
$ cd /path/to/folder

# dump only specific table
$ sqlite3 myDatabase.db ".dump myTable" > mySpecificBackup.sql

Create copy of specific table as CSV or HTML

# change directory (where Database is located)
$ cd /path/to/folder

# cvs without header
$ sqlite3 -csv myDatabase.db "SELECT * FROM myTable;" > mySpecificBackup.csv

# csv with header
$ sqlite3 -header -csv myDatabase.db "SELECT * FROM myTable;" > mySpecificBackup.csv

# html without header
$ sqlite3 -html myDatabase.db "SELECT * FROM myTable;" > mySpecificBackup.html

# html with header
$ sqlite3 -header -html myDatabase.db "SELECT * FROM myTable;" > mySpecificBackup.html

Create SQLite database

# change directory (where Database should located)
$ cd /path/to/folder

# restore from dump
$ sqlite3 myNewDatabase.db < myCreateFile.sql

Select query from Terminal

# change directory (where Database is located)
$ cd /path/to/folder

# simple output
$ sqlite3 myDatabase.db "SELECT * FROM myTable;"

# with header
$ sqlite3 -header myDatabase.db "SELECT * FROM myTable;"

# with header and column
$ sqlite3 -header -column myDatabase.db "SELECT * FROM myTable;"

# select as command line
$ sqlite3 -line myDatabase.db "SELECT * FROM myTable;"

#select as html
$ sqlite3 -html myDatabase.db "SELECT * FROM myTable;"

Merge sqlite tables with same schema

Imagine there are two databases (db_1.db and db_2.db) with tables (myTable) in same schema. Now we merge the content of table “myTable” from database “db_2.db” into “db_1.db”.

# change to directory where databases are located
$ cd /path/to/folder

# start sqlite commands
$ sqlite db_1.db

# run all needed commands
sqlite> ATTACH 'db_2.db' AS toMerge;
sqlite> BEGIN;
sqlite> INSERT INTO myTable SELECT * FROM toMerge.myTable;
sqlite> COMMIT;
sqlite> .quit

Fast PHP server

Since PHP 4.x, there is a CLI SAPI web server. As a tester you simply go to the folder and start the server.

Example

# change to directory where server should start
$ cd /path/to/folder

# start server with specific port
$ php -S localhost:8100

Now start your browser and insert the URL “localhost:8100”. With “Ctrl+c” you can stop the server.

Extended

# explicit document root
$ cd /path/to/folder
$ php -S localhost:8100 -t foo/bar/

# specific configuration
$ cd /path/to/folder
$ php -S localhost:8100 -c php.ini