If it ain’t broke, you are not trying hard enough.
One of the most important part of Web development or rather Software development in general is the (in)famous “Testing”, i.e confirming if the software works as expected.
In this post we will try and understand more on functional testing, specifically automated functional tests using Selenium IDE. Now, generally Software Testing can be categorized in many ways one of which is :
1. White box Testing :
White-box testing is testing that takes into account the internal mechanism of a system or component. In common terms this is a type of testing that involves the logic and consideration of the internal structure of the application. Notable example is Unit Testing. Others include Integration testing, Regression testing etc.
2. Black box Testing :
Black box testing is testing that ignores the internal mechanism of a system or component and focuses solely on the outputs generated in response to selected inputs and execution conditions. In other words, basically this type of testing considers the code as a black box, having no idea about the internals of the code, it only checks if the functionality is working. This is also called Functional Testing.
Functional Testing :
One type of testing that has always been applied and has been used ever since human being developed softwares is the black box “Functional testing”. This basically involves (often manually) confirming if the functionality works as expected.
Web developers often end up doing only functional testing in order to meet deadlines as other tests (although important) involve more efforts and even functional testing can get monotonous and uninteresting after a while.
For e.g Say you are working in some authentication related code which needs to be tested after every bit of code you change or add. You will need to check if the user can login. Ok, big deal you say, you can do that. You are right. Now, if I tell you need to test it 100 times with say 10 different users ? Or how about testing a form with 20 fields to check functionality 50 times ? Will you quit your job and settle down in the Himalayas ? I don’t think so.
Enter “Selenium IDE”, one of the ways to do “automated” functional testing, which you would agree is exciting, once you read this post.
What is Selenium IDE ?
Selenium IDE is one of the tools of the Selenium Suite which has two more tools which can be also used in isolation. But we focus only on the Selenium IDE for this post. Selenium IDE is implemented as a Firefox extension, and allows you to record, edit, and debug tests. Selenium IDE includes the entire Selenium Core, allowing you to easily and quickly record and play back tests in the actual environment that they will run. Yes, by record I mean you will be able to re-play the values you entered the clicks you made etc exactly as you did when you conducted the test. Its like “Test once, play multiple times”
Lets start off then :
Now, assuming you have Firefox (better settle down in Himalayas if you only use IE 😉 ).
1. Install the Firefox Selenium IDE plug in.
2. Restart Firefox.
3. Check installation by checking if the “Selenium IDE” menu item is available in the browser’s “Tools” menu.
Since this post is intended to just introduce you to Selenium IDE, lets use a simple form to check it out.
We are using PHP scripts here, however you can test this based on any language as long as it works from the browser.
Say you have a form you use for authentication with a user name and password field like this :
The Selenium IDE has already started recording, so in your login form enter the username and password correctly and hit “Login”.
In the case we consider that you get the following page after login, but your page can really be any page.
Now, for example the purpose of this is to test is to check if the user is logged in successfully and is redirected to a page with a “Welcome” text in it. We select the “Welcome” text and right click on it to get the context menu like this :
Here, we are using the “verifyTextPresent” option to verify if the text “Welcome” is present after the user enters the username / password and hits “Login”. Please see the list of other options in the Selenium IDE Documentation.
Ok, now we need to end this test, this can be done by pressing the Red button on the panel
Now you have successfully recorded your first test. Lets now execute it. You can adjust the speed of the execution of the recorded test by using the slider (Fast to Slow) just below the Base URL.
Now lets execute the test using the second button as shown :
And .. Voila! See the magic happen right in front of you .. wasn’t that Kewl !
After the test has ended you can check if it executed properly by checking if “verifyTextPresent” option is green and the log displays no error message in red. The test has indicated our application works as expected.
You can modify the test by editing the options in the “Table” tab, for example lets change the password in order to make the test fail and see what happens. Say we change the password to something else by clicking on the password item in the table tab, this will load the form below with the values, change the “Value” in it.
Now, execute the test again, now we get something like this, indicating one of the tests has failed.
You can save this test using the “File” -> “Save” and re-open later. By default it saves the test in the HTML format, for more options refer to the documentation. You can also make a test “Suite” and add different tests and execute them at once, to check if your web applications functionality is working as expected and can save a complete suite to be executed as and when you wish.
In this post we have just discussed just the basic usage of Selenium IDE. it offers much more however, you should now have a basic idea of using it, This can be more useful when you use it with the other tools in the Selenium suite. But thats all for now.
Until next time wish you happy coding and happier testing :).