Selenium - test tool for web applications

Selenium is an instrument for JavaScript web based applications automated testing.

From the beginning I was skeptical about Selenium and its possibilities. The first thing I thought when I was looking a presentation for it was – “Another tool with more restrictions than possibilities.” It was so till the day I tried it.

It really turned out that it is more than I expected. The project Selenium is divided into few subprojects.

I’m using only 2 of these projects - Selenium IDE and Selenium RC.

Selenium IDE is a plug-in for Firefox. The test scripts can be recorded quite easily with its help. Apart from recorder the plug-in can be also used to run tests themselves, as well as for their debugging (using the option for executing the test step by step). The tests are recorded in HTML and table’s elements are used for describing the test-related data.

Here is a small example for login form.

<html>
<body>
<table><tbody>

<tr>

<td>open</td>
<td> /test/</td>
<td></td>

</tr>
<tr>

<td>type</td>
<td>signInForm:Email</td>
<td>lyubomir.todorov@bianor.com</td>

</tr>
<tr>

<td>type</td>
<td>signInForm:Password</td>
<td>pass12</td>

</tr>
<tr>

<td>clickAndWait</td>
<td>signInForm:Sign</td>
<td></td>

</tr>
<tr>

<td>verifyTextPresent</td>
<td>Hello, L T </td>
<td></td>

</tr>

</tbody></table>
</body>
</html>

This way is indeed simple. The table consists of command, target and optional value. Besides you can export the test in your favorite programming language.
Java, C#, Perl, PHP, Ruby and Python are supported.

Here is the common part with Selenium RC. The already exported test can be easily performed by the interpreter of the corresponding programming language.
I use Java. This is how the previous example exported in JAVA looks like:

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;

public class NewTest extends SeleneseTestCase {


public void testNew() throws Exception {


selenium.open(”http://test/”);
selenium.waitForPageToLoad(”30000″);
selenium.type(”signInForm:Email”, “lyubomir.todorov@bianor.com”);
selenium.type(”signInForm:Password”, “pass12″);
selenium.click(”signInForm:Sign”);
selenium.waitForPageToLoad(”30000″);
verifyTrue(selenium.isTextPresent(”Hello, L T Sign Out”));

}

}

But let’s first tell something for Selenium RC. Its usage is indeed really simple. What you only need to do is to add selenium-java-client-driver.jar into class path and start the Selenium server: java – jar selenium-server.jar.

It is used as a proxy between your tests and the browsers, in order to execute the JavaScript commands against your web application. The advantage is that Selenium server detaches the test scripts from the actual code of the targeted application which makes no difference from testing perspective between production ready system and system under test.

For me it is advantageous that it can be easily integrated with the very well-known unit testing framework JUnit which provides a familiar environment for configuration and execution of the recorded tests so as a final result you have a homogeneous setup for all your types of tests including the ones created with Selenium IDE. .

Here is an example how our test will look like the following:

import junit.framework.*;
import com.thoughtworks.selenium.*;
import org.junit.*;

public class loginTest extends TestCase {

/**
* @throws java.lang.Exception
*/

public void setUp() {

private Selenium browser;
browser = new DefaultSelenium( “localhost” ,4444, “*firefox”, “http://test/”);
browser.start();

}

public void testAddItinerary() {


browser.open(”http://test/”);
browser.type(”signInForm:Email”, “lyubomir.todorov@bianor.com”);
browser.type(”signInForm:Password”, “pass12″);
browser.click(”signInForm:Sign”);
assertTrue(selenium.isTextPresent(”Hello, L T”));

}

public void tearDown() {
}

}

In SetUp method an instance of DefaultSelenium is created. In its constructor we indicate which browser we will use.

Let’s now have a look at the exported example from Selenium IDE. In it we don’t have such object so we use directly the object Selenium. In fact the object Selenium is created as a child of the class SeleneseTestCase and as a browser the OS default one is used. SeleneseTestCase inherits the TestCase class of Junit. The methods of the type verifyTrue are in fact the same as those in Junit:

public void verifyTrue(boolean b) {


try {


assertTrue(b);


} catch (AssertionFailedError e) {

verificationErrors.append(e);


}


}

However, it is preferable to define by ourselves the Selenium target and to use the methods of Junit or other unit test framework.

In conclusion I can say that Selenium is really useful tool for testing of web applications. The tests are recorded really fast and what I enjoyed a lot is they are easy for maintenance. You can execute them without any problems over the most wide-spread browsers. It’s perfect for smoke tests (I’m using it mainly for this purpose). You can make tests that only to go through all JSPs of the application (if it’s java based) in order to get away the boring waiting of the initial compilation when the server has to be restarted.

Try it, you may like it. It may work for you☺.

10 Responses to “Selenium - test tool for web applications”

  1. Siddharth says:

    I want to know about selenium tool how to get start with it what is core and RC? Does IDE required 1st? Our application is Dot Net will it support it

  2. lyubomir.todorov says:

    Dear Siddharth,

    You could benefit of comprehensive answers to your questions at the wiki site of Selenium here: http://wiki.openqa.org/display/SEL/Home.

    Selenium supports testing of .Net applications. Here are two good articles on that matter:
    1. an article for testing ASP .Net applications:
    http://www.stevetrefethen.com/blog/AutomatedTestingOfASPNETWebApplicationsUsingSelenium.aspx

    2. a good publication on testing SilverLight applications:
    http://www.hanselman.com/blog/UnitTestingSilverlightWithSelenium.aspx

    Do not hesitate to ask for any information you couldn’t find on the links provided.

    Best regards,
    Lyubomir

  3. siddu says:

    Thanks a lot for the info provided, dude i got clear difference between ide & rc also how to test homogeneously with junit+selenium ide

  4. Frank says:

    Hi Lyubomir,
    I dont’ get it !
    testing what ?
    I tested my website with HTML validator for exmple,
    So testing what ?

    I installed the Selenium IDE plugin for Firefox 3, but I don’t know how to operate it.

    Thank you very much
    Frank

  5. lyubomir.todorov says:

    Hi Frank,

    Selenium is functional automation tool.
    You can record and execute all your interactions with the application’s GUI.

    Please check this demo for getting started with Selenium IDE.
    http://wiki.openqa.org/download/attachments/400/Selenium+IDE.swf?version=1

    Best Regards,
    Lyubomir

  6. sasmita says:

    Hi
    I want to use this tool for .net application where some Java script is used.
    I am facing problem while i run saved script whrere java script is used and it display error message like(Locator is not found).

  7. lyubomir.todorov says:

    Please, check the links above, pointing to .net web application testing.
    Please, address your issue in the openqa.org user’s forum.
    Regrds,
    Lyubomir

  8. Tester says:

    Hello Sir

    I am new to Selenium, i have basic knowledge of java upto servlets,
    Currently i am working in small company as software engineer and got opportunity to use selenium ide to record and selenium RC to run the script using junit and eclipse in which i am sucessfull till now
    from last four days i am trying to parametarize the script for data driven test, but not able to do the same
    My homble request is please provide me the code for parametrizing the script, i will provide u the scenerio like

    Google search which i want to take the keyword to search form the excel sheet which contains 2 columns

    Please send me the script on my mail id as well as publish it in your blog
    Thanks

    Regards
    Tester123Tester
    tester123tester@gmail.com

  9. maria says:

    Hi,

    I exported the selenium test case into java and tried to use junit to run it. But I got “No tests found” error. I do have @Test put before my test case. What’s the problem? I have spent several hours on it, still can’t find a way to solve it, Can you please help?
    Here is my code.

    import org.junit.After;
    import org.junit.AfterClass;
    import org.junit.Before;
    import org.junit.BeforeClass;
    import org.junit.Test;
    import static org.junit.Assert.*;
    import com.thoughtworks.selenium.*;

    public class experimentPipeLine extends SeleneseTestCase {
    String baseUrl;
    protected Selenium selenium;
    public experimentPipeLine() {
    baseUrl = (System.getProperty(”base.url”)==null) ? getProperty( “hostUrl” ) : System.getProperty(”base.url”);
    }

    @BeforeClass
    public static void setUpClass() throws Exception {

    }

    @AfterClass
    public static void tearDownClass() throws Exception {

    }

    @Before
    @Override
    public void setUp() throws Exception{
    super.setUp();
    selenium = new DefaultSelenium(”localhost”, 4444, “*firefox”, baseUrl);
    selenium.start();
    }

    @After
    @Override
    public void tearDown() {

    selenium.stop();
    }

    @Test
    public void regular_95_Nofilter() throws Exception {
    selenium.setTimeout(”600000″);
    selenium.open(baseUrl);
    selenium.type(”user”, “xxx”);
    selenium.type(”pass”, “xxx”);
    selenium.click(”Login”);
    }
    }

  10. lyubomir.todorov says:

    Hi Maria,
    Try with using “test” prefix for the test methods names.
    So, your method should be testRegular_95_Nofilter, instead of regular_95_Nofilter.
    Best Regards,
    Lyubomir

Post Comment