Chrisrod's Weblog

febrero 21, 2010

Creating a Selenium RC test case in 7 simple steps

Archivado en: 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.

Advertisement

Dejar un comentario »

Aún no hay comentarios.

RSS feed para los comentarios de esta entrada. URI para TrackBack.

Deja un comentario

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

Logo de WordPress.com

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

Twitter picture

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

Facebook photo

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

Connecting to %s

Theme: Rubric. Blog de WordPress.com.

Seguir

Get every new post delivered to your Inbox.