Start with Java Webdriver - Procedure to create first test script for the beginners

Prerequisites:
  1. Download Selenium2 jar file from http://seleniumhq.org/download/.
  2. Download TestNG jar file from http://testng.org/doc/download.html
  3. Download and Install JDK and setup java path 
  4. Download Eclipse IDE from http://www.eclipse.org/downloads/
  5. Install TestNG plugin in downloaded eclipse
  6. Firefox should be install with selenium IDE plugin.
Step by step to create first script:

Create Project: 
Open eclipse IDE  and chose work space.


Click on file menu> new > java project.
Enter project name and click Finish button


Created project displayed into eclipse explorer window.
Right click on src folder of created project and click new > packages
Enter package name as I enter “com.test” and click finish button.
Created package reflect under src folder.

Add jar files:

Right click on project click on “build path” and then click on “Configure Build Path” screen should see as below.

Click add “Add External Jars” button, navigate and select “selenium-server-standalone-2.**.0.jar”  and “testng-*.*.jar” files 
Click on Ok button. Jar files listed in explorer window as below.



Record Test Scripts using seleniumIDE convert them into Java/ Junit 4/ Webdriver

Convert junit test scripts into TestNG script:

Remove blow imported Junit Class

import org.junit.*;
import static org.junit.Assert.*;

import below TestNG class.

import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

Change Befor to BeforSuite and After to AfterSuite.
Code look like as below structure.


package com.test;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class FirstTest {
  private WebDriver driver;
  private String baseUrl;

  @BeforeSuite
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "https://www.google.co.in/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testUntitled() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.cssSelector("#gb_2 > span.gbts")).click();
    driver.findElement(By.cssSelector("#gb_1 > span.gbts")).click();
    driver.findElement(By.cssSelector("#gb_8 > span.gbts")).click();
    driver.findElement(By.cssSelector("#gb_1 > span.gbts")).click();
  }

  @AfterSuite
  public void tearDown() throws Exception {
    driver.quit();
  }
}


Right click on class select “Run As” and click on “TestNG Test”.  Test execution should be start.
After execution result generated in “test-output” folder. Click on “index.html” result look like as below

1 comment:

  1. Hey!
    Your content has really helped me a lot. I liked your blog's content very much and i would like you to share this information with rest of the world by making a website. This will help others to get your knowledge and also help you to earn money out of your website. In return for the information that your blog provided me and helped me, i can help you regarding the website thing(including how to earn from your website), you can contact me anytime on freestuffs20@gmail.com or add me on Skype:kool20692

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution