Monday, April 27, 2009

Compare two files line by line

If your are looking for comparing two files(may be any CSV file) and wanted to find out which liens are common or unique to files. See below script:


Sunday, April 19, 2009

Flex automation testing with QTP

QTP can be used to test the Flex application. You need to perform following steps to setup recording a flex application with qtp.

  1. Install QTP 9.2
  2. Install Internet Explorer
  3. Install flash player debug version for IE
  4. Install Flex Builder 3
  5. Install QTP plugin of flex from FLEX_BUILDER3/Installers/QTP_Plugin_Installer
  6. Compile your flex application with automation libraries. It can be done in two ways; either use -include-libraries parameter in Flex Builder3 or use mxmlc ant task. This step is must otherwise QTP will not recognize the flex application
  7. To record a test in QTP, you must compile the application’s SWF file and generate the HTML wrapper files. The main wrapper file defines the application as an object on the page and embeds it so that Flash Player is invoked when the page is requested. You cannot request the application’s SWF file directly. You can write the HTML wrapper yourself, or generate it with the compiler.
  8. Start QTP
  9. QTP wil show the all plugins on startup, select the flex plugin and continue
  10. Start recording for your flex application.

Build FLEX application with automation libraries using Flex3 SDK and ANT

To test Flex Application with any automated testing tool (like QTP), automation libraries should be included in the application SWF file to make the application testable. Flex Automation libraries are not part of the Flex SDK, these comes with Flex Builder 3. If you are using Flex SDK and ANT as build tool, you need to perform following steps to compile your application.

  1. Copy flex automation libraries (automation.swc, automation_agent.swc, automation_dmv.swc, automation_flashflexkit.swc) from Flex Build 3 installation (/frameworks/libs) to your local directory (let say C:\flex-automation).
  2. Copy automation tool library (qtp.swc in case if you are using QTP as testing tool) in C:\flex-automation
  3. Copy flex agents (automation_agent_rb.swc, automation_rb.swc) in C:/flex-automation/locale/en_US. Flex agent facilitates communication between a flex application and an automation tool.
  4. Copy FLEX_HOME/frameworks/flex-config.xml to flex-config-auto.xml and keep it in same directory (FLEX_HOME/frameworks)
  5. Add following entries in include-libraries section of flex-config-auto.xml
  6. <include-libraries>
    <library>C:/flex-automation/automation.swc</library>
    <library>C:/flex-automation/automation_agent.swc</library>
    <library>C:/flex-automation/automation_dmv.swc</library>
    <library>C:/flex-automation/automation_flashflexkit.swc</library>
    <library>C:/flex-automation/qtp.swc</library>
    </include-libraries>
  7. In mxmlc task of ant build file specify load-config path as
  8. <load-config filename="FLEX_HOME/frameworks/flex-config-auto.xml"/>
  9. Add compiler-library-path in mxmlc task
  10. <compiler.library-path dir="C:/flex-automation" append="true">
    <include name="locale/en_US" />
    </compiler.library-path>
so your mxmlc task would look like this:

<mxmlc file="${APP_ROOT}/Main.mxml" output="${build.dir}/main.swf">
<load-config filename="${FLEX_HOME}/frameworks/flex-config-auto.xml"/>
<source-path path-element="${FLEX_HOME}/frameworks"/>
<compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">
<include name="libs" />
<include name="locale/en_US" />
</compiler.library-path>
<compiler.library-path dir="${FLEX_AUTOMATION_HOME}" append="true">
<include name="locale/en_US" />
</compiler.library-path>
</mxmlc>

mxmlc ANT task comes with flexbuilder3 and flex sdk. In flex builder, the location of task is : _FLEX_BUILDER_INSTALL_LOCATION_\sdks\3.0.0\ant\flexTasks.jar

To use this ant task in ant build file use ant taskdef task as below:
< taskdef resource="flexTasks.tasks" classpath="_PATH_TO_TASK_/flexTasks.jar"/>

Wednesday, April 15, 2009

JMeter: run tests using ANT

Do you run your test cases in jmeter GUI, i really never like to run my tests in it. I always like to have command line options or preferably ANT. Following are the steps to run any JMeter test cases using ANT:

1. Install ANT and JMeter
2. Copy ant-jmeter.jar ant ANT_HOME/lib directory.
3. Create a common.xml file (as mentioned below) which can be referred by any Jmeter Test plan.
4. Create a build.xml for every test plan, which refers to common.xml
5. Use xslt task to create the html report of the test cases.

common.xml

<project name="jmeter-test-common" default="run" basedir=".">

<property name="jmeter.dir" value="C:/Jmeter"/>
<taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/>

<target name="run-test">

<echo message="${jmeter.dir}"/>
<jmeter jmeterhome="${jmeter.dir}"
testplan="${test.dir}/${jmeter.file}.jmx"
resultlog="${test.dir}/${jmeter.file}.jtl">
<property name="jmeter.save.saveservice.output_format" value="xml"/>
<property name="jmeter.save.saveservice.assertion_results" value="all"/>
<property name="request.threads" value="${request.threads}"/>
<property name="request.loop" value="${request.loop}"/>
<jvmarg value="-Duser.dir=${test.dir}"/>
</jmeter>

<echo message="Generating Report ..."/>

<xslt in="${test.dir}/${jmeter.file}.jtl"
out="${test.dir}/${jmeter.file}_summary.html"
style="${jmeter.dir}/extras/jmeter-results-report_21.xsl"/>

<xslt in="${test.dir}/${jmeter.file}.jtl"
out="${test.dir}/${jmeter.file}_detailed.html"
style="${jmeter.dir}/extras/jmeter-results-detail-report_21.xsl"/>
</target>
</project>


build.xml


<project name="my-test" default="run" basedir=".">

<import file="${ant.file.my-test}/common.xml"/>

<target name="run">
<antcall target="run-test">
<param name="jmeter.file" value="LOGIN"/>
<!-- here LOGIN is the jmx file name -->
<param name="test.dir" value="${ant.file}/.."/>
<param name="request.threads" value="5"/>
<param name="request.loop" value="1"/>
</antcall>
</target>

</project>


JMeter: Using Variables

In JMeter value of user.dir varaible is set to jmeter/bin directory. Jmeter saves and loads the test script from user.dir, so by default it is jmeter/bin. But what if your test scripts are saved in directory other than jmeter/bin which is the case most of the time.
We can override the value of this varaible in jmeter/bin/jmeter.properties as mentioned below:
user.dir=C:/perftest/mytest
Now the jmeter wil look for test script in C:/perftest/mytest instead of its default location. We can also specify this varaibale as jvm arg; if we are running jmeter from command prompt. (-Duser.dir=C:/perftest/mytest)

Accessing user.dir value in Jmeter test plan
In Test Plan -> User Defined Variables add a new variable
Name: userdir
Value: ${__property(user.dir)}

Now this userdir varaible can be used in jmeter test plan as location of the test script (${userdir}).

Reading values from a file
${__CSVRead(${userdir}/env.txt,0)} -> It will read the first column of the first line from the env.txt file

__P
This is a property function which is intended for use with properties defined on the command line. Unlike the __property function, there is no option to save the value in a variable, and if no default value is supplied, it is assumed to be 1.
${__P(request.threads)} -> return the value of request.threads
${__P(request.loop)} -> return the value of request.loops
${__javaScript(${__P(request.threads)}*2)} -> multiply the value of request.threads by 2

Use JMeter to load test FLEX Applications

Flex applications use a binary protocol called AMF (Action Message Format) that they use to remotely communicate with the server. The easiest way to use JMeter with a Flex application is to use HTTP proxy server.

- Create a New Test Plan
- Right Click on Test Plan, Add -> Thread Group
- Right Click on Thread Group, Add -> Config Element -> HTTP Request Defaults
- Select 'HTTP Request Defaults'
- Specify Server Name or IP, for eg. www.mysite.com
- Specify Port number, eg. 8888
- Specify protocol, i.e. http
- Specify Path, foe eg. /main/index.html
- Select Work Bench
- Right Click on Work bench, Add -> Non-Test Elements -> HTTP Proxy Server
- Select 'HTTP Proxy Server'
- Specify port number for proxy (default is 8080)
- Set Target Controller to 'Test Plan -> Thread Group'
- Set inlude URL patterns , .* to include all
- Set exclude URL patterns, for eg. , .*\.gif, .*\.png, .*\.css, .*\.js
- Open Internet Explorer
- Goto Tools -> Internet Options -> Connections -> LAN settings
- In Proxy Server panel, specify address as 'localhost' and port number (which is specified in
meter's HTTP proxy server, i.e. 8080)

- Select 'HTTP Proxy Server'
- Click on Start
- Open IE and open URL 'http://www.mysite.com:8888/main/index.html'
- As soon as you visit the URL jmeter starts the recording, and you can see all the requests in
METER

- For AMF requests (for Adobe FLEX), request is saved as file with extension '.binary' in the bin
irectory of jmeter

- Click on Stop button of 'HTTP Proxy Server' to stop the recording.

Test Plan is ready. You can modify it now according to your requirements.

Proxy Server setup document from Apache:
http://jakarta.apache.org/jmeter/usermanual/jmeter_proxy_step_by_step.pdf