ZK Testing with Sahi"

From Documentation
m
 
(24 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[ZK_Testing_with_Sahi]]
 
 
 
{{Template:Smalltalk_Author|
 
{{Template:Smalltalk_Author|
 
|author=Enrico Tedeschini
 
|author=Enrico Tedeschini
|date=October 04, 2013
+
|date=October 15, 2013
 
|version=ZK 6.5 (or latest)
 
|version=ZK 6.5 (or latest)
 
}}
 
}}
 +
  
 
__TOC__
 
__TOC__
 
==== Draft Mark ====
 
  
 
=Introduction=
 
=Introduction=
 
This article describes how it’s easy to test your ZK web application with [http://sahi.co.in/ Sahi].  
 
This article describes how it’s easy to test your ZK web application with [http://sahi.co.in/ Sahi].  
Sahi offers a completely different solution from [http://www.zkoss.org/product/zats ZATS].  ZATS is a powerful solution to test a pure web application functionality and incredible product for Automate Unit Tests without server and browser. If you have 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 [http://books.zkoss.org/wiki/Small_Talks/2012/May/Perform_stress_test_on_ZK_using_JMeter-take_Shopping_Cart_as_an_example#Generate_new_desktop_ID_for_redirecting_to_a_new_page Perform stress test on ZK using JMeter-take Shopping Cart as an example].
+
Sahi offers a completely different solution from [http://www.zkoss.org/product/zats 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 [http://books.zkoss.org/wiki/Small_Talks/2012/May/Perform_stress_test_on_ZK_using_JMeter-take_Shopping_Cart_as_an_example#Generate_new_desktop_ID_for_redirecting_to_a_new_page 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.   
 
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.  
 
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, recording all steps and playback them later. All of this without writing any code or having any particular skill.  
+
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 is described how to test a ZK web application on Windows platform.
+
Sahi works on Windows and Linux platform as well. In this article I described how to test a ZK web application on Windows platform.
  
 
=Environment=
 
=Environment=
Line 32: Line 29:
 
==Download and Install Sahi==  
 
==Download and Install Sahi==  
 
<ol>
 
<ol>
<li>Download Sahi Pro Trial Version.  Go to [http://sahi.co.in/sahi-pro/ 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.
+
<li>Download Sahi Pro Trial Version.  Go to [http://sahi.co.in/sahi-pro/ 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.
 
</li>
 
</li>
 
<br/>
 
<br/>
<li>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 always '''Next''' button until the end where you have to press, '''Done''' to complete the installation.
+
<li>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.
 
<br/>
 
<br/>
 
[[Image:Image1.png‎]]
 
[[Image:Image1.png‎]]
Line 45: Line 42:
 
</ol>
 
</ol>
 
<br/>
 
<br/>
 +
 
==Configure Sahi==
 
==Configure Sahi==
 
Start Sahi Dashboard by double click on the desktop shortcut. <br/>[[Image:Image2.png‎]]
 
Start Sahi Dashboard by double click on the desktop shortcut. <br/>[[Image:Image2.png‎]]
Line 60: Line 58:
 
     add_contact.png
 
     add_contact.png
 
     edit_contact.png
 
     edit_contact.png
     refresh.png
+
     refresh_contacts.png
 
     remove_contact.png
 
     remove_contact.png
 +
  licenses
 +
    COPYING
 +
    COPYING.LESSER
 +
    COPYING.zk_libraries
 
   META-INF/     
 
   META-INF/     
 
     MANIFEST.MF
 
     MANIFEST.MF
Line 81: Line 83:
 
           ViewController.java
 
           ViewController.java
 
     lib/
 
     lib/
       ZK libraries
+
       ZK CE 6.5.x libraries
 
     web.xml
 
     web.xml
 
     zk.xml
 
     zk.xml
Line 91: Line 93:
  
  
And when you running this ZK web application the result is this:
+
And when you run this ZK web application the result is as follows:  
 
<br/><br/>
 
<br/><br/>
 
[[Image:Image6.png‎]]
 
[[Image:Image6.png‎]]
  
 
<br/>
 
<br/>
To do this follow these instructions:
+
To do this follow the instructions below:
 
<ol>
 
<ol>
<li>Download [ExampleContacts1.0.war] <span style="color:red">TODO add download war</span> ZK web application sample project. </li>
+
<li>Download the '''ExampleContacts.war''' (Web application archive). <br/>
 +
The ExampleContacts is hosted on github [https://github.com/etedesco/ExampleContacts here].
 +
Note that the use of ExampleContacts is '''free''' for non-profit organisations, students, universities, public schools and non-commercial personal websites.
 +
</li>
 
<li>Deploy this ZK web application in your web server or application server. </li>
 
<li>Deploy this ZK web application in your web server or application server. </li>
 
<li>Open a Browser instance from Sahi Dashboard. Go to the navigation bar, write
 
<li>Open a Browser instance from Sahi Dashboard. Go to the navigation bar, write
Line 114: Line 119:
 
<source  lang="java">
 
<source  lang="java">
  
// // Script to test ExampleContacts web application
+
// Script to test ExmapleContacts web application
 
//--------------------------------------------------------
 
//--------------------------------------------------------
  
Line 123: Line 128:
  
 
_setValue(_textbox(0,  _near(_span("Search:"))), ""); // Clear input filter  
 
_setValue(_textbox(0,  _near(_span("Search:"))), ""); // Clear input filter  
 
 
_click(_cell("Refresh"));  // Press 'Refresh' button  
 
_click(_cell("Refresh"));  // Press 'Refresh' button  
  
Line 139: Line 143:
 
   _click(_cell("Remove"));            // remove "Benedict Ewing" contact
 
   _click(_cell("Remove"));            // remove "Benedict Ewing" contact
 
   _wait(2000);                        // wait few seconds just to see the dialog box
 
   _wait(2000);                        // wait few seconds just to see the dialog box
   _click(_button("OK"));     // press OK to confirm
+
   _click(_cell("OK"));     // press OK to confirm
 
}
 
}
  
Line 145: Line 149:
  
 
// fill input fields  
 
// fill input fields  
_setValue(_textbox(0,  _near(_span("Name (*)"))), "Benedict Ewing");
+
_setValue(_textbox(0,  _near(_span("Name *"))), "Benedict Ewing");
 
_setValue(_textbox(0,  _near(_span("Address"))), "3393 Gravida. St.");
 
_setValue(_textbox(0,  _near(_span("Address"))), "3393 Gravida. St.");
 
_setValue(_textbox(0,  _near(_span("Country"))), "United Kingdom");
 
_setValue(_textbox(0,  _near(_span("Country"))), "United Kingdom");
Line 153: Line 157:
 
// Move cursor between input fields - fix IE and ZK constraints problem
 
// Move cursor between input fields - fix IE and ZK constraints problem
 
if (_isIE()) {
 
if (_isIE()) {
   _focus(_textbox(0,  _near(_span("Name"))));
+
   _focus(_textbox(0,  _near(_span("Name *"))));
 
   _focus(_textbox(0, _near(_span("Address"))));
 
   _focus(_textbox(0, _near(_span("Address"))));
 
}
 
}
Line 159: Line 163:
  
 
_setValue(_textbox(0,  _near(_span("Search:"))), "Benedict");  // Fill input filter with new contact's name
 
_setValue(_textbox(0,  _near(_span("Search:"))), "Benedict");  // Fill input filter with new contact's name
_keyUp(_textbox(0, _near(_span("Search:"))), [13,0]);        // Press enter to confirm  
+
_keyPress(_textbox(0, _near(_span("Search:"))), [13, 0])       // Press enter to confirm  
 +
 
  
 
</source>
 
</source>
Line 168: Line 173:
 
This sample script performs different operations:
 
This sample script performs different operations:
 
*Assertions check,
 
*Assertions check,
*Navigation,
+
*navigation,
*Fill input fields  
+
*fill input fields  
 
*and execution of simple actions
 
*and execution of simple actions
 
Enough to understand how Sahi works.
 
Enough to understand how Sahi works.
  
Or through Sahi Controller you can record your scripts. For this follow the instruction that you find in this [http://sahi.co.in/w/file_download/5/SahiPro_V4_2_Documentation.pdf document] - see chapter '''Recording through Sahi'''
+
Or through Sahi Controller you can record your scripts. For this follow the instructions in this [http://sahi.co.in/w/file_download/5/SahiPro_V4_2_Documentation.pdf document] - see chapter '''Recording through Sahi'''
  
 
==Running test from Sahi controller==
 
==Running test from Sahi controller==
Line 216: Line 221:
 
<ol>
 
<ol>
 
<li>Create a '''demo.suite''' file with these three lines.  
 
<li>Create a '''demo.suite''' file with these three lines.  
<source>
+
<source lang="java">
 
demo.sah
 
demo.sah
 
demo.sah
 
demo.sah
Line 224: Line 229:
 
<li>Copy '''demo.suite''' in in '''C:/workspace/sahi_pro/userdata/scripts''' directory</li>
 
<li>Copy '''demo.suite''' in in '''C:/workspace/sahi_pro/userdata/scripts''' directory</li>
 
<li>Create a '''demo_suite.bat''' batch file with these lines wherever you want
 
<li>Create a '''demo_suite.bat''' batch file with these lines wherever you want
<source>
+
<source lang="java">
 
cd C:\Workspace\sahi_pro\userdata\bin
 
cd C:\Workspace\sahi_pro\userdata\bin
 
call testrunner.bat "demo.suite" "http://localhost:8080/ExampleContacts/" "firefox"  
 
call testrunner.bat "demo.suite" "http://localhost:8080/ExampleContacts/" "firefox"  
Line 235: Line 240:
  
 
==Troubleshooting ID Generator==
 
==Troubleshooting ID Generator==
As I write in the '''Introduction''' chapter, Sahi cannot require creating a Custom ID Generator. In your script you have to identify your element with '''_near''' and '''_in''' functions.  <br/>
+
As stated in the '''Introduction''', Sahi does not require creating a Custom ID Generator. <br/>In your script you have to identify your element with '''_near''' and '''_in''' functions.  <br/>
 
[[Image:Image10.png‎]]
 
[[Image:Image10.png‎]]
  
Line 252: Line 257:
  
 
Using '''_near''' or '''_in''' functions is a simple and powerful solution when DOM elements are not uniquely identifiable by themselves.
 
Using '''_near''' or '''_in''' functions is a simple and powerful solution when DOM elements are not uniquely identifiable by themselves.
 
 
  
 
=Summary=
 
=Summary=
The 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 very well this powerful documentation [http://sahi.co.in/w/file_download/5/SahiPro_V4_2_Documentation.pdf http://sahi.co.in/w/file_download/5/SahiPro_V4_2_Documentation.pdf]. <br/>Do not forget to read about the advanced way of automation tests via its Excel Framework and Ant scripts.  
+
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 http://sahi.co.in/w/file_download/5/SahiPro_V4_2_Documentation.pdf]. <br/>Do not forget to read about the advanced way of automation tests via its Excel Framework and Ant scripts.  
  
 
[[Category:Small Talk]]
 
[[Category:Small Talk]]

Latest revision as of 10:00, 23 October 2013

ZK Testing with Sahi

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


Introduction

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.

Environment

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

Steps

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.
    Image1.png

  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.
Image2.png

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.
Image3.png

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/
  images/
     add_contact.png
     edit_contact.png
     refresh_contacts.png
     remove_contact.png
  licenses
    COPYING
    COPYING.LESSER
    COPYING.zk_libraries
  META-INF/     
     MANIFEST.MF
  WEB-INF/
     classes/
       contacts/
          Contacts.class
          Contacts.java
          ContactsContainer.class
          ContactsContainer.java
          ContactsContainer.class
          ContactsContainer.java
          ViewCntroller$1.class
          ViewCntroller$2.class
          ViewCntroller$3.class
          ViewCntroller$4.class
          ViewController$Predicate.class
          ViewController.class
          ViewController.java
     lib/
       ZK CE 6.5.x libraries
     web.xml
     zk.xml
  edit.zul
  index.zul
  timeout.zul
  view.zul


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

Image6.png


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.

Example
Image7.png

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.
    demo.sah
    demo.sah
    demo.sah
    

    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.
Image10.png

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.

Summary

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.


Comments



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