Quick Tutorial: Netbeans + Selenium + Hudson

Selenium is one of the most powerful web application test tool I know. It supports a wide range of Browsers, Operating Systems, Languages and Test Frameworks (find all infos here). It takes a while to learn all the commands in the API that you can use to test your web application, fortunately there are some plugins  that help you to get started. Although I found it confusing to get started with Netbeans and Hudson, each having its own plugin, plus Firefox also offering a plugin, and some websites and tutorials leading in different directions (if you are in hurry like me).

This short tutorial walks you through the steps to get started in a few minutes.

Remarks:

  • A CI or build-server is usually headless, means there is no desktop, no Gnome, no GUI, so would not be possible for Selenium to start and control a browser. This tutorial assumes you run a build-server with desktop. At a later stage we will discuss the headless testing.
  • In this tutorial we will implement a simple JSP web application with 2 pages and create a JUnit test triggering the Selenium Test.
  • The Netbeans plugin and Hudson plugin for Selenium is not required for this tutorial.
  • The tutorial is created with Ubuntu as OS (Windows should be similar)

Pre-Requirements:

  • Netbeans 6.8 (earlier versions certainly work as well)
  • Glassfish
    A local Glassfish that comes with Netbeans is good enough.
  • Hudson Server
    Download the war file from here. For the tutorial we will use a local standalone Hudson server.
  • Selenium RC
    Download from here.
  • Sourcecode Repository (SVN)
    In order to use Hudson, it is recommended to have a sourcecode repository like SVN to upload the code.

Tutorial:

  • Create a new Web Application
    Call it WebApplSelenium, store all libraries in local folder, no frameworks.

    New Web Application

    New Web Application

    New Web Application

  • Implement 2 JSP Pages
    Lets create 2 pages, on the first pages a simple form with 2 entries and the second page will echo back the entries.simpleform.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
     "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>JSP DEMO</title>
     </head>
     <body>
     <h1>Sample entries:</h1>
     <%-- Call another JSP file on submit--%>
     <form action="simpleresult.jsp">
     Name: <input type="text" name="yourName" value="" /><BR>
     Age: <input type="text" name="yourAge" value="" /><BR>
     <input type="submit" value="Send" />
     </form>
    
     </body>
    </html>
    

    simpleresult.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
     "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>JSP Demo</title>
     </head>
     <body>
     <h1>Values from previous form:</h1>
    
     <%-- JSP Expression --%>
     <%= request.getParameter("yourName") %><br>
     <%= request.getParameter("yourAge") %>
    
     </body>
    </html>
    
  • Deploy and Run the Web Application
    http://localhost:8080/WebApplSelenium/simpleform.jsp

    Run the Web Application

  • Add the Selenium Client Driver
    Unzip the file that you downloaded from selenioumhq.org earlier.
    Add the client driver to the Web project (selenium-java-client-driver.jar)

    Selenium RC Directory

    Project Properties

  • Create a Test Class
    Right Click on Test Packages | Create Java Class (We dont need to use the tools.. to cretae JUnit test)

    
    package webtest;
    
    import com.thoughtworks.selenium.*;
    import org.junit.*;
    
    public class simpleFormTest {
    
     static private String appServerURL = "http://vcb3:8080/";
    
     @Test
     public void testSimple() throws Exception {
    
     Selenium selBrowser = new DefaultSelenium("localhost", 4444, "*firefox", appServerURL);
     selBrowser.start();
     // optional to make it visible/slower:
     // selenium.setSpeed("500");
     selBrowser.open("/TEST_WebAppTestSelenium/");
     selBrowser.type("fullname", "bla1");
     selBrowser.type("age", "99");
     selBrowser.click("//input[@value='Press me']");
     // optional for slower pages (?):
     // selBrowser.waitForPageToLoad("3000");
     Assert.assertTrue(selBrowser.isTextPresent("bla1, 99"));
     selBrowser.stop();
    
     }
    }
    
  • Start the Selenium RC Server
    Open a shell and navigate to the  Selenium Server path (see above directory structure) and start the server
    java -jar selenium-server.jar

    Running Selenium RC Server

    Now we have a Selenium Server running and listening on port 4444.
    Warning: If you installed the Netbeans plugin for Selenium, it will auto-install and autostart a Selenium Server. In that case either stop it or use it.

  • Run the Test locally
    Select Test file (slect the test class in the project browser). Execute JUnit tests for the project might be disabled.
    Warning: Our JUnit test is not fully linked with our project yet. We need to deploy the application first before triggering the test (or activate deploy on save).

    Running Selenium Test

    Test Result

  • Start Hudson
    Navigate to the path of the hudson.war file you downloaded earlier and start it. Note the default port is 8080, it will create a conflict if you start it on the same box as Glassfish.
    java -jar hudson.war –httpPort=7080

    Start Hudson (standalone)

  • Create a Hudson Job and execute it
    (I skip this part for now because it would be a repetition to the previous tutorial here)
    Note: You need apply the same changes to your build.xml and project properties to get the build executed.

    Hudson will execute our test script and trigger Selenium to open the browser for testing.
    Test will be successful.

    ..
    compile-test:
    
    -pre-test-run:
    
    -do-test-run:
     [junit] Testsuite: webtest.NewSeleneseTest
     [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 8.329 sec
     [junit]
    
    test-report:
    
    -post-test-run:
    ..
    

Optional:

  • Download and install the Firefox Selenium IDE from here.
  • Open the IDE in Firefox (tools|Selenium), open the URL of the web application and start performing the inputs and user activities that shall be replayed in out test.

    Recording Selenium Test

  • Change the the clipboard format to Java (JUnit) (Options|Clipboard Format), select the commands from the list and copy. Paste into the respective test class in our project.
    selenium.open("/WebApplSelenium/simpleform.jsp");
    selenium.type("yourName", "someName");
    selenium.type("yourAge", "75");
    
  • You have the option to export the complete java text class from the Selenium IDE but we wont need that since we implemented out test skeleton already.

Open Issues:

  • The triggered JUnit test is executing a test with an already deployed web application, it is actually required to perform a build-deploy-test sequence. The optional Hudson plugin to deploy war files does not help because it will deploy at the en of a build/test job.
  • Execute the build and test on a headless server.
Advertisements

5 thoughts on “Quick Tutorial: Netbeans + Selenium + Hudson

  1. Pingback: Quick Tutorial: Netbeans + Selenium + Hudson (Part 2) « The JavaDude Weblog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s