Tuesday, October 20, 2009

Part 7: Beginners How to Start With??

If you are new to selenium and going through these posts you will be thinking Selenium RC is good and powerful and I am proficeient in my favourite programming language so I should start with Selenium RC. But the answer is NO, you should start with SElenium IDE to know about the selenium Commands and actions.

1. Download Selenium IDE using Firefox (Tools-->Add-ons)
2. After doing that click on Tools you will see Selenium IDE there.
3. Click on Selenium IDE
4. ByDefault selenium IDE with open with HTML as format.
5. If you want to chnage the format go to Options-->Format and choose the format you want.
6. Now click on File --> New Test Case whcih effectively create a new test case, click on the record button from the toolbar (right most)
7. Enter the base url of your application in the Base URL TextField. Do soem actions on the application under test and you can see that the selenium IDE records the action you do.
8. Now click on the "play current test case button" and verify that the test executed currectly.
9. We can view the log in the lower pane of the Selenium IDE window.
10. Now you can save the test and re-open and run it again.

(Seleniums wait methods wait for pre-defined milli seconds bydefault it will be "30000" you can change this by going to Options-->options-->Default value for recorded command)

More importantly give special attention to the middle pane of the selenium IDE window. Where we will see the Command, Target and Value

Command is the Selenium Command/Method to be executed
Target is on what field command should be executed (Target is nothing but locator)
Value is the input value of the field (for Text, DropDown, etc)

So to get good control over selenium you should check the auto help option from Command Filed" Type any character in the command Field Selenium IDE will list the commands starts with that character. Now select any of the listed command, If you look at the bottom pane we can see that the pane is changed to References and it displays the description the selected Command.

Work and play with the IDE commands and be ready by yourselves to move to the next level

Monday, October 19, 2009

Part 6: Running The Tests

Using Selenium IDE

• We can open the html tests and click on Run Tests button
• To run all the tests we have we need to create test suites. B default selenium looks for TestSuite.html, so we can either add all the html tests to TestSuite.html
•Or we can override the TestRunner url b replacing the TestSuite.html with our own test suite name Ex: http://localhost/TestRunner.html?test=AllTests.html

Using Selenium RC

•Running tests using selenium RC is up to the framework developer. The common steps to follow are,
1. Starting the selenium Server
2. Initializing the DefaultSelenium object with the application url, browser and other desired properties.
3. call the start method of selenium object (selenium.start())
4. Starting your test (Either we can make use of any other unit testing frameworks like junit, nunit, testNG etc or can use the programming languages technique but in this case you may loose the flexibility and advantages of a testing framework so nobody does that)

Part 5: Locating the Elements in a Web Page (Object Spying)

For testing our web application we need to enter the data and verify the outputs, how we are going to do this. In other words how we are going to identify the web components on the web page. Its known as spying the objects. For example if we need to do an action on a component, all we need is uniquely identify the component. If we have only a button in the page and it has a unique id or name then we are blessed. What if we have so many buttons and all have no id but the same name(Since all are doing the same action but in different situations so the developer decides to give the same name) These kind of situations we need to do something tricky which help us to identify each button uniquely

Selenium uses the following types to locate the objects on a web page.

1. ID or NAME Locator(The element with @id, @name attributes which is unique in the page.).

2. XPATH Locators.

(Xpath locators are beginning with “//” xpaths are useful when we don't have unique id or name in this case we ma need to use the combination of other attributes like alt, value, class, span etc..)

3. DOM Locators

(We can use the DOM model to traverse the document and locate the objects. The DOM locators must begin with document Ex: document.acme.forms.loginForm.username)

4. CSS Locators

CSS locators are like other XPATH locators but here we will make use of CSS attributes. When we started with IE as test browser I used to face so many problems with XPATH locators. The test execution was extremely slow and in few places the way IE translates the Pages was bit different from Firefox which I used to locate elements. So it will be much appreciated if you use CSS locators over XPATHs. It will increase the execution speed in IE and in AJAX rich applications it will reduce the chances of failures by dynamic object id generation


Example:
For example in our login page we need to test the following scenario. (Note that I will be using java code for samples)

1. Open the login page
2. Verify the login page opened
3. Type the username
4. Type the password
5. Click on the login button
6. Verify the login is successful

Here we need to locate 3 elements 1. Username Textfield 2. Password field 3. Login button. We can use the Selenium IDE to record teh actions there b figuring out the locators but if you are more experience ou can find out locators directly from the html source, DOM, or some locator tools.
Ok when we opened the html we could see that the following code

input id=username value =’Enter username here’ size=10, onKePres= “Do something”
input id=password value =’’ size=10, class= pwd
button id=loginButton value =’’ size=10 name= login

Here all the 32 elements have ids we can make use of it. Hence our code will be look like this

Selenium.type(“username”);
Selenium.type(“password”);
Selenium.click(“loginButton”);
Selenium.wait(“60000”);
The same code can use XPATH/DOM like this (Note that I am not showing the DOM or CSS definition for the page all I need is to show the usage or syntax)
Selenium.tpe(“//input[@value=’ ’Enter username here’]”); //XPATH
Selenium.tpe(“css=div[class=pwd]”); //CSS
Selenium.click(“document.LoginForm.loginButton”); //DOM
Tools which helps us to identify the attributes of a field
1. Firebug (Firefox pluggin) (search for firefox addons)
2. IE Developers Toolbar “http://chrispederick.com/work/webdeveloper/”
3. Xpath Checker (Firefox addon)

Part 4: How Does Selenium work

1.Selenium is developed by using JavaScript and implemented with browser technology.
2.Tests run directly in the browser.
3.Selenium deploys its own BrowserBot with our application for this selenium engine uses JavaScript and Iframes.
4.Since Selenium uses Javascript it works with an Java-Script enabled browser.

The above statements are the technical description of Selenium as A Tool but as a user you ma not need to dig in to how it works or how it developed and all. All you want is how it works for you.
1. Selenium loads our application b the specified url, the command use for this is selenium.open(url)
2.Selenium identifying the elements in the web page under test b using the element locators
3.Selenium does Actions on the web page under test by using the locators and its action methods (type, click, select, check..etc)
4.After the output generated or the resultant page loads (selenium waits for the pageload)Selenium does verifications using assert or verify methods.
5.Accessors (getter methods): After generating the out put we can get and store it.
6.We can refine our tests using other methods like storing values, waiting for a pageload etc. Doing some operations on the output values etc.

Make use of selenium's auto help from selenium IDE (type the method/Acton name’s first character and refer the description comes along with the method)or use the appropriate drivers documentation to learn more about the methods in selenium (If you are using selenium RC and an of the drivers you will get the DOC for the appropriate drivers.)

Part 3: Selenium Flavors (Components)

There are 3 types of Selenium Components available in now.

1. Selenium Core:

Selenium Core is the test execution framework for all selenium flavors.
Even though users can use Selenium Core as the testing tool but not encouraged
It is the engine of both, Selenium IDE and Selenium RC
Selenium core is written in DHTML.
Selenium developers it self says that the Selenium Core will be deprecated in recent future, hence I am not at all bothering about Selenium core in any of the topics.

2. Selenium IDE

Selenium IDE is a record and playback tool. Selenium IDE is a firfox extension so as I mentioned before this is its biggest limitation. So if you want to run our tests on different browsers you may need to go for Selenium RC. But we can run our tests which has been recorded b IDE in an browser using Selenium RC.
Selenium IDE provides auto complete for all the selenium commands.
Can save the recorded tests in to different supported formats (html, java, rub, etc..)
we can also set debug and break points.

3. Selenium RC (Remote Control)

Selenium Remote Control (RC) allows us to write the tests in any programming language against any HTTP website using any javascript-enabled browser.
Selenium RC has a server which automatically launches and kills browsers, and acts as a HTTP proxy for web requests.
Selenium Client Driver libraries for our favorite computer languages HTML, Java, C#, Perl, PHP, Python, and Ruby..
The RC server also bundles Selenium Core, and automatically loads it into the browser.

4. Selenium-Grid

Grid allows the Selenium-RC test suites that must be run in multiple environments and allows for running tests in parallel
Multiple instances of Selenium-RC are running on various operating systems and browser configurations, each of these when launching register with a selenium hub. When tests are sent to the hub they are then redirected to an available Selenium-RC, which will launch the browser and run the test. This allows for running tests in parallel.

Part 2: Why Not Selenium

When we discuss about a tool if we are not considering the cons then the discussion is not complete. Yes some of the advantages will come as disadvantages.
1. Open Source Tool difficult to get the right support.
2. Need to put great effort to design the right framework
3. Need the right knowledge to design and integrate different tools for the framework (Ex. You may need to integrate with Reporting tools, build, integration, other utils.
4. Record and Playback only available with FireFox browser
5. If you are planning to run our tests on multiple browsers then need special care for the object locators (We will discuss about this in the element locators section.)

Here I was not listing the selenium’s limitations to handle some situations in the application, instead I was listing the disadvantages as a testing tool. We may find some tricky situations where selenium is failing to handle our requests, in such situations its our responsibility to find out the solutions.

Tuesday, October 13, 2009

Part 1: Why Selenium

Its a simple question arise when you think about selecting your automation tool. The answer is pretty simple.

1. Its Open sourse!!
2. Its easy to learn and impliment..
3. Supports multiple browsers.
4. Supports multiple scripting/programming languages.
5. Flexible to your choice, simple record and playback (IDE), Comple Framework design to your choice by using your favorite programming language.
6. Supports parellel execution.

Automating the Web Application With Selenium.

I was thinking to share my knowledge about selenium from the day I started working with it. I like the tool with all of its strengths and limitations. Is an excellent tool currently available in the open source testing tools world to test your web application. If you are just in development phase of your project and you are planning to automate your tests using selenium just take a precaution from the development side; put a unique identifier for your javascript objects as much as possible...

In the following sections I will share my knowledge as much as possible, when I started with selenium 2 years back there where no such tutorials or tips were not available.. But now you can find out a lot of documents and tutorials available over Internet. The selenium document ion itself is pretty described and well documented now. I refereed the document recently after long time I surprised with the content and the excelled way of describing the features and techniques in fact one moment I thought of not to write this posts. Anyways it was a though which I had from past two years so y do I stop? OK I will continue with my decision...