My Blog List

Tuesday, April 26, 2011

Selenium with AutoIt - Example

Many web applications contain HTML forms which interact with the user. The user is able to enter data in to the form fields and submit the data to a server. Selenium is able to handle form inputs such as entering text in text fields, selecting radio buttons, selecting a value from a drop-down menu and so on. Things get a little tricky when dealing with input form fields of type “file”, to allow users to browse for a file and attach the file to the HTML form and submit the file.

So, what is the problem?
I can’t seem to use Selenium Core to upload a file; when I try to type in the file upload text field, nothing happens!

There seems to be two inter-related problems:

1 – Unfortunately, this is yet another JavaScript security restriction; JS is not allowed to modify the value of input type=”file” form fields. You can work around this by running your tests under Selenium IDE or under Selenium RC running in the experimental “*chrome” mode for Firefox, but at present there is no straight forward way to do this in Selenium Core.

2 – Handling of the “Choose File” dialog box with Selenium alone is not possible. We need to have another program running to select the path and file from the “Choose File” dialog box.

So, How can we upload files?
Fortunately, there exists a workaround to the above problems. This is where the magic of AutoIt and Selenium combination can work wonders!

First we will write a simple script in AutoIt and save the file as an executable: (please read documentation on AutoIt website to learn how to save the scripts as an executable file.

1WinWaitActive("Choose file")
2Send("C:\attach\samplefile.txt") \\location of the file you want to attach to the form and submit
3Send("{ENTER}")

We shall name the above file as attachFile.exe

Now, within a Java code, we can run a process which will execute the above program just before when we want to upload and submit a file.

01package com.company;
02
03import java.io.IOException;
04import com.thoughtworks.selenium.Selenium;
05
06public class AddAttachment {
07 public static void attach(Selenium selenium, String fileName) {
08 try {
09 String[] commands = new String[]{};
10 commands = new String[]{"c:\\test\\attachFile.exe"}; //location of the autoit executable
11 Runtime.getRuntime().exec(commands);
12 } catch (IOException e) {}
13
14 //autoit executable is now waiting for a "Choose file" dialog to popup
15 selenium.click("name=browseButton");
16 //once the "Choose file" dialog is opened, the autoit will input the path and file name
17 }
18}

The above seems to be the easiest way to deal with file uploads and attachments using Selenium.

Thanks for testing excellence guy....


Saturday, April 2, 2011

Selenium - Handling Windows


Testing web application is always fun and if you are automating your web application testing with Selenium, its even more fun. Unfortunately, like any other tool selenium has few limitations. One of the most annoying limitation of selenium is its inability to handle browser windows (Or any other expected or unexpected windows). When we automate any application which launches these windows (For example security warning windows, certifications windows and so on), we get stuck by the inability of selenium to handle them. I have seen people executing those automated tests but clicking on OK/Yes/Continue manually whenever these windows appear. This defies the whole purpose of automation, because tests can not be executed without human intervention.

Though it is not possible to solve this problem using Selenium (As far as I know), but we can certainly combine Selenium with other tools to solve it. One option is to use a small AutoIT script which will run in the background and wait for windows you are interested in. When that window / dialogue box appears AutoIT script will click on specific button or send required keystroke. If you are not familiar with AutoIT, here is the brief introduction from their site –

AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys).

You might have guessed that this is a windows only solution, but still its better than not having any solution at all :-).

Once you have installed AutoIT, actual script is very simple –

; Alters the method that is used to match window titles during search operations.
; = Match the title from the start (default)
; = Match any substring in the title
; = Exact title match
; = Advanced mode, see Window Titles & Text (Advanced)
;1 to -4 = force lower case match according to other type of match.
while(1)
Opt("WinTitleMatchMode","1")
;Pauses execution of the script until the requested window exists.
WinWait($CmdLine[1]);$CmdLine[1] is param 1 (after the script name)
$title = WinGetTitle($CmdLine[1]) ; retrives whole window title
WinActivate($title)
;Now click on Yes button to contibue running the script
ControlClick($title,"","&Yes")
;Send(“{ENTER}”)
WinClose($title)
Wend

When you launch this script from command line, it will continue to run in the background and whenever any window appears with the title you specified as parameter, it will perform the operation (Click on continue, yes, enter, esc etc as needed) you have specified.

Selenium - TestNG Framework

Those want to study and use Selenium with TestNG framework. this link will be more helpful...

http://testng.org/doc/documentation-main.html#test-results

Selenium Remote Control - Introduction


Selenium IDE have limitations in terms of language support (Only Selenese - InBuilt Selenium Commands) and browser support (Firefox only) . These limitations can be diminished by using Selenium Remote Control.

Selenium RC can be used for automating web applications for different web browsers on different platforms using your favorite language like JAVA, C#, Perl, Python, Ruby etc. You can use Selenium RC with or without Selenium IDE. Though, You might find it easier to start with Selenium IDE and than move to Selenium RC for customization and making your automation more robust.

Selenium RC is a java server which allows you to interact with the web application using any language. Your script ( If not hosted along with your application on the server) can not talk to your web application directly because of the security restrictions enforced by browser ( Same origin policy ). Using selenium server, you can bypass this restriction and have your automation scripts running against any web application. There are still some limitations, for example crossing domain boundaries, using it for https etc., but you can find solutions for most of them.

Selenium RC server is written in Java and in order to use Selenium RC you should have java runtime environment (newer than 1.5) installed on your machine. Assuming that you have downloaded and unzipped Selenium Remote Control from openqa.org and also have java runtime on your system, you can launch Selenium Remote Control server using following command ( Make sure that you are into directory where selenium-server.jar is located or this directory is in your path variable ) -

java -jar selenium-server.jar

This command should start Selenium Server on your machine and your automation scripts are ready to interact with your web application using this server. You can leave this server up and running while you are developing and executing your automation scripts. If you wish, you can also start Selenium Remote Control in interactive mode and start playing with its various commands on command prompt itself. In order to do that, you need to supply a command line option called interactive.

java -jar selenium-server.jar -interactive

There are many command line options that you can specify while starting selenium RC. Some of the interesting options are multiWindow , forcedBrowserMode, userExtensions, browserSessionReuse and many more.

For now lets move on to client drivers for Selenium, so that you can start using it. In the above paragraphs, we discussed that using Selenium RC you can automate your web application in most of the popular languages. This support is provided by the mechanism of client drivers. These client drivers provide the functionality to write automation script in any language and interaction with the Selenium Remote Control Server. Client drivers for most of the languages are already available and since it is open source you can create your own client driver if it is not available for your language. Details of extending existing client drivers or creating your own client driver can be found on openqa's website.

Once these client drivers are available, you can create a Selenium object in your automation script by specifying host name and port of the Selenium server. Using this Selenium object you can handle all the browser specific tasks for your web application. Some of the well known and popularly known client drivers are distributed along with the selenium RC itself. If you notice the directories where you unzipped the Selenium RC, you will find folders for various languages like Java, Perl, Python etc. Detailed instructions of using these client drivers are available on the respective section in openqa, we will summarize them here very briefly.

  • Java - selenium-java-client--driver.jar should be in the classpath, you can use this with JUnit or TestNG

  • .NET - add the ThoughtWorks.Selenium.Core.dll assembly as a reference to your VS project, use it with NUnit

  • Perl - Install Test::WWW::Selenium . You will need make (on unix) or nmake (on windows) in order to use it. You will also find it useful to use Test::more

  • Ruby - To use the Ruby Client Driver, just "require" or "load" selenium.rb in your Ruby script and create a new Selenium::SeleneseInterpreter object

Similarly, for any client driver steps are more or less same. i.e, install or use driver so that Selenium object can be created in your automation scripts which can interact with the browser.

If you have followed along, at this point you should have Selenium RC server up and running and you have also installed Selenium client drivers. You can now create your automation scripts and run them as you would run any program in the language of your choice. For example -

java YourAutomationClass
perl AutomationScript.pl
ruby AutomationScript.rb
And so on..

Though you can write your automation scripts using Selenium RC alone, we have found it very easy to start with Seleinum IDE and export test cases in specific language. Once you have basic test case, you can use language specific features to customize them and making them more robust.

Hope this document will help you all ! ! !