PMC:QA:Sahi integrate with Ant

From ADempiere ERP Wiki

Jump to: navigation, search

Test Environment:

  • firefox-3.0.6 broswer
  • jdk-1.6.11
  • ant-1.7.1

Contents

Ant Integration

Start and stop Sahi via Ant

Example:

Start Sahi via Ant

<target name="startsahi" description="start sahi proxy">
               <java classname="net.sf.sahi.Proxy" fork="true" spawn="true" dir="${sahi.home}">
                        <!--<env key="MOZ_NO_REMOTE" value="1"/>-->
                        <classpath location="${sahi.home}/lib/sahi.jar">
                                <fileset dir="${sahi.home}/extlib" includes="**/*.jar"/>
                        </classpath>
                        <arg value="${sahi.home}" id="basePath"/>
                        <arg value="${user.data}" id="userdataPath"/>
                </java>
        </target>

Stop Sahi via Ant

<target name="stopsahi" description="stop sahi server">
                <sahi stop="true" sahihost="localhost" sahiport="9999"/>
        </target>

run them by:

sahi # ant -f zkdemo.xml startsahi/stopsahi

Running tests in batch mode

  • Description

Runs a suite of sahi files. This opens multiple browser windows , executes sahi scripts, and logs the results. The build can be made to fail based on a failureproperty.

  • Parameters


Attribute Description Required
suite path to suite file
browser Path to browser executable
baseurl url relative to which all urls to be tested will be resolved
sahihost IP or hostname of server where sahi is running
sahiport port where sahi is running
failureproperty Name of property which will be set to false in case build fails.
haltonfailure Takes "true" or "false". Specifies if build should stop executing other tests if one test fails.
threads Number of simultaneous browser instances where sahi tests will be run. Do not use this property if using firefox. optional
logdir Directory to output this suite's results. If absent, uses the default playback logs dir. optional


suites

Once a few scripts have been recorded, scripted and refactored, they need to be run in a batch mode. Sahi does this via suites.

A suite file is a normal text file with an extension of .suite. For example you may have myapp.suite.

Each line of myapp.suite will have this syntax:

<testName.sah> <absolute url or url relative to the baseurl> 

eg.

test1.sah /startPageForTest1.jsp 
test2.sah http://www.d2.com/startPageForTest2.htm

In the above example, note that the first and third lines use relative urls and the second line uses an absolute url.

The suite can be executed through the command line or through ant.

Example:

in directory:/mnt/sda4/Sahi/sahi/userdata/scripts

create suit "my.suit",the content of it as follow:

test_forums.sah

and test_forums.sah

_setValue(_textbox("q"), "sahi forums");
_click(_submit("Google Search"));
_assertExists(_link("Sahi - Web Automation and Test Tool"));
_click(_link("Sahi - Web Automation and Test Tool"));
_assertExists(_span("Customer Stories and Testimonials"));

About Sahi scripting,please refer to:http://sahi.sourceforge.net/scripting.html

Using the commandline

The java class net.sf.sahi.test.TestRunner is used to run suites from the command line.

Usage:

java TestRunner <suite_name> <browser_executable> <start_url> <log_dir> <sahi_host> <sahi_port> <number_of_threads> 
<browser_process_name> [<browser_option>]

Set log_dir to “default” if you want to log to the default log dir."default" is sahi/userdata/logs/playback directory

browser_process_name is used to find and kill browser process. On windows set it to the exe; eg. firefox.exe or iexplore.exe; Note that this attribute was added in Sahi V2 and may not be needed for older Sahi versions.

Set browser_option to “-profile /browser/ff/profiles/sahi$threadNo” for Firefox.

For Firefox:

java -cp ..\lib\ant-sahi.jar net.sf.sahi.test.TestRunner D:/sahi/scripts/demo/demo.suite "C:\Program Files\Mozilla Firefox\firefox.exe" http://machine_name:10000/demo/ default localhost 9999 3 firefox.exe "-profile D:\sahi\browser\ff\profiles
\sahi$threadNo -no-remote" 


=test example

  • first start Sahi

you can start sahi by the follow way or run ant script.

bin # ./sahi.sh
  • Configuring the browser

you can configure:

 Firefox:
   * Go to Tools > Options > General > Connection Settings >
   * Set to "Manual Proxy Configuration"
   * Set "HTTP Proxy" to "localhost"
   * Set "Port" to "9999". (This setting can be modified through <sahi_root>/config/sahi.properties)
   * Do the same for "SSL Proxy" too, if you wish to record and playbacl https sites
   * Keep "Use the same proxy for all protocol" unchecked as Sahi does not understand protocols other than HTTP
   * NOTE: "No Proxy for" should NOT have localhost in it.

or configure via firefox plugin FoxyProxy,how to you can refer to **"Configuring Firefox to use Sahi's proxy" part in doc Sahi**

  • run test suit
workbox sahi # java -cp lib/ant-sahi.jar net.sf.sahi.test.TestRunner /mnt/sda4/Sahi/sahi/userdata/scripts/my.suite "/usr/bin
/firefox" http://www.google.com /mnt/sda4/Sahi/sahi/default localhost 9999 3 firefox

or

java -cp lib/ant-sahi.jar net.sf.sahi.test.TestRunner /mnt/sda4/Sahi/sahi/userdata/scripts/my.suite "/usr/bin/firefox" http://www.google.com /mnt/sda4/Sahi/sahi/default localhost 9999 3 firefox "-profile /mnt/sda4/Sahi/sahi/userdata/browser/ff/profiles/sahi0 -no-remote"

then if will output follows in console:

suiteName = /mnt/sda4/Sahi/sahi/userdata/scripts/my.suite
browser = /usr/bin/firefox
base = http://www.google.com
sahiHost = localhost
port = 9999
threads = 3
browserOption =
browserProcessName = firefox
Added shutdown hook.
http://localhost:9999/_s_/dyn/Suite_start?suite=%2Fmnt%2Fsda4%2FSahi%2Fsahi%2Fuserdata%2Fscripts%2Fmy.suite&base=http%3A%2F
%2Fwww.google.com&browser=%2Fusr%2Fbin%2Ffirefox&threads=3&browserProcessName=firefox&
sahisid=sahi_ac3e412802a05045b6085240682325b647cc&html=%2Fmnt%2Fsda4%2FSahi%2Fsahi%2Fdefault&browserOption=

and during this process,it will run my.suite(the script test_forums.sah).

base on http://www.google.com,search "sahi forums",then submit,click "Sahi - Web Automation and Test Tool" to link page sahi forums

the result:

Status:SUCCESS
Shutting down ...

and in the console of start sahi show as follow:

workbox bin # ./sahi.sh
...
>>>>>>                Tests size = 1
#### Running Script: /mnt/sda4/Sahi/sahi/userdata/scripts/test_forums.sah
/usr/bin/firefox http://sahi.example.com/_s_/dyn
/Player_auto?file=%2Fmnt%2Fsda4%2FSahi%2Fsahi%2Fuserdata%2Fscripts%2Ftest_forums.sah__SahiAmpersandSahi__startUrl=http%3A%2F
%2Fwww.google.com__SahiAmpersandSahi__sahisid=sahi_7af7ae8204768046850be3d00ef937196099sahix5df00e7b0e2b304b6d0b1bc00c0eed060ac8x
PIDs: [10616, 10653, 10655]; 10098 ms
Killing /mnt/sda4/Sahi/sahi/userdata/scripts/test_forums.sah
Kill: [10616, 10653, 10655]
Shutting down ...

log

we set log dir "/mnt/sda4/Sahi/sahi/default",you can goto it and view the log

image:saan.png

Using ant

The above command can be written as ant script.

Ant target for firefox:

<taskdef name="sahi" classname="net.sf.sahi.ant.RunSahiTask" classpath="lib/ant-sahi.jar"/> 
<target name="runfftests">  
<sahi suite="../scripts/demo/demo.suite" 
browser="C:\Program Files\Mozilla Firefox\firefox.exe" 
baseurl="http://machine_name/path/" 
sahihost="localhost" 
sahiport="9999" 
failureproperty="sahi.failed" 
haltonfailure="false" 
browseroption="-profile ${basedir}/browser/ff/profiles/sahi$threadNo -no-remote" 
browserprocessname="firefox.exe" 
threads="3" >  
</sahi>  
<antcall target="failsahi"/> 
</target> 

Example:

<target name="runfftests">
       <!--    <antcall target="startsahi"/>-->
                <sahi suite="${user.data}/scripts/my.suite"
                      browser="/usr/bin/firefox"
                      baseurl="${urlbase}"
                      sahihost="localhost"
                      sahiport="9999"
                      failureproperty="sahi.failed" 
                      haltonfailure="false"
                      browserProcessName="firefox"
                      threads="3">
                </sahi>
              <antcall target="failsahi"/> 
        </target>

Test

put the above code to zkdemo.xml file.

workbox sahi # ant -f zkdemo.xml
Buildfile: zkdemo.xml
runfftests:
     [sahi]
     [sahi] suiteName = /mnt/sda4/Sahi/sahi/userdata/scripts/my.suite
     [sahi] browser = /usr/bin/firefox
     [sahi] base = http://www.google.com/
     [sahi] sahiHost = localhost
     [sahi] port = 9999
     [sahi] threads = 3
     [sahi] browserOption = null
     [sahi] browserProcessName = firefox
     [sahi] Added shutdown hook.
     [sahi] http://localhost:9999/_s_/dyn/Suite_start?suite=%2Fmnt%2Fsda4%2FSahi%2Fsahi%2Fuserdata%2Fscripts%2Fmy.suite&base=http
%3A%2F%2Fwww.google.com%2F&browser=%2Fusr%2Fbin%2Ffirefox&threads=3&browserProcessName=firefox&
sahisid=sahi_81b7eada0ec06040580a2bf0075755660811&html=
     [sahi] STATUS:SUCCESS
failsahi:
BUILD SUCCESSFUL
Total time: 20 seconds
Shutting down ...

zkdemo.xml

whole scipts of zkdemo.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="bids" default="runfftests">
        <property environment="env"/>
        <property name="sahi.home" value="${env.SAHI_HOME}" location="/mnt/sda4/Sahi/sahi/" />
        <property name="user.data" value="${env.SAHI_USERDATA_DIR}" location="/mnt/sda4/Sahi/sahi/userdata"  />
        <property name="urlbase" value="http://www.google.com/"/>
        <taskdef name="sahi" classname="net.sf.sahi.ant.RunSahiTask" classpath="${sahi.home}/lib/ant-sahi.jar"/>

        <target name="runfftests">
        <antcall target="startsahi"/>
        <sleep seconds="4"/>
                <sahi suite="${user.data}/scripts/my.suite"
                      browser="/usr/bin/firefox"
                      baseurl="${urlbase}"
                      sahihost="localhost"
                      sahiport="9999"
                      failureproperty="sahi.failed"
                      haltonfailure="false"
                      browserProcessName="firefox"
                      threads="3">
                </sahi>
                <sleep seconds="4"/>
                <antcall target="stopsahi"/>
                <sleep seconds="4"/>
                <antcall target="failsahi"/>
        </target>
                <target name="failsahi" if="sahi.failed">
                <fail message="Sahi tests failed!"/>
        </target>
                <target name="startsahi" description="start sahi proxy">
                <java classname="net.sf.sahi.Proxy" fork="true" spawn="true" dir="${sahi.home}">
                        <!--<env key="MOZ_NO_REMOTE" value="1"/>-->
                        <classpath location="${sahi.home}/lib/sahi.jar">
                                <fileset dir="${sahi.home}/extlib" includes="**/*.jar"/>
                        </classpath>
                        <arg value="${sahi.home}" id="basePath"/>
                        <arg value="${user.data}" id="userdataPath"/>
                </java>
        </target>
        <target name="stopsahi" description="stop sahi server">
                <sahi stop="true" sahihost="localhost" sahiport="9999"/>
        </target>
</project>



Referrence

Links

Personal tools