5/24/2006

 

Integrating Selenium With Build Script

[Information Below is about Selenium-Core only] When I was at TWU, michael used three days to put selenium tests into build script. I even don't know the cc.net is actually running the selenium tests finally. From that time, I know, this is not a easy problem. Today it is my first time trying to setup a cc running selenium tests. It costs nearly a whole day to fight with resin and jdk bugs. Fortunately, I won the game at last:) Here is some tips I want to share: Steps:
  1. Start server
  2. Run Tests
  3. Get Result
  4. Stop server
Key Problem is "How to get the result". Selenium will call a url "postResult" After all tests were finished. The official solution to catch the result is writing a servlet. But here are two problems need to be taken into consideration: 1. When to stop the server? 2. How to know tests passed? To solve these two problems, the servlet need to "provide" information about the testing progress and result. So one side, the servlet is a result recevier from selenium; on the other side, it is the selenium testing information provider for build script. Here is my code:
  
public class SeleniumResultServelet extends HttpServlet {
 
 private String result = null;
 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  OutputStream outputStream = response.getOutputStream();
  if (result == null) {
   outputStream.write("pending".getBytes());
  } else {
   outputStream.write(result.getBytes());
  }
  outputStream.write("\r\n".getBytes());
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
  result = request.getParameter("result");
 }

}
ps: selenium use "POST" to access postResult URL, build script use "GET" to access postResult URL. Inside the building script, there is a loop:
while(true) {
    Thread.sleep(500);
    URL url = new URL(postResultURL);
    InputStream inputStream = url.openStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    String status = reader.readLine().trim();
    reader.close();
    if ("failed".equals(status)) {
     throw new RuntimeException("Selenium Test Failed");
    }
    if (!"pending".equals(status)) {
     break;
    }
   }
Above it is my premature way to integrate selenium... ps: the reason why I write this part of build script in Java is not only because I need to integrate selenium, but mainly because Resin can not be stopped by ant under windows (I tried windows service, but failed). So please don't blame me about that...

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]