Sahi
Sahi
Sahi
6
Web Automation and Testing Tool User Manual
Contents
Sahi - Introduction .......................................................................................................... 5
Sahi Architecture ....................................................................................................................................................... 6
Starting Sahi ................................................................................................................. 13 Recording through Sahi ................................................................................................ 14 Playing back through Sahi............................................................................................ 19
Running a test from the controller ........................................................................................................................... 19 View Logs ............................................................................................................................................................... 20
Example............................................................................................................................................... 25
Using the Sahi Script Editor .................................................................................................................................... 26 Playback Tab ........................................................................................................................................................... 28
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
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
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, Sahis 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
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.
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
Getting Started
Prerequisites
Java 1.5 or above is needed for running Sahi.
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
Sahi Pro
Sahi Pro
10
Sahi Pro
11
Sahi Pro
12
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 browsers proxy settings, so that requests go through the Sahi Proxy (localhost:9999)
Sahi Configuration
HTML based results stored in database. Used in distributed If the browsers are not visible on Dashboard, look here for trouble shooting: http://sahi.co.in/w/trouble-shooting-sahi Sahi Pro Tyto Software Pvt. Ltd. 13
Sahi Pro
14
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
15
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
Sahi Pro
16
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 o
Click the Assert button to generate assertions for the element. They will appear in the Evaluate Expression box. 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. Once satisfied, click on Append to Script. This will add the assertions to the script.
Sahi Pro
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
18
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
19
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
20
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
21
Eg.
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
Sahi Pro
22
This can be invoked as ant browsertests. Parameters: Attribute suite browserType baseurl sahihost sahiport failureproperty haltonfailure singlesession threads Description path to suite or test file The name of the browserType as specified in browser_types.xml url relative to which all urls to be tested will be resolved IP or hostname of server where Sahi is running port where Sahi is running Name of property which will be set to false in case build fails. Takes "true" or "false". Specifies if build should stop executing other tests if one test fails. Takes "true" or "false". If true, all scripts run sequentially in a single browser session without closing the browser. threads is ignored. Number of simultaneous browser instances where sahi tests will be run. Works for IE, Firefox and Chrome Required Yes Yes Yes Yes Yes Yes Yes No No
Nested Elements report Sahi Pro Specifies type and location of report types can be junit html tm6 Tyto Software Pvt. Ltd. No 23
Sahi Pro
24
Usage: 1) 2) 3) 4) CTRL - Mouse over Element1 on browser Click the Anchor button CTRL - Mouse over Element2 on browser. 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:
Now if we CTRL-Hover over the textbox, we see that the Accessor now resolves the textbox in relation with Python Cookbook.
Sahi Pro
25
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
26
Sahi Pro
27
Playback Tab
Click for browser, OS and java related information
Playback Tab
Recorder Tab
Next step to execute. Changing this value, will cause Sahi to skip steps till this.
Click to stop the script View played back steps and status
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 HTML based results of tests HTML based results stored in database. Used in distributed runs
Sahi Pro
28
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. 29
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
30
Database Report
Sahi Pro
31
Suite Report
Script Report
Sahi Pro
32
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. 33
Checks Invalid login message "test" "bad password" "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.
The code in books_lib.sah is given below: Sahi Pro Tyto Software Pvt. Ltd. 34
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"))); }
Sahi Pro
35
Starting script Expand All | Collapse All loadSahi Check shopping cart total [Documentation] login addBooks verifyTotal logout Test login error message [Documentation] login verifyNotLoggedIn verifyErrorMessage "Invalid username or password" Stopping script Checks Invalid login message "test" "bad password" "sample_lib.sah" Smoke test for add books "test" 3 1640 "secret" 2 1
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
36
Syntax
Test Case Key word Argument 1 Argument Argument 2 3 Very first line of sheet Blank lines are ignored Loads a Sahi script with required function definitions Param2 New test case started. StepOne(Param1, Param2) is called. StepTwo(Param3) is called New test case started. String values are quoted
loadSahi
"custom_lib.sah"
Step One
Param1
Param3 25 "age"
Step Two
Test [Documentation] Some Case description Three about the test case Step One 25 // Step Two Step Two Param5 Param6
"age"
New test case started. String values are quoted Commented step using //
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
37
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= _assertEqual 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 login _getText(_cell("msg")) $msg "abcd"
"secret"
[TearDown] _click _button("Logout") _log "In Global Teardown" Verify books total addBooks verifyTotal 3 1650 3 2000 2 1
38
This will execute as: _log login addBooks verifyTotal _click _log "In Global Setup" "test" "secret" 3 2 1 1650 _button("Logout") "In Global Teardown"
"In Global Setup" "test" "secret" 3 2 2 2000 _button("Logout") "In Global Teardown"
Sahi Pro
39
Create [CreateKeyword] Add AddBooksCheck Books Key Word Check [Arguments] $java
[CreateKeyword] command creates a keyword called "Add Books Check" 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
Sahi Pro
40
login "test" "secret" Add Books Check 3 2 1 _click _button("Logout") login "test" "secret" Add Books Check 4 5 0 _click _button("Logout") login "test" "secret" Add Books Check 0 1 9 _click _button("Logout")
1650
2100
3350
Sahi Pro
41
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
42
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. Sahis Controller window will pop up. You can now start recording your Flex application. NOTE:
Ctrl + hover (to get a flex elements 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
43
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
44
On the Jenkins dashboard, click on New Job Enter a job name and select Build a free-style software project. Click OK.
Sahi Pro
45
On the project configuration page add a build step with Ant as follows.
Specify the path to the xml report. Base root is Jenkins workspace root
Save the configuration. On the Projects home page left menu, click on Build now to run the test.
Sahi Pro
46
Once the test has been executed, you should be able to check the latest test results from the projects home page. Click on Latest test Result to view all the tests and their results.
Sahi Pro
47
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
48
#!/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
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
Sahi Pro
49