Sunday, July 27, 2014

Running Selenium Tests in (Eclipse + Junit) Framework


Use the Selenium IDE for recording the tests. From the below website, Download Firefox plugin which records and plays back user interactions with the browser.

http://seleniumhq.org/download/

You can export the recorded test case into desired programming language using the option: File -> Export Test Case AS -> Java / JUnit 4 / WebDriver



Download and install the Eclipse from : http://www.eclipse.org/downloads/

Download the JUnit jars (junit.jar and hamcrest-core.jar) from http://junit.org/


Create a project in eclipse. Add JUnit jars in project's build path: Properties -> Java Build Path -> Libraries -> Add External Jars

Download the Selenium Client jars from below website. Add the jars to project's build path.

http://docs.seleniumhq.org/download/

------------------
You may test your JUnit installation by creating following two classes:

Create a class MessageDisplay in the project

public class MessageDisplay {

   private String message;

   /* Constructor
    * @param: message to be printed
    */
   public MessageDisplay(String message){
      this.message = message;
   }
   
   /* prints the message to console */
   public String displayMessage(){
      System.out.println(message);
      return message;
   }
}

Create a class TestMessageDisplay in the project

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class TestMessageDisplay {

   String message = "Hello World";
   MessageDisplay messageDisplay = new MessageDisplay(message);

   @Test
   public void testPrintMessage() {
      assertEquals(message,messageDisplay.displayMessage());
   }
}

Run the program successfully by right clicking on TestJUnit: Run As -> JUnit Test
------------------

Now add a test Case to the project by going to Project Explorer: New -> JUnit Test Case



Now, copy the code that Selenium IDE generated. You may need to fix compilation erros (such sa package name errors, class name errors, etc).

Run the test case by right clicking: Run As -> JUnit Test






Further info:

http://docs.seleniumhq.org/docs/03_webdriver.jsp#webdriver-backed-selenium-rc

- Web Driver vs. RC

Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation. How these direct calls are made, and the features they support depends on the browser you are using. Information on each ‘browser driver’ is provided later in this chapter.

For those familiar with Selenium-RC, this is quite different from what you are used to. Selenium-RC worked the same way for each supported browser. It ‘injected’ javascript functions into the browser when the browser was loaded and then used its javascript to drive the AUT within the browser. WebDriver does not use this technique. Again, it drives the browser directly using the browser’s built in support for automation.

- Do you need Selenium Server?

You may, or may not, need the Selenium Server, depending on how you intend to use Selenium-WebDriver. If you will be only using the WebDriver API you do not need the Selenium-Server. If your browser and tests will all run on the same machine, and your tests only use the WebDriver API, then you do not need to run the Selenium-Server; WebDriver will run the browser directly

- Running Standalone Selenium Server for use with RemoteDrivers

From Selenium’s Download page download selenium-server-standalone-.jar
Start the server on the command line with
java -jar /selenium-server-standalone-.jar