views:

518

answers:

2

I looked at the documentation and all it says is to create a servlet... With what?

Is there code I need to use for this servlet?

Does it just need to be blank and have the name of postResults?

Is there a provided ant script for this?

I can't find anything on google or selenium's site that lets me in on this.

Thanks

UPDATE: I found the following example

       <servlet>
        <servlet-name>postResults</servlet-name>
        <servlet-class>com.thoughtworks.selenium.results.servlet.SeleniumResultsServlet</servlet-class>

        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>postResults</servlet-name>
        <url-pattern>/postResults</url-pattern>
    </servlet-mapping>

However I can't seem to find this Class file anywhere in my selenium jars. I have the RC and regular core downlaods but no dice. Where do I get this jar file from?

A: 

The postResults servlet is useful in a continuous integration environment where you want to have the selenium test results sent to a URL of your choosing ( I believe it's configurable when setting up your selenium test ) and then have that server include the selenium results as part of the build results. If you don't want to do any post-processing on the selenium test results, then you don't have to setup a postResults servlet at all.

digitaljoel
This does not answer my question at all
mugafuga
Sorry, you seemed confused as to the purpose of the servlet, so I was just pointing out that you don't need to create a servlet if you don't have any plans to use the results in the servlet, which is what it sounds like when you ask about creating an empty one. My apologies.
digitaljoel
Seleniums results pane isn't comprehensive so you have to have a results servlet jet there is no developed easy to implement out of the zip file solution that comes with selenium. So they make you come up with it.
mugafuga
+1  A: 

If you are using the pure html/javascript capabilities of Selenium like I am then you know that you do not get a results report when testing unless you have a postResults servlet setup somewhere to push the results to.

I found a solution by taking apart the fitRunner plug-in to determine what I would need to get one setup.

This is a java solution btw.

http://jira.openqa.org/browse/SEL-102 you can download a zip file here with everything you would need and a bunch of stuff you don't need.

In your webapp just add the servlet mapping you find in the web.xml to your web app. make sure the package your reference is created as such below

Then add the following jars you will find in the zip to your web app library if you don't already have them.

jstl.jar and standard.jar

create two classes your.package.path.SeleniumResultServlet

paste the following code in it.

package com.your.package.path;

import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SeleniumResultsServlet extends HttpServlet {

    private static TestResults results;

    public static TestResults getResults() {
        return results;
    }

    public static void setResults(TestResults testResults) {
        results = testResults;
    }

    protected void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {

     if (request.getParameter("clear") != null) {
      results = null;
      ServletOutputStream out = response.getOutputStream();
            out.println("selenium results cleared!");
     } else {
      forwardToResultsPage(request, response);
     }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
                                                throws ServletException, IOException {
        String result = request.getParameter("result");
        String totalTime = request.getParameter("totalTime");
        String numTestPasses = request.getParameter("numTestPasses");
        String numTestFailures = request.getParameter("numTestFailures");
        String numCommandPasses = request.getParameter("numCommandPasses");
        String numCommandFailures = request.getParameter("numCommandFailures");
        String numCommandErrors = request.getParameter("numCommandErrors");
        String suite = request.getParameter("suite");

     int numTotalTests = Integer.parseInt(numTestPasses) + Integer.parseInt(numTestFailures);

     List testTables = createTestTables(request, numTotalTests);

     results = new TestResults(result, totalTime,
       numTestPasses, numTestFailures, numCommandPasses,
       numCommandFailures, numCommandErrors, suite, testTables);

        forwardToResultsPage(request, response);
    }

    private List createTestTables(HttpServletRequest request, int numTotalTests) {
     List testTables = new LinkedList();
     for (int i = 1; i <= numTotalTests; i++) {
            String testTable = request.getParameter("testTable." + i);
            System.out.println("table " + i);
            System.out.println(testTable);
            testTables.add(testTable);
        }
     return testTables;
    }


    private void forwardToResultsPage(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setAttribute("results", results);
        request.getRequestDispatcher("/WEB-INF/jsp/viewResults.jsp").forward(request, response);
    }

    public static class TestResults {

        private final String result;
        private final String totalTime;
        private final String numTestPasses;
        private final String numTestFailures;
        private final String numCommandPasses;
     private final String numCommandFailures;
        private final String numCommandErrors;
        private final String suite;

     private final List testTables;

        public TestResults(String postedResult, String postedTotalTime, 
                String postedNumTestPasses, String postedNumTestFailures, 
                String postedNumCommandPasses, String postedNumCommandFailures, 
                String postedNumCommandErrors, String postedSuite, List postedTestTables) {

            result = postedResult;
            numCommandFailures = postedNumCommandFailures;
            numCommandErrors = postedNumCommandErrors;
            suite = postedSuite;
            totalTime = postedTotalTime;
            numTestPasses = postedNumTestPasses;
            numTestFailures = postedNumTestFailures;
            numCommandPasses = postedNumCommandPasses;
      testTables = postedTestTables;
        }

        public String getDecodedTestSuite() {
            return new UrlDecoder().decode(suite);
        }

        public List getDecodedTestTables() {
            return new UrlDecoder().decodeListOfStrings(testTables);
        }

        public String getResult() {
            return result;
        }
        public String getNumCommandErrors() {
            return numCommandErrors;
        }
        public String getNumCommandFailures() {
            return numCommandFailures;
        }
        public String getNumCommandPasses() {
            return numCommandPasses;
        }
        public String getNumTestFailures() {
            return numTestFailures;
        }
        public String getNumTestPasses() {
            return numTestPasses;
        }
        public String getSuite() {
            return suite;
        }
        public Collection getTestTables() {
            return testTables;
        }
        public String getTotalTime() {
            return totalTime;
        }
     public int getNumTotalTests() {
      return Integer.parseInt(numTestPasses) + Integer.parseInt(numTestFailures);
     }
    }
}

then go ahead and create a UrlDecoder class in the same package

 package your.package.path;
import java.io.UnsupportedEncodingException;import java.net.URLDecoder;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;

    /**
     * @author Darren Cotterill
     * @author Ajit George
     * @version $Revision: $
     */
    public class UrlDecoder {

        public String decode(String string) {
            try {
                return URLDecoder.decode(string, System.getProperty("file.encoding"));
            } catch (UnsupportedEncodingException e) {
                return string;
            }
        }

        public List decodeListOfStrings(List list) {
            List decodedList = new LinkedList();

            for (Iterator i = list.iterator(); i.hasNext();) {
                decodedList.add(decode((String) i.next()));
            }

            return decodedList;
        }

}

In your web-inf create a folder called jsp

copy the viewResults.jsp into it that is in the zip file. copy the c.tld to the /web-inf

restart your server

if you get some goofy errors when trying to load the postResults servlet from selenium try changing the taglib reference int the viewResults.jsp to use the sun url instead due to different version dependencies it may not work. servlet 1.0 vs. 2.0 stuff.

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Then when you run the test runner and choose automatic option in your selenium screen you will have a postResults servlet that you can use and customize at will.

Hope this helps others. Having a way to format test results is a great help when trying to create documentation and the stuff that comes out of the zip with regular selenium doesn't give you this basic functionality and you have to bake it up yourself.

mugafuga