Pages

Monday, October 17, 2011

How to run Selenium WebDriver code with JUnit

In my previous post I have shown how to run Selenium WebDriver Code with NUnit.

Now I will show you how Selenium WebDriver code generated from selenium IDE can be run at JUnit4 Framework.

Selenium IDE has a functionality to convert its recorded script to Selenium WebDriver and Java in JUnit4 Framework.

So If we know how to record Selenium IDE (see the post : http://bit.ly/pa1TG5) then we can easily convert it to selenium WebDriver code.

Before starting we just define a Test case.

Test case :

    1. Open Selenium Website (http://seleniumhq.org/) in Firefox browser.
    2. Click Project tab
    3. Click Selenium IDE link
    4. Verify that Selenium IDE Text is present

Step 1: Record the test case in Selenium IDE

  • Open Firefox Browser
  • Open Selenium IDE window by Clicking Tools->Selenium IDE
  • Make Red colored Record button on.
  • Enter the URL : http://seleniumhq.org/ at the address bar and click Search button
  • Click Project tab
  • Click Selenium IDE link
  • Verify that Selenium IDE Text is present

The recorded script will be like the following figure

se9_thumb

Now make the record button off and Click Run button to run the script.

Step 2: Convert the Selenium IDE recorded script to JUnit 4 (WebDriver) code

  • Go to Selenium IDE window
  • Click Options menu
  • Click Format –> JUnit 4 (WebDriver)

se27

Sometimes in Options –> Format menu, JUnit 4 (WebDriver) options may not be displayed. The menu options like:

se13_thumb1

In that case, there is some change needed in selenium IDE option window. Please follow the steps:

  1. Click Options menu in Selenium IDE window
  2. Click “Options…” and Selenium IDE options window is displayed.

se12_thumb

se14_thumb1

Click Enable experimental features check box and click OK button. Now go to Format options and driver list.

  • A message dialog is displayed after selecting the appropriate format like (JUnit 4 (WebDriver))

se11_thumb

  • Click OK button
  • Selenium IDE window shows the selenium WebDriver Code in Java format with unit testing Framework JUnit 4 is implemented.

se28

 

Step 2: Run the Code in Eclipse IDE

  • Open Eclipse IDE
  • Create a New Java project by Clicking the options File –>New –>Java Project and New Java Project Window is displayed

se29

  • Enter Project name and select Project execution environment as JavaSE-1.6
  • Click Next button
  • Click Finish button
  • New Project is created
  • Add a New Java Class to the project
  • Now Copy the whole text from Selenium IDE and paste into New Java class file. It is like Copy the whole text like CTRL+A to select the whole text and CTRL+C to copy. 
  • There are some Reference error show at editor.
  • Right click on the project name and click Properties and Properties window is displayed

se30

  • Click Java Build path and click Libraries Tab
  • Click Add Library button

se31

  • Add Library window is displayed
  • Select JUnit and click Next
  • Select JUnit4 as JUnit Library and click Finish button
  • JUnit4 library is added and click OK button of properties window
  • Now go to selenium download page ( http://seleniumhq.org/download/ ) and download the latest version of Selenium Client Driver for Java.  Here version 2.8.0 is downloaded.

se33

  • Now unzip downloaded zip file and rename the folder as “Selenium” and placed to the Current project folder
  • Right click on the Project name in Eclipse and click Properties and properties window is displayed
  • Click Java Build Path
  • Go to Libraries tab and click Add External JARs

se34

  • Go to Selenium folder and select the jar file named “selenium-java-2.8.0.jar” and click Open. Selenium webdriver jar file is added

se35

  • Click OK button of Properties window
  • Remove the package name “package com.example.tests;”
  • Rename the Class name Untitled to Java Class file name

  • Add the following code at the testUntitled() method
Assert.assertEquals("Text Found", "Selenium IDE", driver.findElement(By.xpath("//div[@id='mainContent']/table/tbody/tr/td/p/b")).toString());


  • The method will be
public void testUntitled() throws Exception {
    driver.get("http://seleniumhq.org/");
    driver.findElement(By.linkText("Projects")).click();
    driver.findElement(By.linkText("Selenium IDE")).click();
    Assert.assertEquals("Text Found", "Selenium IDE", driver.findElement(By.xpath("//div[@id='mainContent']/table/tbody/tr/td/p/b")).toString());
  }
 


  • The whole codes are

  1: 
  2: 
  3: import java.util.regex.Pattern;
  4: import java.util.concurrent.TimeUnit;
  5: 
  6: import junit.framework.Assert;
  7: import junit.framework.AssertionFailedError;
  8: 
  9: import org.junit.*;
 10: import static org.junit.Assert.*;
 11: import static org.hamcrest.CoreMatchers.*;
 12: import org.openqa.selenium.*;
 13: import org.openqa.selenium.firefox.FirefoxDriver;
 14: import org.openqa.selenium.support.ui.Select;
 15: 
 16: public class testSel {
 17:   private WebDriver driver;
 18:   private String baseUrl="";
 19:   private StringBuffer verificationErrors = new StringBuffer();
 20:   @Before
 21:   public void setUp() throws Exception {
 22:     driver = new FirefoxDriver();
 23:     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 24:   }
 25: 
 26:   @Test
 27:   public void testUntitled() throws Exception {
 28:     driver.get("http://seleniumhq.org/");
 29:     driver.findElement(By.linkText("Projects")).click();
 30:     driver.findElement(By.linkText("Selenium IDE")).click();
 31:     Assert.assertEquals("Text Found", "Selenium IDE", driver.findElement(By.xpath("//div[@id='mainContent']/table/tbody/tr/td/p/b")).toString());
 32:   }
 33:   @After
 34:   public void tearDown() throws Exception {
 35:     driver.quit();
 36:     String verificationErrorString = verificationErrors.toString();
 37:     if (!"".equals(verificationErrorString)) {
 38:       fail(verificationErrorString);
 39:     }
 40:   }
 41: 
 42:   private boolean isElementPresent(By by) {
 43:     try {
 44:       driver.findElement(by);
 45:       return true;
 46:     } catch (NoSuchElementException e) {
 47:       return false;
 48:     }
 49:   }
 50: }
 51: 


  • Now Run project by click Run button from JUnit4

se34



  • Firefox Browser is opened and Script runs and Test is successful.

That’s all. I have described here all the steps in a very easy and details way. If any thing could not be understand, please comment me, I must answer. Please comment me about the whole blog.

8 comments:

  1. Hi Nadim,

    Thank you, its very useful.
    While running code i am getting error

    Exception occurred executing command line.
    Cannot run program "D:\x\Selinum work trial\seltest\test.class" (in directory "D:\x\Selinum work trial\seltest"): CreateProcess error=193, %1 is not a valid Win32 application

    ReplyDelete
  2. Hello Nadim,

    I had to replace the "toString()" method with "getText()" in the assert to get this to work. Thanks!

    ReplyDelete
  3. I also had to make the above change. Overall a very helpful tutorial

    ReplyDelete
  4. Hello Nadim,

    I took all steps as you described, but I got an error: java.lang.NoClassDefFoundError. Could you prompt to me what I did incorrectly?

    ReplyDelete
  5. Hello Nadim,

    I also got the same class not found error, please suggest me what has went wrong

    ReplyDelete
    Replies
    1. Please use all the files from selenium folder.

      Delete
  6. Hi,

    please check the Selenium-server.jar file or may be your using latest version of the browser.... check with ie8 you may not get this Error....

    ReplyDelete
  7. Please find the below useful link which provides selenium webdriver tutorials which will also guide you to build a good framework.
    http://seleniumeasy.com

    ReplyDelete