May 18 2008

Automate Website Testing with Selenium RC

Tag:Tag , , , , alvinsingh @ 23:59

One of the difficulties I face when we do a release is regression testing - specifically testing the front-end (website) to ensure everything is working/looking correct. At the moment we have a team of 2 testers who work almost feverishly when a release is getting close to ensure that it is indeed a good release. To address this issue I have been looking at Selenium and how we can use it to automate a large portion of this regression testing.

I come from a Java background where we use Continuous Integration so I wanted to integrate Selenium testing as part of our build cycle. Selenium comes in a number of forms - IDE, Core, RC and Grid (some of which work together). In this article I will be focusing on Selenium RC - to find what the others are about head over to the Selenium website.

Since the website I work on utilizes AJAX in quite a number of places, one of the requirements I had for Selenium was that it had to be able to deal with AJAX enabled sites. Although the Selenium website doesn’t specifically talk about it, I did find help regarding the subject in their user-forums.

Below is a short write-up on the steps I took to get some basic tests running via Selenium RC. Note that you should download the Selenium RC package to get the java client libs and the Selenium server - available here.

1. Selenium Test Case

First I setup a JUnit 4 abstract class to setup selenium before my actual test class runs. You can see below in my AbstractSeleniumTest class in the setUp method I construct a DefaultSelenium object by passing in parameter values obtained from the System - this is so that I can test different environments (local dev, integration, uat etc.) configured at runtime. Also to note are the use of the new JUnit4 annotations.


public abstract class AbstractSeleniumTest {

	protected static Selenium selenium;

	protected static String SELENIUM_SERVER_HOST="selenium.server.host";
	protected static String SELENIUM_SERVER_PORT="selenium.server.port";
	protected static String SELENIUM_BROWSER_STARTCOMMAND="selenium.browser.startCommand";
	protected static String SELENIUM_BROWSER_URL="selenium.browser.url";

    @BeforeClass
    public static void setUp() {
        selenium = new DefaultSelenium(System.getProperty(SELENIUM_SERVER_HOST),
        							   Integer.parseInt(System.getProperty(SELENIUM_SERVER_PORT)),
        							   System.getProperty(SELENIUM_BROWSER_STARTCOMMAND),
        							   System.getProperty(SELENIUM_BROWSER_URL));
        selenium.start();
    }

    @AfterClass
    public static void tearDown() {
        selenium.stop();
    }

    public void waitForElement(final String waitingElement, String timeoutMessage) {
        new Wait() {
            public boolean until() {
                return selenium.isElementPresent(waitingElement);
            }
        }.wait(timeoutMessage);
    }
}

Now my class which will test an AJAX’ified login box.


public class AuthenticationTests extends AbstractSeleniunTest {

    @Test
    public void testUserLogin() throws InterruptedException {
        selenium.open("/");
        selenium.click("link=Login");
        waitForElement("login-form_j_username", "Login not shown");
        selenium.type("login-form_j_username", "xxxx@xxxx.com");
        selenium.type("login-form_j_password", "xxxx");
        selenium.click("login-form_0");
        waitForElement("user-home-link", "User home link not shown");
        assertTrue(selenium.isElementPresent("user-home-link"));
    }
}

2. Setting up Selenium Server

Selenium server needs to be setup on the box on which you plan to run the tests from and this box should also have some web browsers installed. I use a mac so I am testing under firefox (note - that firefox3 beta does not seem to work yet so ensure you have firefox2 ).

Once you have extracted the Selenium RC download, run the server as below.
[sourcecode language='bash']
computer:selenium-server-1.0-beta-1 alvins$ pwd
~/selenium-remote-control-1.0-beta-1/selenium-server-1.0-beta-1
computer:selenium-server-1.0-beta-1 alvins$ java -jar selenium-server.jar -interactive
alvin-singhs-computer:selenium-server-1.0-beta-1 alvins$ java -jar selenium-server.jar -interactive
23:18:15.624 INFO - Java: Apple Inc. 1.5.0_13-119
23:18:15.625 INFO - OS: Mac OS X 10.5.2 i386
23:18:15.628 INFO - v1.0-beta-1 [2201], with Core v1.0-beta-1 [1994]
23:18:15.724 INFO - Version Jetty/5.1.x
23:18:15.725 INFO - Started HttpContext[/,/]
23:18:15.726 INFO - Started HttpContext[/selenium-server,/selenium-server]
23:18:15.727 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
23:18:15.732 INFO - Started SocketListener on 0.0.0.0:4444
23:18:15.732 INFO - Started org.mortbay.jetty.Server@2a5330
Entering interactive mode… type Selenium commands here (e.g: cmd=open&1=http://www.yahoo.com)
[/sourcecode]
Note that if you are in a linux headless environment you must have X installed and run Xvfb before running the above.
[sourcecode language='bash']
computer:selenium-server-1.0-beta-1 alvins$ Xvfb :1
computer:selenium-server-1.0-beta-1 alvins$ export DISPLAY=:1
[/sourcecode]

3. Running the Test

I use eclipse for development and so to run the test, right click on the Test class–>Run As–>Open Run Dialog–>Arguments Tab as below. The arguements are quite self explanatory - they are fed into the DefaultSelenium class as can be seen in the code from Step 1. The third argument deserves a mention - this is the browser you wish to run your tests on - some supported browsers include iexplore, konqueror, firefox, safari and opera.



Once you are all set, simply click Run and what should happen is that the test will contact the selenium server, start firefox and start executing the tests. Of course if you are running in a headless environment you will see firefox launch but running in interactive mode you should still be able to see the selenium server logging its activity.

[sourcecode language='bash']
23:24:14.037 INFO - Checking Resource aliases
23:24:14.042 INFO - Command request: getNewBrowserSession[*chrome, http://xxxx.com] on session null
23:24:14.042 INFO - creating new remote session
23:24:14.118 INFO - Allocated session 15a43743285c4dba913a3ff0ab2b5dea for http://xxxx.com, launching…
23:24:14.160 INFO - Preparing Firefox profile…
23:24:18.520 INFO - Launching Firefox…
23:24:20.819 INFO - Got result: OK,15a43743285c4dba913a3ff0ab2b5dea on session 15a43743285c4dba913a3ff0ab2b5dea
23:24:20.830 INFO - Command request: open[/, ] on session 15a43743285c4dba913a3ff0ab2b5dea

[/sourcecode]

Depending on your build environment you will have to decide how you want to integrate these tests as part of your build. The Selenium Wiki has some great examples on different ways to do this. My company uses Bamboo (CI Server) from the great people at Atlassian and this runs the tests after successful builds.

Happy testing!

BlogMemes del.icio.us Digg DZone Facebook FeedMeLinks Google Google Reader Ask.com Yahoo! MyWeb Newsvine reddit Simpy SlashDot StumbleUpon Technorati

19 risposte a “Automate Website Testing with Selenium RC”

  1. Richard Wilkinson ha scritto:

    If you use TestNg you get access to BeforeSuite and AfterSuite methods (i’m not sure if JUnit 4 has this, JUnit 3 didnt) which as expected run before and after your test suite. This allows you to start up your selenium server too as part of your test suite, all from java code.

    private SeleniumServer server = null;

    @BeforeSuite
    public void beforeSuite() throws Exception
    {
    System.out.println(”Before Suite”);
    server = new SeleniumServer();
    server.start();
    }

    @AfterSuite
    protected void afterSuite() throws Exception
    {
    System.out.println(”After Suite”);
    server.stop();
    }

  2. tieTYT ha scritto:

    I wish you answered this question: What is Selenium RC?

  3. alvinsingh ha scritto:

    Selenium RC (Remote Control) is basically an API into Selenium. You can control the browser via programming your commands in whatever language - in my case Java.

  4. Kiran ha scritto:

    I’m new to selenium.I want to implement this tool for my project.
    I have recorded a script using Selenium IDE and saved the script in Java Script and now i want to run this usiong selenium RC, but don’t know how to do this can you please help me in this.

    I have downloaded the selenium RC and installed JRE too in my machine and also worked on the examples commands given in the tutorial at http://selenium-rc.openqa.org/tutorial.html.
    Please help me it’s urgent. Thanks in advance
    Kiran

  5. alvinsingh ha scritto:

    In the Selenium IDE there as an option to show you Java generated code. Once you have Selenuim RC working - you can use Selenium IDE to create your tests, let it generate the Java code and modify as necessary.

  6. Kiran ha scritto:

    Hi,

    I want to work with Selenium RC can you tell me how to start with in detail. Plese don’t tell me to refer to this http://selenium-rc.openqa.org/tutorial.html tutorial. I have already gone through this in this they have given hoe to run the commands.

    My problem is I have a script to login to the application which is written in Java which is stored at location D:\path.
    Now I want to run this through the selenium RC. If you help me in this it would be a great help. Please give me in detail…

    Thanks,
    Kiran.

  7. Java Script Website ha scritto:

    Good site I \”Stumbledupon\” it today and gave it a stumble for you.. looking forward to seeing what else you have..later

  8. Kevin Gann ha scritto:

    I have an application which implements a flow from page to page with elements on each page I’d like to test. Do you have any suggestions on how to lay these out in Java code? I’d like to build up a library of page to page methods.

  9. Cai ha scritto:

    Hi there! =)
    I have the same problem with Kiran. I want to run a selenium test case ( java selenium test generated by the selenium ide ) but am not quite sure how to do it. Please help. Thanks.

  10. alvinsingh ha scritto:

    Kevin,

    There is some work going on at the moment on getting pieces in place for Selenium 2.0. One of those pieces is the integration of the webdriver project (http://code.google.com/p/webdriver/) by a very talented guy named Simon Stewart. One of the patterns that Simon introduces is of the PageFactory pattern - http://code.google.com/p/webdriver/wiki/PageFactory. I have written some tests with this pattern and I must say it is most excellent. I think you could emulate this pattern to some degree in the current version of Selenium with a little ’scaffolding’.

    Thanks,
    Alvin

  11. Kevin Gann ha scritto:

    Thanks for the advice. I’ll check out that pattern. I recently had problems with getting Selenium RC working in a headless configuration on our Linux CI server (as you show in your post). I figured out how to start and stop it via the Ant build script, but using SeleniumServer.start() and .stop() methods seem to hang and the client can never connect. Thanks again for your expertise. :)

  12. ticos ha scritto:

    Looking through the comments I see 2 other inquiries like the one I’m about to submit:
    I’ve recorded a script with IDE, exported it as “Java” by giving it a .java extension. I can open the file, I’ve added a loop so I could create a bunch of users. Saved. I think what others and I want to know is: how do we run this file with Selenium RC. What is the syntax? Or is it more complicated than that?
    Thanks in advance.

  13. Chetan ha scritto:

    My problem is that i cant run from eclipse , it opens the run dialog but what are the further steps to be followed to run the test class

  14. alvinsingh ha scritto:

    Ok - I will give some more detailed on info on the exact steps to get it up and Selenium RC and Eclipse running in my next post. This will be part of my follow up to “What’s wrong with current web testing approaches” (my latest post).

  15. shyam ha scritto:

    I have three problems.I am using junit + loggingselenium + eclipse. 1)I cant able to run the script in internet explorer. 2)I need to build the project so that i need to run in command prompt, so can you help me how to build the project. 3)I cant able to create junit-testsuite for loggingselenium, so i need a idea how could i log the testsuite report with screenshots

  16. jagdish ha scritto:

    I’m new to selenium.I want to implement this tool for my project.
    I have recorded a script using Selenium IDE and saved the script in Java Script and now i want to run this usiong selenium RC, but don’t know how to do this can you please help me in this.

    I have downloaded the selenium RC and installed JRE too in my machine and also worked on the examples commands given in the tutorial at http://selenium-rc.openqa.org/tutorial.html.
    Please help me it’s urgent. Thanks in advance
    jagdish

  17. Diogo ha scritto:
  18. websurfer ha scritto:

    Thankyou for this posting - it was helpful - especially the waitForElement method in the Setup class.

  19. Ruchi ha scritto:

    I am also having the same issue as Chetan.. I am not able to run the test as I don’t see anything on Run Dialog window. Do we need to add any jar to our classpath? is there any change that needs to be done in Preferences?

Leave a Reply