ZK Testing with Sahi

From Documentation
ZK Testing with Sahi

Enrico Tedeschini
October 15, 2013
ZK 6.5 (or latest)


This article describes how it’s easy to test your ZK web application with Sahi. Sahi offers a completely different solution from ZATS. ZATS is a powerful solution to test a pure web application functionality and incredible product for Automate Unit Tests without server and browser. However, if you want to test your ZK web application in a real environment with different browsers, different servers and different network traffic/latency you have to use a tool like Sahi. I tried different tools like Selenium, JMeter and others but all of them require changing code. They require to create a Custom ID Generator as described in this article Perform stress test on ZK using JMeter-take Shopping Cart as an example.

All tests described here are performed with a Sahi Pro Version. But all of them work as well with Sahi Open Source Version. From my point of view Sahi is a powerful tool for regression, stress, reliable and performing tests for any ZK web applications. For example Sahi can help you in the troubleshooting process. When you see a problem you only have to, through Sahi Dashboard, record all steps and playback them later. All of this without writing any code or having any particular skill. Sahi works on Windows and Linux platform as well. In this article I described how to test a ZK web application on Windows platform.


Microsoft Windows 7 platform with

  • Firefox Version 22.x
  • Chrome Version 29.x
  • Internet Explorer Version 8.x
  • Safari Version 5.1.x
  • JRE 6 or later
  • Apache Tomcat 6.x or other Web Server or Application Server


Download and Install Sahi

  1. Download Sahi Pro Trial Version. Go to http://sahi.co.in/sahi-pro/, press Free Trial button and fill the form to download a fully functional version of Sahi Pro V5.0.9, valid for 1 month. The license key and a download link will be emailed to you.

  2. Install Sahi Pro wherever you want. In this example Sahi Pro was installed on C:/workspace/sahi_pro directory. It is recommended to install Sahi in a folder without spaces in the path. You don’t need to change any other setting. Press Next button until the end where you have to press, Done to complete the installation.

  3. Copy the license.data file received by email on C:/workspace/sahi_pro/userdata/config directory

Configure Sahi

Start Sahi Dashboard by double click on the desktop shortcut.

Then press the Configure hyperlink to make visible on the Dashboard all browsers installed on your computer. Browsers’ path should match the browser location on your computer. When all browsers location match you can see a Sahi Dashboard like this.

Install ZK sample project

To perform these tests it was created an essential ZK web application project.
This is the web application directory structure (Source code files included)

ExampleContacts - WebContent/
       ZK CE 6.5.x libraries

And when you run this ZK web application the result is as follows:


To do this follow the instructions below:

  1. Download the ExampleContacts.war (Web application archive).
    The ExampleContacts is hosted on github here. Note that the use of ExampleContacts is free for non-profit organisations, students, universities, public schools and non-commercial personal websites.
  2. Deploy this ZK web application in your web server or application server.
  3. Open a Browser instance from Sahi Dashboard. Go to the navigation bar, write http://localhost:8080/ExampleContacts/ and press Enter.
    The above html page will appear otherwise do not forget to verify the right hostname and port in case you have a different server configuration.

Create a Sahi script

So, now you have to create your first Sahi script.
To do this follow these instructions:

  1. Create a demo.sahi with this content
    // Script to test ExmapleContacts web application
    _assertExists(_cell("Add"));      // check if exist 'Add' button
    _assertExists(_cell("Edit"));     // check if exist 'Edit' button
    _assertExists(_cell("Remove"));   // check if exist 'Remote' button
    _assertExists(_cell("Refresh"));  // check if exist 'Refresh' button
    _setValue(_textbox(0,  _near(_span("Search:"))), ""); // Clear input filter 
    _click(_cell("Refresh"));  // Press 'Refresh' button 
    // Verify if exist "Camille Bauer" contact
    if (_exists(_div("Camille Bauer"))) {
      _click(_click(_div("Camille Bauer"))); // select "Camille Bauer" row
      _click(_cell("Edit")); 				 // edit contact 
      _wait(2000);  						 // wait few seconds just to see the dialog box
      _click(_cell("Cancel")); 				 // press Cancel and exit from dialog box
    // Verify if exist "Benedict Ewing" contact
    if (_exists(_div("Benedict Ewing"))) {
       _click(_div("Benedict Ewing"));      // select "Benedict Ewing" row
       _click(_cell("Remove"));             // remove "Benedict Ewing" contact
       _wait(2000);                         // wait few seconds just to see the dialog box
       _click(_cell("OK"));			    // press OK to confirm
    _click(_image("add_contact.png")) // Press 'Add' button to open  'Add Contact' form
    // fill input fields 
    _setValue(_textbox(0,  _near(_span("Name *"))), "Benedict Ewing");
    _setValue(_textbox(0,  _near(_span("Address"))), "3393 Gravida. St.");
    _setValue(_textbox(0,  _near(_span("Country"))), "United Kingdom");
    _setValue(_textbox(0,  _near(_span("Phone"))), "+44 605 4296-9504");
    _setValue(_textbox(0,  _near(_span("Email"))), "[email protected]");
    // Move cursor between input fields - fix IE and ZK constraints problem
    if (_isIE()) {
      _focus(_textbox(0,  _near(_span("Name *"))));
      _focus(_textbox(0, _near(_span("Address"))));
    _click(_cell("OK")); // press OK to confirm '"Benedict Ewing' as new contact
    _setValue(_textbox(0,  _near(_span("Search:"))), "Benedict");  // Fill input filter with new contact's name
    _keyPress(_textbox(0, _near(_span("Search:"))), [13, 0])       // Press enter to confirm
  2. Copy this script in C:/workspace/sahi_pro/userdata/scripts directory

This sample script performs different operations:

  • Assertions check,
  • navigation,
  • fill input fields
  • and execution of simple actions

Enough to understand how Sahi works.

Or through Sahi Controller you can record your scripts. For this follow the instructions in this document - see chapter Recording through Sahi

Running test from Sahi controller

Now we are ready to start the browser and to run the test.

  1. From Sahi Dashboard launch IE browser or Firefox browser or Chrome browser
  2. Press the ALT key and Double Click on page to bring up the Sahi Controller
  3. Click on Playback tab in the Sahi Controller
  4. Fill out demo.sah in the File field
  5. Fill out http://localhost:8080/ExampleContacts/ in the Start URL field
  6. Click Set button and wait until the page http://localhost:8080/ExampleContacts/ is loaded or reloaded
  7. Click Play button to run the test
  8. Wait until all steps are performed. SUCCESS or FAILURE message will be displayed at the end of the steps.
    Click on Log link at the bottom of Sahi Controller to see all test details in a HTML report page.


Running tests in sequence from batch file

To run a single test with Firefox, Chrome, Internet Explorer and Safari in sequence you have to follow this instruction:

  1. Create a demo.bat batch file with this four lines wherever you want
    cd C:\Workspace\sahi_pro\userdata\bin
    call testrunner.bat "demo.sah" "http://localhost:8080/ExampleContacts/" "firefox" 
    call testrunner.bat "demo.sah" "http://localhost:8080/ExampleContacts/" "chrome"
    call testrunner.bat "demo.sah" "http://localhost:8080/ExampleContacts/" "ie" 
    call testrunner.bat "demo.sah" "http://localhost:8080/ExampleContacts/" "safari"

  2. Verify Sahi Dashboard running
  3. Close all browser instances
  4. Launch demo.bat to run the test.
  5. Wait until all steps are performed.
    Click on Log link at the bottom of Sahi Dashboard to see all test details in a HTML report page.

Running tests in parallel from batch file

Sahi allows parallel or multithreaded playback. Multiple tests can be run simultaneously and you can also spread the tests across machines. To run three simultaneously test with Firefox you have to follow this instruction:

  1. Create a demo.suite file with these three lines.

    Each line will be running simultaneously
  2. Copy demo.suite in in C:/workspace/sahi_pro/userdata/scripts directory
  3. Create a demo_suite.bat batch file with these lines wherever you want
    cd C:\Workspace\sahi_pro\userdata\bin
    call testrunner.bat "demo.suite" "http://localhost:8080/ExampleContacts/" "firefox"

  4. Verify Sahi Dashboard running
  5. Close all browser instances
  6. Launch demo_suite.bat to run the test.
  7. Wait until all steps are performed.
    Click on Log link at the bottom of Sahi Dashboard to see all test details in a HTML report page.

Troubleshooting ID Generator

As stated in the Introduction, Sahi does not require creating a Custom ID Generator.
In your script you have to identify your element with _near and _in functions.

This image shows the Edit Contact dialog box. Suppose you have to fill the Country field with United Kingdom. To identify the input field and fill it you have different solutions.

Solution 1 (WRONG): Write

_setValue(_textbox(X), "United Kingdom");

Where X is the ZK component's UUID. As we know, ZK by default generate random component's ID for preventing Cross-Site Request Forgery. Reason why this solution it’s not feasible.

Solution 2 (RIGHT): Write

_setValue(_textbox(0,  _near(_span("Country"))), "United Kingdom");

Where the _near function identify the Country input field by the nearest DOM Country label. With this solution all problems related to the ZK ID are solved.

Using _near or _in functions is a simple and powerful solution when DOM elements are not uniquely identifiable by themselves.


This Small Talk is created as a simple demonstration of Sahi and ZK. However, if you want to use Sahi in a more advanced way with more complex functions, you may consider to read this powerful documentation http://sahi.co.in/w/file_download/5/SahiPro_V4_2_Documentation.pdf.
Do not forget to read about the advanced way of automation tests via its Excel Framework and Ant scripts.


Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.