Friday, December 20, 2013

Develop test cases using the Selenium WebDriver

Welcome back!  Today we will make some tests using Selenium WebDriver. All you need is to download Eclipse IDE For Java EE Developers and Selenium from our previous post. 

First step: Installation and configuration

Once you have downloaded Selenium you have to decompress the downloaded file.  You will find two different files .jar with the name of selenium -java-2.35.0.jar and selenium-java-2.35.0-srcs.jar. The name can change depending of the downloaded version of the tools, these files have to be copied on the libs folder of selenium.




Now that you have copied the files in the libs folder, you can create a new project in eclipse in which you can make some basic tests with the help of selenium. First you go to eclipse then you go to File->New->Java Project.


The next window will be displayed. Here you are going to write the project’s name. In this example the name of the project is “Tests”.




Then, you click on the Next button and you go to libraries.



You will see the next window, here you click on the button that says Add External JARs, a window will appear in which you have to select all the files .jar that are in the libs folder of selenium.


Once you have selected all the .jar files of selenium, you have to click on the finish button. The new project will appear at the left side of the screen.  In the left side of the screen you will find a section that says Referenced Libraries that contains all the files that you have selected previously.



The configuration is done :)  

We can start to write the tests, hooray!


Second step: Writing the test cases

Now we will do two simple tests:

  • search a text in google 
  • obtain the title of the Google page.



To do the tests go to File->New->JUnit Test Case.


You will see a new window in which you will have to write the name of the JUnit Test Case, in this case the name is “prueba” (it means test in our native language ;) ).


Once you have created the file, you will be able to make the test, first you create the test that opens firefox, goes to the google page and search ‘Selenium WebDriver’. To do this you have to import the following libraries:

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

To do the test you can use the following code, this test after searching  ‘selenium WebDriver’ obtains the title of the page and compare if the title of the page isSelenium WebDriver - Buscar con Google’, if the title is different the test will fail:

@Test
 public void test() throws InterruptedException {
  WebDriver firefox_driver = new FirefoxDriver();
  firefox_driver.get("https://www.google.com.gt");
  WebElement search = firefox_driver.findElement(By.name("q"));
  search.sendKeys("Selenium WebDriver");
  WebElement search_button = firefox_driver.findElement(By.id("gbqfb"));
  search_button.click();
  Thread.sleep(2000);
  System.out.println(firefox_driver.getTitle());
  if(!firefox_driver.getTitle().equals("Selenium WebDriver - Buscar con Google")){
   fail("Test Fail");
  }
 }

Now to do the test that obtains the title of the page and prints it in the console you can use the following code, this test will fail if the title of the page is not Google:

@Test
 public void title() {
  WebDriver firefox_driver = new FirefoxDriver();
  firefox_driver.get("https://www.google.com.gt");
  System.out.println(firefox_driver.getTitle());
  if(!firefox_driver.getTitle().equals("Google")){
   fail("Test Fail");
  }
 }

So that´s it for writing the tests, in the following post we will use ANT to execute them and then we will automatize the execution of our testcases using Hudson.

See you soon!

4 comments :

  1. The blog gave me an idea to develop test cases using selenium Thanks for sharing it
    Selenium Training in Chennai

    ReplyDelete
  2. Great post and i have one doubt. How to develop a template for test cases?. Please can anyone explain. Software Testing Training in Chennai

    ReplyDelete
  3. Great sharing about test cases. keep sharing. want to learn selenium training with best placement support reach us Selenium Training in Chennai

    ReplyDelete