Chrisrod's Weblog

febrero 21, 2010

Creating a Selenium RC test case in 7 simple steps

Filed under: Selenium Test — chrisrod @ 7:40 pm

In this little tutorial I will explain as clear as I can, how to create a test case using Selenium IDE, Selenium RC and JUnit, hope it will be useful for everybody.

01.   Download Selenium RC and after downloading Selenium IDE (http://seleniumhq.org/download/) install it in Firefox

Seleniumrc

Figure 01. Download page of Selenium IDE and RC.

 

02.   Open Selenium IDE in firefox, look for my blog in google and click on the first link that appears:

SeleniumIDE

Figure 02. Selenium IDE on firefox.

 

03.   Export the test case to JUnit clicking on Options à Format àjava JUnit

04.   Personally, I don’t want just to copy and paste the code that is just autogenerated  by Selenium IDE, you can do this, but it is more fun to create some classes and subclasses and then make run your test cases J. So you can check at the code that I generated from the help that I got from Selenium IDE.

package com.selenium.test;

 

import com.thoughtworks.selenium.SeleneseTestCase;

 

/**

 *

 * Abstract class that will setUp() and tearDown() all the test classes that

 * will extend this class.

 *

 * @author Christian Roman

 *

 */

abstract class SeleniumTestBase extends SeleneseTestCase {

      /**

       * Method that will open the web browser and redirect it to the main page of

       * google.

       */

      @Override

      public void setUp() throws Exception {

            setUp(«http://www.google.com.bo/», «*chrome»);

            selenium.open(«/webhp?hl=es»);

      }

 

      /**

       * Method that will close the session opened by the method setUp().

       */

      @Override

      public void tearDown() throws Exception {

            selenium.close();

            selenium.stop();

      }

     

      public void clickGoogleLink(String keyword, String link){

            selenium.type(«q», keyword);

            selenium.click(«btnG»);

            selenium.waitForPageToLoad(«30000»);

            selenium.click(«link=»+link);

            selenium.waitForPageToLoad(«30000»);

      }

 

}

Listing 01. SeleniumTestBase.java

 package com.selenium.test;

 

import org.junit.Test;

 

public class GoogleSearchTest extends SeleniumTestBase {

     

      @Test

      public void testChrisrodWordpress() throws Exception {

 

            clickGoogleLink(«chrisrod wordpress», «SEAM « Chrisrod’s Weblog»);

            assertTrue(selenium

                        .isTextPresent(«Uso del LDAP para el modulo de autenticaciones en SEAM»));

      }

     

      @Test

      public void testHowToDraw() throws Exception {

            clickGoogleLink(«how to draw faces», «Portrait Drawing – Learn to Draw Faces»);

            assertTrue(selenium.isTextPresent(«Learn portrait drawing skills including face proportions»));

      }

}

Listing 02. GoogleSearchTest.java

05.   The structure of my code looks like this:

Figure 03. Code structure for running the test cases and libraries needed to accomplish this objective.

 

06.   Run selenium server, I mean double click over the jar file: selenium-server.jar, that comes with Selenium RC.

07.   Run your JUnit test case and enjoy watching how your web browser open, executes commands and close by itself (well actually controlled by Selenium Server)

CodeStructureRC

Figure 04. Running JUnit on GoogleSearchTest.

SeleniumRCinAction

Figure 05. Selenium Server in action.

febrero 16, 2010

A BRIEF EXPLANATION OF SELENIUM TEST

Filed under: Selenium Test — chrisrod @ 11:09 pm

Se

 01.                       What is selenium?

 

Selenium is a set of tools that is used to automate web application testing, across many platforms.

 

Between the biggest advantages of Selenium, we can mention:

          Runs in many browsers.

          Can be controlled with programming language like Java or PHP, that makes selenium very handy to integrate it to unit tests.

Selenium is usually used to have test cases that need to be run frequently,  with unlimited iterations and at the same time through this is possible to have a disciplined test documentation.

02.                       Components

Selenium-ide-logo

2.1 Selenium IDE

               Selenium IDE is a Firefox Add on, that can be easily download from this site:

http://seleniumhq.org/download/

               This tool is integrated with Firefox and the way to use it is very easy and friendly to use.   Selenium IDE works like a recorder, recording all the actions that one has on a specific website, like clicks, data typed in a form,  making it possible to reproduce all steps took, and by this way create a test suite, that can be easily run with this handy tool.

 

Another great feature of this IDE, is that one can have a well structured explanation of all the possible commands that can be recorded or executed by Selenium IDE. Talking about the commands, with Selenium IDE is possible to have assertion or verifications when they are needed, set some delays, wait a determined time to a page to load, etc, etc.

 

If there is a disadvantage on using Selenium IDE, is that all the test cases have to be recorded on Firefox.

 

Getting into the programming world, Selenium IDE make possible to export all the commands that were set or recorded to one or all these programming languages:

          Java (JUnit or TestNG)

          Groovie (JUnit)

          C#

          Perl

          PHP

          Python

          Ruby

                SeleniumIDE

Figure 01. Selenium IDE

 

Selenium-rc-logo

2.2 Selenium RC.

               Selenium Remote Control, gives Selenium all the flexibility that just a programming language can offer, making possible the development of test cases well structured, with programming logic (like iterations, control flow, etc), that at the end

 means a real automation of a suit of testing cases on an automated build environment.

Selenium-grid-logo

2.3 Selenium Grid.

               Selenium Grid is on my opinion, one of the most interesting technologies in the field of web application testing, because apart of the fact of flexibility that Selenium RC offer to the testing cases, with Selenium Grid, is possible to run as many tests as we want  in a parallel way, relying on the HW capabilities or number of computers available to run the test cases.

 

With the help of Selenium Grid, one can run test cases in different browsers at the same time, on different operating systems, and by this way, having at the end a report that tells how the web application is behaving with all the test cases that run on it.

 

  

03.                       Selenium RC

 

3.1. What is Selenium RC?

 

As was mentioned previously, Selenium RC allow the creation of more complex test cases, because now is possible to rely on all the facilities that a programming language can offer, like querying to a Data Base, reading files, sending e-mails, etc.

Is important to mention that Selenium RC is composed by these components:

          Selenium Server

          Selenium Core

          Client Libraries

 

 Selenium Server.

        The Selenium Server is the hearth of Selenium RC, because is on this place, where all the commands sent by the programming language and all the messages answered by the web browser, are processed, interpreted and sent to the corresponding party.

Other functions that take place in the Selenium Server, is launching and closing the web browser, as mentioned at the previous paragraph, running the Selenese Commands to be executed in the browser through the injection of Selenium Core in the browser, HTTP proxy from which all the HTTP messages pass.

The way Selenium Server accomplish all these tasks, is basically because Selenium Server works relying on GET/POST request, that makes possible to any programming language to send request to the Selenium Server.

 Selenium Core.

As was mentioned previously, Selenium Core is a chunk of JavaScript functions that interpret and run Selenese Commands in the browser where the test is executed.

Selenium Core resides into the Selenium  Server and when this opens the Web Browser, the Selenium Core is injected in it and by this way any Selenese Command can be executed in the browser.

Client Libraries.

The client libraries are the interface between the Selenium Server and a Programming language. For the very same reason Selenium provides many methods to interact with the Web Browser, there is even a method to execute JS commands directly in the browser.

SeleniumRC Squema 

Figure 02. Selenium RC

04.                       Selenium Grid

4.1 What is Selenium Grid?

Selenium Grid is the best thing that Selenium could ever develop (so far), and I say this because with Selenium Grid, as mentioned before, is possible to run test cases in a parallel way in one or many computers, and not just that, because is possible to run on different operate systems, different browsers, and all at the same time, fantastic, isn’t it?

The components of Selenium Grid, are:

          Selenium Hub

          Selenium RCs

Selenium Hub.

Basically the functions of the Selenium Hub are the functions of a network switch, because all the test cases are now addressed to the hub instead of the RC and  this hub allocates all the test cases on every RC that is connected to it.  This means that the main function of the Selenium Hub is redirect all the Selenese commands sent by the Test Cases, to the corresponding RC and at the same time keep track of the testing sessions.

To make possible to Selenium work on this way, it is necessary to implement test cases that have to be executed in a parallel mode. Talking about JUnit, there is an extra library called “Parallel JUnit” https://parallel-junit.dev.java.net/, and through this library is possible to execute more than one test case at the same time. The other possibility, that I personally recommend, is using TestNG http://testng.org that is a testing framework inspired in JUnit, and for this reason the logic of creating testing cases is basically the same, TestNG also includes many annotations to its framework, that make the life easier when creating test cases. To mention other advantages of TestNG, we can say that:

          TestNG has a configuration file (testing.xml) that makes possible to configure the way that we want to run the test cases, and between these possibilities, running test cases in parallel.

          TestNG has support of parameters.

          TestNG has an Eclipse plug-in.

           TestNG makes possible to classify test cases by groups and by this way control the order and priority of the execution of the test cases.

 Selenium Grid Setup

Figure 03. Selenium Grid

05.                       Conclusions

 

Selenium is a powerful tool that can be used in many ways, but is particularly interesting the way that Selenium can be used to run and automate test cases, what makes possible to have testing cases that can be executed periodically and with this to have at the same time a good documentation of all the test cases that were created for a web application.

 

With the use of Selenium RC and Selenium Grid, the automation and running of test cases takes a different perspective, making possible to have a very detailed coverage of all the possibilities available in a web application, reviewing as well the behavior of it on different Operating Systems and Web Browsers especially when cross browser compatibility is a must.

 

Crea un blog o un sitio web gratuitos con WordPress.com.