This article explains some of the basics to get you up and running to automate your web applications with WatiN. Index
Download and install WatiN.
Using WatiN from a console application.
Using IE Developer Toolbar with WatiN.
Download and install WatiN
If you haven’t already done this, you should download the latest release of WatiN WatiN. Alternatively you could get the latest source from the Subversion repository (using TortoiseSVN) and build it yourself. After you downloaded the version run the installer or Unzip the download in a directory. Lets assume you used the msi installer, then WatiN will be installed in c:\Program Files\WatiN\<version>-<.net version> Using WatiN from a console application
Start your favourite .Net ide. In this example I will use Visual Studio 2003. Create a new console application named WatiNGettingStarted. Next, add a reference to WatiN.Core.dll by opening the Add Reference dialog and selecting the assembly from the WatiN installation directory.
Rename the file Class1.cs to WatinConsoleExample.cs and replace the existing code with the following code. This code will:
Open a new Internet Explorer window and go to the Google.com website (it might be redirected to a localised version).
Type the text WatiN in the search text field which can be uniquely identified in the html code by the name q.
Click the search button which can be uniquely identified by the text Google Search (this is the value attribute of the html input element).
using System;
using WatiN.Core;
namespaceWatiNGettingStarted
{
class WatiNConsoleExample
{
[STAThread]
static void Main(string[] args)
{
// Open a new Internet Explorer window and
// goto the google website.
IE ie = new IE(“http://www.google.com”);
// Find the search text field and type Watin in it.
ie.TextField(Find.ByName(“q”)).TypeText(“WatiN”);
// Click the Google search button.
ie.Button(Find.ByValue(“Google Search”)).Click();
// Uncomment the following line if you want to close
// Internet Explorer and the console window immediately.
//ie.Close();
}
}
}
Build and start the project and see Internet Explorer go to the Google website, search for WatiN and finally show the results webpage. Using IE Developer Toolbar with WatiN
Lets make this example a bit more interesting by writing the results statistics to the console window.
First we need to find out how we can get this information from the Google results webpage by using WatiN. Lets inspect the webpage with the Internet Explorer Developer toolbar (this is a must have if you want to do serious web automation!).
After opening the Dom Explorer with the View DOM button from the DevToolBar, select Select element by click in the Find menu and click on the results table cell on the webpage. The DOM Explorer then shows where the table cell is located in the DOM hierarchy (see screenshot).
Since the results table cell hasnt a unique identifier, like an Id or name, we cant get a reference to the table cell in an easy way. We need to use the hierarchy of the html elements in the page to get to the results table cell. First lets concentrate on finding the table the results table cell is located in. The table is the third table so we could use the Table collection to get a reference to this table. The code would look like this:
Table table = ie.Tables[2];
There is another way to get this same table, using the classname of this Table. Since no other table has the classname t assigned to it, this makes it unique to the table we are looking for. To find this table by its classname we will use the Find.By factory method like this:
Table table = ie.Table(Find.By("classname","t"));
At this point we have a reference to the table the results statistics are in. Next, find the table row and table cell the actual info is located in.
Finding the right table row is simple, there is only one row (<TR>) in this table. To get the results table cell we will use the TableCells collection. The text in the second table cell (<TD>) contains the info we are looking for. This results in the following code:
The last thing we have to do is write this info to the Console window. And here is the final code for this example.
using System;
using WatiN.Core;
namespaceWatiNGettingStarted
{
class WatiNConsoleExample
{
[STAThread]
static void Main(string[] args)
{
// Open an new Internet Explorer Window and
// goto the google website.
IE ie = new IE(“http://www.google.com”);
// Find the search text field and type Watin in it.
ie.TextField(Find.ByName(“q”)).TypeText(“WatiN”);
// Click the Google search button.
ie.Button(Find.ByValue(“Google Search”)).Click();
// Find the table cell which shows the search result statistics.
Table table = ie.Table(Find.By(“classname”,”t”));
string resultStats = table.TableRows[0].TableCells[1].Text;
// Write these statistics to the console window.
Console.WriteLine(resultStats);
// Uncomment the following line if you want to close
// Internet Explorer and the console window immediately.
//ie.Close();
}
}
}
Build and start the project and youll see something like this:
pywinauto is a set of python modules to automate the Microsoft Windows GUI. At it’s simplest it allows you to send mouse and keyboard actions to windows dialogs and controls.
A lot is done through attribute access (__getattr__) for each class. For example when you get the attribute of an Application or Dialog object it looks for a dialog or control (respectively).
myapp.Notepad # looks for a Window/Dialog of your app that has a title 'similar'
# to "Notepad"
myapp.PageSetup.OK # looks first for a dialog with a title like "PageSetup"
# then it looks for a control on that dialog with a title
# like "OK"
This attribute resolution is delayed (currently a hard coded amount of time) until it succeeds. So for example if you Select a menu option and then look for the resulting dialog e.g.
You can use opensource(ie free!) – Selenium tool ( selenium IDE is a plugin to firefox) to record and playback tests (like WinRunner, QTP). You can then export the recorded test in most language e.g. html, Java , .net , perl , ruby etc. The exported test can be run in any browser and any platform using “selenium remote control”.
Selenium IDE – Plugin to Firefox to record and play test in firefox
and also export tests in different languages. The most appealing
format is the html test case which seems to based on fit html .
Selenium RC- Allows playing of exported test in different platform/OS
Selenium Grid – Allows to control lots of selenium machines..
Other Project Management/wrapper tools around selenium:
Bromine(wrapper for selenium tests): http://bromine.openqa.org/ Warning: The above tool at this moment(Oct 2008) is very primitive – with little
documentation/flash demo. To uninstall bromine -
you may have to kill all process(e.g. mysqld etc) it started and then
delete the installation folder.
How does Selenium remote control internally works ?
Selenium RC launches the browser with itself as the proxy server (hence you may get certificate warning in some modes) and then it injects javascript – to play the test. This also means it can easily work in ALL browsers/platform – and it can be easily used to test AJAX(unlike professional tools).
What are the different modes that Selenium RC uses ?
With latest release of Selenium RC(1.0 beta 2) there
are two modes for IE *iexplore(same as *iehta) and *iexploreproxy.
Similarly for Firefox use *firefox(same as *chrome) and *firefoxproxy.
You should prefere *iexplore(ie *iehta) and *firefox (ie *chrome)
respectively as they work with cross domain.
Who should use it ?
Developers can use it – for “browser” regression testing ( and replace htmlunit/httpunit in some cases) .
Per the one of the forces behind selenium(Neal ford) – it should really be used by Business Analyst first .
QA should enhance/use it do regression test/cross browsers testing on all platforms .
Selenium IDE
In below screen shot – you can see that you need to do recording in main firefox window.
You can use selenium IDE to type any additional command that you may have missed(e.g. timeout)
In selenium IDE – you can open Selenium IDE Options – to change default timeout or
to add you own user extension to extend selenium.
Pre Requisite : Install Firefox Install Java jdk: Download jdk (not java SE) without Netbeans IDE or JEE. I am using jdk1.5(but it should work with jdk1.6)
Install Selenium IDE
After installation – you should see selenium ide option in firefox.
Install Selenium RC
After installation – go to directory containing file ‘selenium-server.jar’-
you will need to add appropriate bat/sh script as mentioned in
below steps.
Step a) Record your test case using Selenium IDE as shown in earlier flash demo.
For intial recording- avoid using “cross domain” testing (eg www.abc.com, www.def.com) and
preferably use http site only.(till you figure out the workarounds)
eg You can use my test case that opens this tutorial(it searches for selenium tutorial
on google and opens this tutorial):
<html>
<!--NOTE - Sometimes you may have wrong or blank base url - as seen on top of IDE-->
<link rel="selenium.base" href="http://www.google.com/" />
<title>New Test</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">New Test</td></tr>
</thead><tbody>
<tr>
<td>open</td>
<td>/</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>q</td>
<td>selenium tutorial</td>
</tr>
<tr>
<td>clickAndWait</td>
<td>btnG</td>
<td></td>
</tr>
<tr>
<!-- Note selenim ide noted this as click. you have to change it manually to clickAndWait-->
<td>clickAndWait</td>
<td>link=Selenium Tutorial for Beginner/Tips for Experts</td>
<td></td>
</tr>
</tbody></table>
</body>
</html>
Step b) Play your test case using selenium IDE itself to make sure
that your test case works.. Modify the recorded code if necessary(eg replace click by clickAndWait
or change timeout) as explained in Quirks section.
Step c) Export your recording test case – as html file
and/or optionally in programming languages like java, perl etc
Do I Export in HTML or do I export in programming language like java client ?
If you need dynamic capability (e.g. reading data from csv file or database) then
you have to export it in programming language like java.
You would also need to export in java – if your page has lots of flash
components. e.g. http://www.adobe.com/devnet/flash/articles/flash_selenium_04.html
There is also attempt to make silverlight work with java and .net out here.
The client code using java also is more stable (less permission denied errors) then
the html case.
But the html is easy to modify/maintain and it generates nice results – whereas in
java client code it tends to be cryptic(but again html test case – does not seem to be flexible).
I personally prefer the html option for the long term(if i don’t need dynamic capability)
Step d) Create a test suite
After you saved your html test case (eg save as a.html) – you need to create
a “Test Suite” (eg suite.html) – as you can NOT play test case directly.
Test suite is just a collection of test case which are defined/ordered using html table tags .
If you are adding another test case in the same test suite, then – initially – try to keep it to the
same – base URL(ie starting point)
- eg below
Note:
1) The first row (e.g Overall Tests) in your suite is always considered as Header
2) Selnium RC does not support a TestSuite where you go across domains
(ie the only domain that you would be allowed is for e.g. www.abc.com)
You may be able to work around it with *chrome for firefox and *iehta for IE.
Continue Next step?
If you are happy in running you test suite manually in Firefox only with Selenium
IDE – then you can skip below steps.(Its also the most stable – with no cross domain/security
warnings)
But If you wish to run you test suite in IE or if you want to schedule your
test suite bat file automatically via inbuilt Microsoft Scheduler on XP
or cron on Unix – then follow below steps.
Step e) Run your exported html test case in IE(or firefox) using selenium rc
run it in command line/bat file with below command :
java -jar selenium-server.jar -multiwindow -htmlSuite “*iexplore” “https://www.abc.com” “C:\suite.html” “C:\results.html”
REM Use *chrome for firefox and try *iexploreproxy
You can then see the nice results it generated and you can write a simple script to
email the results to yourself in case of failure(The result html file will contained “failed”
keyword).
You may also schedule your test suite with default/inbuilt Microsoft Scheduler on XP.
NOTE:
1) You may use -port option to change ports in above.
2) You may have problems in using *iehta (necessary for removing security warning
and going across domains) with above options..
3)Warning:Its better to use *chrome mode – which is more stable. Else- You are likely
to get weird permission denied error or some script error with this option – or
maybe it will ask you to download your -.do/.faces page.
(But note – I don’t seem to get similar problem with java client code ie
using the code that was exported to java)
In short firefox is more likely to be stable as compared with IE for selenium.
In rare cases – You may get some lock errors with firefox though.(in
which case you may try to create seperate profile eg http://seleniumhq.org/docs/05_selenium_rc.html#specifying-the-firefox-profile
or in worst case – reboot machine/reinstall it )
Continue Next step ?
If you want dynamic capability (eg reading from CSV file or reading from database) – then
you may want to export your test case in programming language like java etc.
else skip next section.
Step f) OPTIONAL – Run your exported test case in programming like java .
Once selenium rc is extracted/installed then go to the directory containing selenium-server.jar.
If you want to use selenium-rc using programming language like java/.net/php etc(most cases)
You may want to add a bat/sh script to keep selenium rc always running..
REM set PATH variable to contain location of firefox/IE binary
REM set PATH=%PATH%;C:\firefox/bin
java -jar selenium-server.jar -multiwindow
REM -interactive is more user friendly – but does not work with websites using non frameable pages
REM – optionally use -port , if you want to run more then 1 instance
Now compile your selenium java client code with junit jar files , use selenium
java client driver, point to above selenium RC server and run it.
Assuming you exported the test case with name “NewTest.java” – you
can try to use below bat file(after getting the junit*jar and selelenium-java-client.jar – and creating clases directory in same folder)
——— eg. put below in compileJava.bat —————————-
REM Get junit*.jar (got from junit.org)
REM Get selenium-java-client-driver.jar
REM (got from selenum rc installation/selenium-java-client-driver* folder)
REM NOTE: classes directory should already exist in current directory
set CLIENT_JAR=.
set CLASSPATH=%CLASSPATH%;.;.\classes;;%CLIENT_JAR%\junit-4.4.jar;%CLIENT_JAR%\selenium-java-client-driver.jar
REM COMPILE(note default package at first line is com.example.tests)
javac -d classes NewTest.java
REM ensure that selenim rc server is already running on current machine
REM Now running the actual test.
java junit.textui.TestRunner com.example.tests.NewTest
REM - remove pause if you are going to schedule it.
pause;
——————————————————————-
This implies that java(or programming) code is dependent on
selenium rc server to be running on same(or other) machine. But the “html test case” – launches the “selenium rc server” every time.(hence you need different port – if you run html test cases in parallel)
TIP:
By default selenium java client does not give nice reports(seen with html option)
You may use loggingselenium” project to get the nice reports(along with timing).
Also if you use this, it will also embed the screenshot that you called- in the html file.
Best way is to run/edit- after right click/saving this attached code as the usage on their website is incomplete/confusing.
It depends on loggingselenium and apache commons jar.
Do check the reference manual here to get concepts of selenium locators
and the various commands that are available. You could get condensed version of the manual here.
Without reading the manual – You may easily miss useful commands like pause(to test session timeout), storeHtmlSource (you can potentially apply selenium regular expression to verify the values of hidden variables insides that html) , typeKeys (to do things like page up etc)
Assert vs verify: If you use seleniums verify command – then the test suite/case – will still run – and fail at the end. Hence in most cases you should use assert command which will cause the test to stop.
Sometimes selenium IDE may use “click” instead of “clickAndWait” for some
“submit” button cases resulting in your next page’s assert/verify to fail. Hence
you should change it manually or add waitForPageToLoad with some timeout.
If the recording is not working – then go to firefox and click on “Tools->Add on -> Selenium IDE ” .First disable it and then enable it and restart the browser. Sometimes clicking on Selenium IDEs
Options->HTML Format helps.
If web site does not allow itself to be frameable then use -multiwidow option(instead of -interactive) with selenium rc.
To eliminate certificate warning(and work cross domains ) use chrome/iehta modes -
which is now default(as compared with rc beta1 version)
http://selenium-rc.openqa.org/experimental.html
If you check selenium rc options eg “java -jar selenium-server.jar -h”
then you will find that it has a “timeout” option. But that timeout is for the overall
run(and not for individual “clickAndWait” ) . Hence you may be advised to use “setTimeOut” via your
java program or in case of HTML use -Selenium IDE open test case-> Add Insert new command
-> setTimeout with value(not Target) of say 240000 (ms)
Selneium has this cool – captureEntirePageScreenShot command which works
well with IDE/HTML . Consider using it at critical points(to debug
issues, if you schedule your test cases).
But with programming language like java – it may not work(try
using the experimental – captureScreenshot .
Consider writing a wrapper for your TestCase/TestSuite in .net/jsp
so that you can maintain your testcase/test suite in HTML.
Sometimes the browsers or the java process may not get killed after the test is run.. So you may need to manually
kill it(you can try to use some automated tools like Pskill on Windows ).
When your client java test (using RC) finishes with error – then it does not give clear error -
you just the line number of the failed assert in stack trace – Hence you may need to trap all exceptions and – print the last body test
If you are testing with page where the id dynamically changes – then You may need to use lots of xpath in your test . In that case install firebug and use “Inspect element” by right clicking on the element -> Copy XPath by right click on bottom-> Add “/” in front of value you got in your clipboard-> paste this as a “target” in Selenium IDE for your “clickAndWait” command. You can check the validility by – click on find button in Selenium IDE to ensure that your XPath is correct(assuming that page is open) . You may find more tip on xpath using firebug/xpather here
If you are using Selenium HTML test case – then one way to remove hard coded values
in your selenium test is to have simple JSP(or asp/php) that converts CSV file to HTML file.
Then you just need to invoke that JSP page (for particular CSV) – and call various “store” values
and replace hard coded values in your test with “get” values .
That way you can just change your test/data by just replacing the CSV file.
The latest selenium rc(1.0 beta 2) does support Firefox 3..
With firefox 3 you can easily add your local https server with invalid certificate in safe list(to remove warning message).
Its difficult to do that with Firefox 2.
If you are running selenium rc test with IE a site which already has invalid certificate -
then IE will give a warning for “invalid certificate”. You can have selenium
automatically click on “Continue” link in it(which has ID of overridelink) by adding
below command in your IE specific test case.
<tr>
<td>clickAndWait</td><td>//a[@id='overridelink'] </td>
<td></td>
</tr>
With firefox 3 – you can permanently store the exception. However selenium -rc starts
new firefox profile every time(probably to get around lock issues) – hence you may
need to start firefox with same/particular profile.
You may also consider changing IE options to be strict (eg Tool->Internet Options->Advanced->Display
Notification on every script error) to have selenium catch any javascript issues/bugs in your website.
Consider use of UI-Element – which is now supported in RC here
if you read above FAQ – you will realize selenium has limitation in IE with certain dialog box(eg upload file, basic auth). You can get around some of the limitation with autoit script
from this site.
Its advisable not to directly download the exe from above site – but rather install autoit, compile autoit script – from one of it program menus options – to Exe and than use the compiled exe.
For whatever reasons – the above scripts(or autoit) does not help in Firefox dialog boxes(but you dont need it anyway as in chrome mode – selenium automatically gets rids of dialog boxes)
However if you are trying to get rid of basic authentication dialog – you can directly pass user/pass in URL.(however for IE you would need to add some registery settings)PS: Autoit and autohotkey seem to have same codebase – and both have primitive recorder.
(else we probably wont have needed selenium)
NOTE: On windows – you should use ‘start’ in front of exe/.au3 file to make it run in background -
and then run your selenium rc test in the same batch file(eg start autotItscript.au3)