sahi
TRANSCRIPT
Sahi Pro © Tyto Software Pvt. Ltd. 2
Contents
Sahi - Introduction .......................................................................................................... 5
Sahi Architecture ....................................................................................................................................................... 6
Sahi Advantages ............................................................................................................. 7
Browser Platform Independence ............................................................................................................................... 7
Smart Identification Mechanisms .............................................................................................................................. 7
UI Relations .............................................................................................................................................................. 7
Parallel Playback ....................................................................................................................................................... 7
Getting Started ................................................................................................................ 8
Prerequisites .............................................................................................................................................................. 8
Download Sahi Pro .................................................................................................................................................... 8
Installation of Sahi .................................................................................................................................................... 8
Starting Sahi ................................................................................................................. 13
Recording through Sahi ................................................................................................ 14
Playing back through Sahi ............................................................................................ 19
Running a test from the controller ........................................................................................................................... 19
View Logs ............................................................................................................................................................... 20
Creating a Suite File ..................................................................................................... 21
Running a test or suite from command line ............................................................................................................. 22
Running a test or suite using ant: ............................................................................................................................ 23
Sahi Pro Controller ....................................................................................................... 24
Sahi Pro © Tyto Software Pvt. Ltd. 3
Recorder Tab ........................................................................................................................................................... 24
The Anchor Button .................................................................................................................................................. 25
Example ............................................................................................................................................... 25
Using the Sahi Script Editor .................................................................................................................................... 26
Playback Tab ........................................................................................................................................................... 28
Running Tests on multiple machines ........................................................................... 29
Reports .................................................................................................................................................................... 30
Database Report ...................................................................................................................................................... 31
Sahi Pro Excel Framework ........................................................................................... 34
Sample Excel Sheet ................................................................................................................................................. 34
Loading Sahi script libraries .................................................................................................................................... 34
Executing the Excel Sheet ....................................................................................................................................... 35
Play back reports/logs ............................................................................................................................................. 35
IMPORTANT NOTES ............................................................................................................................ 36
Syntax ...................................................................................................................................................................... 37
Variables ............................................................................................................................................. 38
SetUp and TearDown .......................................................................................................................... 38
Creating Keywords on the fly in Excel ................................................................................................................... 40
Data driven testing .................................................................................................................................................. 41
Sahi Flex Support- sfl (Beta) ........................................................................................ 42
Introduction ............................................................................................................................................................. 42
Compile with sfl.swc using command line .............................................................................................................. 42
Compile using Adobe Flash Builder (Add SWC files to Flex Builder projects) ..................................................... 42
Recording a flex application .................................................................................................................................... 43
Appendix 1: Configure Sahi from the Dashboard........................................................ 44
Sahi Pro © Tyto Software Pvt. Ltd. 4
Appendix 2: Integrate with Jenkins ............................................................................. 45
Appendix 3: Configuring Sahi with Xvfb ................................................................... 48
What is xvfb? .......................................................................................................................................................... 48
Installing Xvfb: ....................................................................................................................................................... 48
Setting up Xvfb on display 1: .................................................................................................................................. 49
Running test in Xvfb headless browser through Sahi:............................................................................................. 49
Sahi Pro © Tyto Software Pvt. Ltd. 5
Sahi - Introduction
Sahi is an automation tool to test web applications. Sahi injects javascript into web pages using a proxy
and the javascript helps automate web applications.
Sahi is a tester friendly tool. It abstracts out most difficulties that testers face while automating web
applications. Some salient features include:
Excellent recorder
Platform and browser independence
No XPaths
No waits
Multithreaded (parallel) playback
Excellent Java interaction
Inbuilt reporting
Sahi Pro builds on Sahi Open Source to add significant time saving features. Sahi Pro adds the ability to:
Spread playback across multiple machines
Edit scripts and create functions right from the Controller
Build object repositories automatically while recording
Automatically accept SSL certificates
Automate your flex components with sfl - Sahi Flex (Beta)
Compare logs and screenshots to compare the outcomes of scripts run on different browsers
Customize your logs as XML, HTML or directly feed them to a database. Modify XSL style sheets
to get custom reporting according to your needs.
Use inbuilt Excel based Framework which lets non-technical users participate in automation
Sahi Pro © Tyto Software Pvt. Ltd. 6
Sahi Architecture
Sahi uses a HTTP proxy at its core to inject javascript into web pages. The injected Javascript uses
custom code to identify elements on the browser and simulate actions like click, type etc. on them.
Html responses which pass through the proxy are modified such that JavaScript is injected at the start
and the end of the response. This allows the browser to record and playback scripts and talk back to the
proxy when needed. Apart from handling requests for pages that the browser requests, Sahi’s proxy also
handles custom commands related to recording, playback etc. which the browser sends.
The architecture of Sahi allows Sahi to be used on any browser or operating system. Sahi relies on two
core technologies/concepts:
HTTP proxy – to inject code
Javascript code – to find elements and emulate actions
Both these technologies are basic building blocks of internet technologies and will necessarily be
supported by all browsers, making Sahi very easily extensible to newer browsers or newer versions of
browsers.
Sahi Pro © Tyto Software Pvt. Ltd. 7
Sahi Advantages
Sahi is an engineering solution for an engineering problem. We use heuristics based algorithms, fuzzy
logic, thresholds etc. to arrive at a solution which is 99.9% correct. Sahi also exposes the ability to be
exact when needed, making Sahi very suitable for any web application.
Browser Platform Independence Sahi, because of its architecture, supports any browser with javascript support and any Operating
System with Java support
Smart Identification Mechanisms The Javascript DOM exposes a lot of properties of various elements on the browser. Sahi uses these
different properties to identify elements. One significant point is that Sahi gives the visible text a higher
priority than other code related properties like css style. This helps in two ways
Writing and maintaining test scripts become very easy. What you see is what you script.
It validates what a user sees and not what a developer coded.
Sahi automatically goes through frames, iframes etc. to find elements. One does not need to explicitly
select a frame in the automation script. So if developers moved the login fields into an iframe, Sahi will
still work with your older scripts.
UI Relations Sahi is slightly different from other automation technologies. It relies less on the underlying code and
more on the business intention of the user interface. Code changes are frequent in an evolving
application and relying on business intention is a more stable way of writing automation test cases. Sahi
does this through what is called UI Relations. UI Relations help identify one element with respect to
another element which communicates strong business intention.
For example:
The cost of "Laptop" in a tabular structure may be represented in Sahi as
_cell(0, _near(_div("Laptop")), _under(_div("Cost")))
This represents the cell which is near "Laptop" and under "Cost". Note that even if there are more
columns or rows added in that grid structure, this always points to the correct cell. If we had used the
column index or row index, the test would break as soon as the location of Laptop or Cost changed.
Parallel Playback Sahi allows Parallel or Multithreaded playback. Multiple tests can be run simultaneously to reduce
playback time, thus reducing your build time and shortening your feedback cycle. One can also spread
the tests across machines or on the cloud and get one consolidated report, all stored in a database.
Sahi Pro © Tyto Software Pvt. Ltd. 8
Getting Started
Prerequisites Java 1.5 or above is needed for running Sahi.
Download Sahi Pro Download Sahi Pro from http://sahi.co.in/w/download-sahi-pro
Installation of Sahi Once Sahi Pro is downloaded, double click on the jar file to run the installer.
If that does not work, navigate to the folder on command prompt and run
java -jar install_sahi_pro_v36_yyyymmdd.jar
On starting the installer, follow the steps below:
Sahi Pro © Tyto Software Pvt. Ltd. 10
It is recommended to install Sahi on a folder without spaces in the path
Sahi Pro © Tyto Software Pvt. Ltd. 13
Starting Sahi Start Sahi Dashboard by any of the following methods:
1) Double click on the desktop shortcut
2) Go to Start -> All Programs -> Sahi -> Start Sahi
3) Start from the command line.
Windows: – Go to <SahiPro>\userdata\bin and run start_dashboard.bat
Linux – Go to <SahiPro>/userdata/bin and run start_dashboard.sh
The Sahi Dashboard starts the Sahi proxy, and allows launching of different browsers. Sahi automatically
modifies the browser’s proxy settings, so that requests go through the Sahi Proxy (localhost:9999)
If the browsers are not visible on Dashboard, look here for trouble shooting: http://sahi.co.in/w/trouble-shooting-sahi
Sahi
Configuration
Opens the
“Scripts” folder in
a file explorer.
HTML based
results of tests
Opens a terminal at
userdata/bin folder
HTML based
results stored in
database. Used
in distributed
Sahi Pro © Tyto Software Pvt. Ltd. 14
Recording through Sahi
Click on any browser on the Dashboard. A browser window should open with the following
screen
Sahi Pro © Tyto Software Pvt. Ltd. 15
Press ALT and double click on the window which you want to record. The Sahi Controller will
pop up. (If that does not work, press CTRL and ALT keys together and then double click. Make
sure popup blockers are turned off)
Sahi Pro © Tyto Software Pvt. Ltd. 16
On the controller, go to the Record tab.
Give a name for the script, and click ‘Record’. (.sah is optional)
Navigate on your website like you normally would. Most actions on the page will now get
recorded.
o The currently recorded step is visible in the “Evaluate Expression” box
o All recorded steps are visible in the “Recorded Steps” box
Sahi Pro © Tyto Software Pvt. Ltd. 17
Add an assertion:
o Move the mouse over any html element while pressing Ctrl key. The Accessor field will
get populated in the controller.
o Click the “Assert” button to generate assertions for the element. They will appear in the
“Evaluate Expression” box.
o Click “Test —>” to check that the assertions are true. You can evaluate any javascript
using “Evaluate Expression” and “Test —>”. Actions performed via the controller will not
be automatically recorded. Only actions performed directly on the page are
automatically recorded. This lets you experiment on the webpage at recording time
without impacting the script.
o Once satisfied, click on “Append to Script”. This will add the assertions to the script.
Click “Stop” to finish recording.
Press CTRL
key and
Hover
mouse
Sahi Pro © Tyto Software Pvt. Ltd. 18
Note that the controller can be closed and reopened at any time, without disrupting recording.
The recorded script is stored in <sahi_pro>\userdata\scripts directory. The recoded script can be viewed
and edited easily through any text editor. Sahi Scripts are simple text files which use Javascript syntax.
The script can be edited even while recording, so that logical segregation into functions etc. can be done
as recording happens.
Sahi Pro © Tyto Software Pvt. Ltd. 19
Playing back through Sahi
Running a test from the controller
Open the Sahi controller (ALT-Dbl click on the page).
Click on “Playback” tab
Enter the script name in the “File:” field (with the help of the auto completion feature)
Enter the start URL of the test. Eg. If you had started recording from
http://sahi.co.in/demo/training/, use that URL.
Click ‘Set’.
Wait for the page to reload.
Click ‘Play’.
Steps will start executing, and the controller will be updated accordingly. Once finished, SUCCESS or
FAILURE will be displayed at the end of the steps.
Note that the controller can be closed at any time, without disrupting playback.
Sahi Pro © Tyto Software Pvt. Ltd. 20
View Logs On the controller, go to Playback tab and click on “View Logs” link at the bottom right. It will open a
window with the results neatly formatted in HTML.
Clicking on a line in the logs will drill down to exact line in script. Logs show all the assertion in green. If
the assertion has failed it will show in red. You can click on any of these lines to go into the line of script
to debug. You can also view the logs at http://localhost:9999/logs
Sahi Pro © Tyto Software Pvt. Ltd. 21
Creating a Suite File Multiple scripts can be run in a batch using suites. A suite file is a normal text file with an extension of
.suite. It includes all the Sahi scripts that need to be executed as a batch
Suites can be run from a command prompt or through ant. They cannot be run from the Controller.
Suite logs show details of all scripts run
Sahi Pro © Tyto Software Pvt. Ltd. 22
Running a test or suite from command line The following command is used to run a test from a command line.
Command:
Eg.
cd sahi/userdata/bin testrunner.bat demo/demo.suite http://sahi.co.in/demo/ firefox testrunner.bat demo/sahi_demo.sah http://sahi.co.in/demo/ ie
One can directly click on the “Bin” link on the Dashboard to open a command prompt at usedata/bin
NOTE: Example scripts and suites can be found in SAHI_HOME/userdata/scripts/demo folder
testrunner.bat <sah file|suite file> <startURL> <browserType>
Sahi Pro © Tyto Software Pvt. Ltd. 23
Running a test or suite using ant:
Sahi tests and suites can be invoked from ant. Below is a snippet to run demo.suite through ant. The
paths and classpaths may need to be adjusted depending on where you call your ant file from.
<taskdef name="sahi" classname="net.sf.sahi.ant.RunSahiTask" classpath="lib/ant-sahi.jar"/> <target name="failsahi" if="sahi.failed"> <fail message="Sahi tests failed!"/> </target> <target name="runbrowsertests"> <sahi suite="../userdata/scripts/demo/demo.suite" browserType="firefox" baseurl="http://machine_name/path/" sahihost="localhost" sahiport="9999" failureproperty="sahi.failed" haltonfailure="false" threads="6"> <report type="html"/> <report type="junit" logdir="../userdata/temp/junit"/> </sahi> <antcall target="failsahi"/> </target>
This can be invoked as “ant browsertests”.
Parameters:
Attribute Description Required
suite path to suite or test file Yes
browserType The name of the browserType as specified in browser_types.xml Yes
baseurl url relative to which all urls to be tested will be resolved Yes
sahihost IP or hostname of server where Sahi is running Yes
sahiport port where Sahi is running Yes
failureproperty Name of property which will be set to false in case build fails. Yes
haltonfailure Takes "true" or "false". Specifies if build should stop executing other tests if one test fails.
Yes
singlesession Takes "true" or "false". If true, all scripts run sequentially in a single browser session without closing the browser. “threads” is ignored.
No
threads Number of simultaneous browser instances where sahi tests will be run. Works for IE, Firefox and Chrome
No
Nested Elements
report Specifies type and location of report – types can be “junit” “html” “tm6” No
Sahi Pro © Tyto Software Pvt. Ltd. 25
The Anchor Button
The Anchor button on the Recorder helps in identifying one element relative to another.
Usage:
1) CTRL - Mouse over Element1 on browser
2) Click the Anchor button
3) CTRL - Mouse over Element2 on browser.
4) Element2 will be resolved relative to Element1
Example
Suppose we need to identify the third textbox in the UI below
If we hover over the third textbox, it is identified as _textbox(“q*2+”)
Instead, we first CTRL-Hover over Python Cookbook; the Accessor field shows:
Then we click the Anchor button
Now if we CTRL-Hover over the textbox, we see that the Accessor now resolves the textbox in relation
with Python Cookbook.
Sahi Pro © Tyto Software Pvt. Ltd. 26
Using the Sahi Script Editor The Sahi script Editor is used to edit and Refactor Sahi scripts after recording. It allows conversion of
steps into parameterized functions and extraction of constants.
Select the steps you want to convert to a function and click “Create Function”. The Right hand panel will extract function parameters. Specify a function name, and choose the required parameters and constants. Click on “Continue” to create function code. If not satisfied, Click “Undo” to revert.
Sahi Pro © Tyto Software Pvt. Ltd. 28
Playback Tab
Enter the script
name
Enter the start
URL of the script Set the script for
playback
Recorder Tab
Playback Tab
Click to play the
script
Click for browser,
OS and java related
information
View played
back steps and
status
HTML based results
stored in database.
Used in distributed
runs
A copy of the test
script currently
loaded for play back
is displayed in the
web browser.
Displays the actual
code that will be
executed by Sahi
The browser
scripts included in
the Sahi script
Click to step
through the script,
one step at a time
Click to stop the
script
Resets the script
to play from 0 Click to pause the
script
Next step to
execute. Changing
this value, will
cause Sahi to skip
steps till this.
HTML based
results of tests
Sahi Pro © Tyto Software Pvt. Ltd. 29
Running Tests on multiple machines Sahi can distribute and run tests on multiple machines (nodes)
Configuration:
1) Sahi needs to be running on each machine
2) Browser paths need to be correctly specified in browser_types.xml on each node.
Refer http://sahi.co.in/w/browser-types-xml for more details on how to configure browser_types.xml
Once Sahi is started on all nodes, a suite can be run using this ant target:
<taskdef name="sahid" classname="in.co.sahi.ant.DAntRunner" classpath="lib/ant-sahi.jar" /> <target name="drun"> <tstamp> <format property="ts" pattern="yyyy_MM_dd_HH_mm_ss" locale="en, IN" /> </tstamp> <property name="tempdest" value="scritps/${ts}" /> <sahid suite="${tempdest}/demo/demo.suite" baseurl="http://${urlbase}/demo/" sahihost="localhost" sahiport="9999" failureproperty="sahi.failed" haltonfailure="false" browserType="firefox"> <sync originFolder="userdata/scripts" destFolder="${tempdest}" ignorePattern=".*(svn|copied).*" /> <node host="localhost" port="9999" /> <node host="machine1" port="9999" /> <node host="machine2" port="9999" /> </sahid> <antcall target="failsahi" /> </target>
sahid task which can distribute tests across multiple machines. <sync> copies the originFolder to destFolder. destFolder will be created in sahi_pro/userdata directory on every node. In this target we are dynamically creating a directory every time, based on the timestamp. <node> The nodes have to be specified using <node> tags. browserType specifies the type of browser to pick. This maps to the <name> attribute in browser_types.xml
NOTE: Refer to userdata/bin/drun.bat and drun.sh for running from command line
Sahi Pro © Tyto Software Pvt. Ltd. 30
Reports Reports are logged into the database or to the filesystem based on settings in sahi.properties or
userdata.properties
reports.filesystem.html.enabled=true reports.filesystem.xml.enabled=true reports.db.enabled=true reports.sql.enabled=true # Database logging. # Tables are created using sahi/user data/config/db/create_report_tables.sql db.driver_name=org.h2.Driver db.jdbc_url=jdbc:h2:$userDir/database/db0;DB_CLOSE_DELAY=-1 db.user_name=sa db.password= db.script_gen_xsl=$userDir/config/db/script_sql_gen.xsl
Database Reports for distributed runs can be accessed at http://localhost:9999/_s_/dyn/pro/DBReports This URL is available as a link at the bottom of Playback tab.
Sahi Pro © Tyto Software Pvt. Ltd. 33
Log comparison (via Compare Logs link)
HTML, SQL and XML logs are created in a time-stamped folder under
sahi_pro/userdata/logs/playback/<suiteLogFileName>
HTML logging converts result XML to HTML using XSL files in sahi_pro/userdata/config/reports.
Database logging is done by first converting XML to SQL using XSL files in sahi_pro/userdata/config/db
and then firing those queries against the database configured in sahi.properties or userdata.properties.
Database tables are created if required using sahi_pro/userdata/config/db/create_report_tables.sql
Sahi Pro is bundled with H2, a filesystem based small database. The database files are in
sahi_pro/userdata/database. The files db0.h2.db, db0.trace.db and db0.lock.db can be deleted to
recreate logs from scratch.
Sahi Pro © Tyto Software Pvt. Ltd. 34
Sahi Pro Excel Framework
Sahi Pro introduces an advanced way of automation via its Excel Framework. The Sahi Excel Framework
allows testers to write their test cases in Excel and run it from Sahi. Often a testing team consists of a
mix of subject matter experts, some manual testers and testers with some automation experience.
Writing tests in the language of the business allows all stake holders to participate and derive value out
of the automation process.
Sample Excel Sheet For example, a simple test written as an Excel sheet looks like this:
TestCase Key Word Argument1 Argument2 Argument3
loadSahi "books_lib.sah"
Check shopping cart total [Documentation] Smoke test for add books
login "test" "secret"
addBooks 3 2 1
verifyTotal 1640
logout
Test login error message [Documentation] Checks Invalid login message
login "test" "bad password"
verifyNotLoggedIn
verifyErrorMessage "Invalid username or password"
These tests talk mostly in the language of the business (also called a Domain Specific Language or DSL
for that business), and hide away all the implementation details of clicking buttons and populating
textboxes.
Loading Sahi script libraries The implementation details are moved into an included Sahi script, which is linked to this Excel sheet via
the initial
loadSahi "books_lib.sah"
statement.
The code in books_lib.sah is given below:
Sahi Pro © Tyto Software Pvt. Ltd. 35
function login($username, $password){ _setValue(_textbox("user"), $username); _setValue(_password("password"), $password); _click(_submit("Login")); } function addBooks($numJava, $numRuby, $numPython){ _setValue(_textbox("q", _near(_cell("Core Java"))), $numJava); _setValue(_textbox("q", _near(_cell("Ruby for Rails"))), $numRuby); _setValue(_textbox("q", _near(_cell("Python Cookbook"))), $numPython); _click(_button("Add")); } function verifyTotal($total){ _assertEqual($total, _textbox("total").value); } function logout(){ _click(_button("Logout")); } function verifyNotLoggedIn(){ _assertExists(_textbox("user")); } function verifyErrorMessage($msg){ _assert(_isVisible(_div("errorMessage"))); _assertEqual($msg, _getText(_div("errorMessage"))); }
Executing the Excel Sheet Executing the Excel sheet is no different from executing a Sahi script.
Just copy the Excel sheet into sahi/userdata/scripts directory (or inside a sub-directory) and the xls file
will be visible in the drop down in Controller Playback tab.
Choose the file, Click "Set" and then "Play" to playback.
Play back reports/logs On execution, Sahi generates logs showing success or failure. Logs are visible from the "Logs" link in
Playback tab.
Logs can also be accessed via http://localhost:9999/logs
A sample log is shown below. Clicking any step expands to show the underlying Sahi steps. You can click
on the log statements below.
Sahi Pro © Tyto Software Pvt. Ltd. 36
Starting script
Expand All | Collapse All
loadSahi "sample_lib.sah"
Check shopping cart total [Documentation] Smoke test for add books
login "test" "secret"
addBooks 3 2 1
verifyTotal 1640
logout
Test login error message [Documentation] Checks Invalid login message
login "test" "bad password"
verifyNotLoggedIn
verifyErrorMessage "Invalid username or password"
Stopping script
Please refer to sahi/userdata/scripts/demo/framework folder for some examples.
IMPORTANT NOTES
1. You may need to close the Excel sheet before running it 2. Only the first sheet will be executed. The first sheet should be called Sheet1 and should not be
renamed 3. MS Excel needs to be installed on the system 4. Due to a bug, numbers need to be formatted as text in Excel. One can do this by entering a
single quote before entering the number For example type '23 instead of 23.
Sahi Pro © Tyto Software Pvt. Ltd. 37
Syntax
Test Case
Key word Argument 1 Argument 2
Argument 3
Very first line of sheet
Blank lines are ignored
loadSahi "custom_lib.sah" Loads a Sahi script with required function definitions
Test Case One
Step One Param1 Param2 New test case started. StepOne(Param1, Param2) is called.
Step Two Param3 StepTwo(Param3) is called
Test Case Two
Step One 25 "age" New test case started. String values are quoted
Step Two Param5
Test Case Three
[Documentation] Some description about the test case
[Documentation] is useful for, well, documentation
Step One 25 "age" New test case started. String values are quoted
// Step Two Param5 Commented step using //
Step Two Param6
Spaces will be removed from keywords and corresponding functions invoked. The rules for writing the
Excel sheet are as follows
The first line should be populated with
Test Case | Key word | Argument 1 | Argument 2 | Argument 3
The names of the columns are not important, but they should not be left blank
If the first column is populated, a new test case is started.
The second column holds keywords. Keywords are mapped to functions in the included Sahi script.
They can be user defined functions or Sahi APIs themselves
Sahi Pro © Tyto Software Pvt. Ltd. 38
For example,
login "test" "secret"
in the excel sheet, maps to the javascript call
login("test", "secret");
_assertEqual _getText(_cell("msg")) "abcd"
becomes
_assertEqual(_getText(_cell("msg")), "abcd");
Variables
The Excel framework also supports variables, eg.
$userId=createUserInGroup "My name" "My group"
verifyUserCreated $userId "My name" "My group"
or
$msg= _getText(_cell("msg"))
_assertEqual $msg "abcd"
SetUp and TearDown
Different test cases may need the same steps to be executed before and after. For example, one may need to login before and logout after each test case. This can be accomplished through global SetUp and TearDown blocks. TearDown will be called inspite of any errors or failures in the testcase.
[Global] [SetUp]
_log "In Global Setup"
login "test" "secret"
[TearDown]
_click _button("Logout")
_log "In Global Teardown"
Verify books total addBooks 3 2 1
verifyTotal 1650
Verify books again addBooks 3 2 2
verifyTotal 2000
Sahi Pro © Tyto Software Pvt. Ltd. 39
This will execute as:
_log "In Global Setup"
login "test" "secret"
addBooks 3 2 1
verifyTotal 1650
_click _button("Logout")
_log "In Global Teardown"
_log "In Global Setup"
login "test" "secret"
addBooks 3 2 2
verifyTotal 2000
_click _button("Logout")
_log "In Global Teardown"
Sahi Pro © Tyto Software Pvt. Ltd. 40
Creating Keywords on the fly in Excel Keywords are normally created as functions in the included Sahi scripts, so that the details are hidden
away and one gets good programmatic control in the scripts.
But sometimes it may be convenient to club together different steps and create a keyword in the Excel
sheet itself.
Create AddBooksCheck Key Word
[CreateKeyword] Add Books Check
[CreateKeyword] command creates a keyword called "Add Books Check"
[Arguments] $java $ruby $python $total These are the arguments or parameters that "Add Books Check" takes as input
[Documentation] Data drivable add books check
addBooks $java $ruby $python
verifyTotal $total
So now, a call to "Add Books Check" like this:
Add Books Check 3 2 1 1650
is equivalent to
addBooks 3 2 1
verifyTotal 1650
Sahi Pro © Tyto Software Pvt. Ltd. 41
Data driven testing
Data Driven Example
[Keyword] Add Books Check Starts a testcase "Data Drivern Example" AddBooksCheck function will be called for the data set below.
[SetUp] This will be called BEFORE each row of data
login "test" "secret"
[TearDown] This will be called AFTER each row of data
_click _button("Logout")
[Documentation] java ruby python total Ignored by Sahi. Makes test case more readable
[Data] 3 2 1 1650 [Data] denotes start of data
4 5 0 2100
0 1 9 3350
This roughly translates to:
login "test" "secret"
Add Books Check 3 2 1 1650
_click _button("Logout")
login "test" "secret"
Add Books Check 4 5 0 2100
_click _button("Logout")
login "test" "secret"
Add Books Check 0 1 9 3350
_click _button("Logout")
Please refer to sahi/userdata/scripts/demo/framework folder for some examples.
Sahi Pro © Tyto Software Pvt. Ltd. 42
Sahi Flex Support- sfl (Beta)
Introduction Sahi Pro provides support for automation of flex applications. Before automating your flex application
you will first need to compile your swf with the correct version of sfl.swc
Eg. If your application is compiled with Flex 4.0, use sfl4.swc
Compile with sfl.swc using command line You can compile your flex application with sfl using the following command (Change sfl version as
needed)
mxmlc yourapp.mxml -include-libraries+=sfl4.swc --output=yourapp.swf
Compile using Adobe Flash Builder (Add SWC files to Flex Builder projects)
In Flex Builder, select your Flex project in the Navigator.
Select Project > Properties. The Properties dialog box appears.
Select Flex Compiler in the tree to the left. The Flex Compiler properties panel appears.
In the "Additional compiler arguments" field, enter the following command:
-include-libraries "sfl4.swc"
In Flex Builder, the entries in the include-libraries compiler option are relative to the Flex Builder
installation directory.
The default location of this directory on Windows is C:\Program Files\Adobe\Flex Builder.
After compilation refresh the browser cache, to make sure that the modified yourapp.swf is available.
Sahi Pro © Tyto Software Pvt. Ltd. 43
Recording a flex application
From the dashboard, open any browser and navigate to your flex application. Press ALT and double click on the window which you want to record. Sahi’s Controller window will pop up. You can now start recording your Flex application. NOTE:
Ctrl + hover (to get a flex element’s accessor) will work only if the Flex object is in focus. To do
this, you will have to first click on the Flex object.
Flex APIs are different from the normal JavaScript APIs.
Eg. _f("yuiswf0").textinput("username2")
Sahi Pro © Tyto Software Pvt. Ltd. 44
Appendix 1: Configure Sahi from the Dashboard
Various attributes of Sahi can be modified from the Dashboard by clicking the “Configure” link.
browser_types.xml: browser_types.xml configures the various browsers that Sahi can use. Note that
browser options for various browsers are different and it is advisable to use the defaults provided and
only change the paths where needed.
userdata.properties: Sahi can be configured using two files:
sahi/userdata/config/ userdata.properties
sahi/config/sahi.properties
Sahi first looks for a property in userdata.properties, and if not found, looks for it in sahi.properties. It is
recommended to modify/add properties to userdata.properties. Every property in sahi.properties can
be overridden in userdata.properties
Details of different properties are available as comments in sahi.properties
exclude_inject.txt: Sahi controls the browser by injecting javascript into web pages. However there are
various requests like XMLHttpRequests, javascript, css, etc. where Sahi should not inject its code. While
this is correctly detected and handled in most cases, there are instances where one may need to
explicitly ask Sahi not to inject code. In such instances, patterns can be added to exclude_inject.txt.
When a URL matches this pattern, Sahi will NOT inject its code.
download_urls.txt: Sahi automatically downloads and saves files into sahi/usedata/temp/downloads
directory. If Sahi is unable to detect a request as a download, you can force it to download the file by
adding URL patterns to this file.
download_contenttypes.txt: Sahi automatically downloads and saves files into
sahi/usedata/temp/downloads directory. If Sahi is unable to detect a request as a download, you can
force it to download the file by adding relevant content-types to this file.
block_ssl_domains.txt: If a domain is added here, any HTTPS calls to this domain will return a 404 error.
Browsers like firefox make HTTPS requests to addons.mozilla.org and sb-ssl.google.com which can hang
playback. Adding these domains to block_ssl_domains.txt will prevent hangs.
NOTE: Once changes are made, you will have to click on the “Save” button and restart Sahi for those
changes to take effect.
Sahi Pro © Tyto Software Pvt. Ltd. 45
Appendix 2: Integrate with Jenkins
Before integration, you will first need an Ant target to run the suite file.
<taskdef name="sahi" classname="net.sf.sahi.ant.RunSahiTask" classpath="lib/ant-sahi.jar"/>
<target name="failsahi" if="sahi.failed">
<fail message="Sahi tests failed!"/>
</target>
<target name="runbrowsertests">
<sahi suite="../userdata/scripts/demo/demo-chrome.suite"
browserType="firefox"
baseurl="http://machine_name/path/"
sahihost="localhost"
sahiport="9999"
failureproperty="sahi.failed"
haltonfailure="false"
threads="6">
<report type="html"/>
<report type="junit" logdir="../userdata/temp/junit"/>
</sahi>
<antcall target="report-gen"/> <!-- This target generates XML based reports for the suite -->
<antcall target="failsahi"/>
</target>
<target name="report-gen"> <delete dir="../userdata/temp/junit/reports/sahi-html"> </delete> <mkdir dir="../userdata /temp/junit/reports" /> <junitreport todir="../userdata /temp/junit/reports"> <fileset dir="../userdata /temp/junit/tests"> <include name="TEST-*.xml" /> </fileset> <report format="frames" todir="../userdata /temp/junit/reports/sahi-html" /> </junitreport> </target>
To run this Ant target from Jenkins, follow these steps
On the Jenkins dashboard, click on “New Job”
Enter a job name and select “Build a free-style software project”. Click OK.
Sahi Pro © Tyto Software Pvt. Ltd. 46
On the project configuration page add a build step with Ant as follows.
Save the configuration. On the Project’s home page left menu, click on “Build now” to run the
test.
Specify the path to the xml
report. Base root is Jenkins’
“workspace root”
Specify the Ant target
here.
Sahi Pro © Tyto Software Pvt. Ltd. 47
Once the test has been executed, you should be able to check the latest test results from the
project’s home page. Click on “Latest test Result” to view all the tests and their results.
Sahi Pro © Tyto Software Pvt. Ltd. 48
Appendix 3: Configuring Sahi with Xvfb
It is sometimes good to run tests headless (without a user interface). It is faster and allows the user to perform other tasks on the computer, while the tests are being run in background. On linux systems, Firefox can be configured to use Xvfb to run Sahi tests in headless mode.
What is xvfb?
From wikipedia (http://en.wikipedia.org/wiki/Xvfb): Xvfb or X virtual framebuffer is an X11 server that performs all graphical operations in memory, not showing any screen output. From the point of view of the client, it acts exactly like any other server, serving requests and sending events and errors as appropriate. However, no output is shown. This virtual server does not require the computer it is running on to even have a screen or any input device. Only a network layer is necessary.
Installing Xvfb:
For ubuntu, run apt-get install Xvfb
For Fedora, run yum install Xvfb
For other linux,
Download xvfb.tgz from http://ftp.xfree86.org/pub/XFree86/4.6.0/binaries/FreeBSD-4.x/
Let us consider that the downloaded file is at ~/Downloads.
cd /
tar xvf ~/Downloads/xvfb.tgz
cd /usr
sudo mkdir X11R6
sudo cp ~/Downloads/xvfb/*.* /usr/X11R6/
chmod +x bin/Xvfb
cd bin
Create a new file Xvfb and add the script below,
Sahi Pro © Tyto Software Pvt. Ltd. 49
#!/bin/sh mode=$1 case "$mode" in 'start') if [ -f /usr/X11R6/bin/Xvfb ]; then echo "***Starting up the Virtual Frame Buffer on Screen 1***" /usr/X11R6/bin/Xvfb :1 -screen 0 1152x900x8 & fi ;; *) echo " Usage: " echo " $0 start (start XVFB)" echo " $0 stop (stop XVFB - not supported)" exit 1 ;; esac exit 0
chmod +x Xvfb
Setting up Xvfb on display 1:
export DISPLAY=:1.0 firefox startx -- `which Xvfb` :1 -screen 0 1024x768x24
Running test in Xvfb headless browser through Sahi: Configure sahi/userdata/config/browser_types.xml to have browser entries as below,
<browserType>
<name>firefox-xvfb</name> <displayName>Firefox</displayName> <icon>firefox.png</icon> <path>env DISPLAY=:1 firefox</path> <options>-profile "$userDir/browser/ff/profiles/sahi$threadNo" -no-remote</options> <processName>firefox</processName> <capacity>5</capacity>
</browserType>
or Click on “Configure” link on Dashboard, and add the above entries before the ending </browserTypes> tag in browser_types.xml
Save and restart Sahi.
To run a test in Xvfb: navigate to sahi/userdata/bin on terminal and run the command:
testrunner.sh demo/sahi_demo.sah http://sahi.co.in/demo/ firefox-xvfb