Home > Agile, Java, Selenium, Test Automation > Test Automation using Java, Selenium WebDriver and TestNG

Test Automation using Java, Selenium WebDriver and TestNG

SOFTWARE REQUIREMENTS:

  • Windows 7 as OS
  • Eclipse as IDE
  • TestNG as Unit Testing Tool
  • Selenium as a Test Automation Tool

SET-UP INSTRUCTIONS:

Adding the Selenium WebDriver to the project:
  • Download the Selenium WebDriver for Java from the location: http://seleniumhq.org/download/
  • Create a new Java project in Eclipse IDE using File menu: ‘File -> New -> Java Project
  • In Step 1 of Wizard, Add the Project name (say ‘JavaTestAutomation‘ as in my case) and click on the ‘Next‘ button.
  • In Step 2 of Wizard, click on the ‘Libraries‘ tab and then click on ‘Add External JARs‘ button.
  • Add all the Selenium JAR files from the downloaded Selenium WebDriver folder in the ‘JAR Selection‘ window.
  • Finally, click on the ‘Finish‘ button in the Wizard window.
Setting up TestNG with Eclipse:
  • In Eclipse, select ‘Help -> Install New Software…
  • Input the site URL http://beust.com/eclipse in the ‘Work With‘ field and hit the ‘Enter‘ key. ‘TestNG‘ appears in the list.
  • Select ‘TestNG‘ option and click on the ‘Next’ button as in the snapshot.
  • Accept the terms of the license agreement for TestNG and click on the ‘Next‘ button. TestNG plug-in installation progress indicator is displayed.
  • Restart the Eclipse and TestNG plug-in is installed as it appears under the ‘Run‘ toolbar option shown in the snapshot below:

Consider a sample test scenario

As an end user,
I would like to visit the google search page 
And then I would like to search an item so that
I can view the search results

STEPS:

  • In Eclipse, create a new Class file (say ‘GoogleSearch’ in my case) using File -> New -> Class.
  • In ‘New Java Class’ window, set the Name field to ‘GoogleSearch‘ and Package field to ‘googlesearch‘ as in the snapshot below. Click on the ‘Finish‘ button.
  • Define the required packages under the import declaration.
  • Under the @Test attribute, define a constructor to create a new instance of the Firefox driver.
  • Under the @Test attributenavigate to the google search page.
  • Under the @Test attributefind the text input element by its name. Here, the element name for the search field is ‘q’, which is identified using the Firebug (Add-ons for Firefox).
  • Under the @Test attribute, input the search text and hit the Search button.
  • Under the @Test attribute, validate the result using Assert.assertEquals method.

 

TEST EXECUTION INSTRUCTIONS:

  • Copy and paste the code snippet below into Eclipse.
package googlesearch;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.testng.Assert;
import org.testng.annotations.Test;

public class GoogleSearch {

protected WebDriver driver;

@Test
public void googlesearch()
{
// Create a new instance of the Firefox driver
this.driver = new FirefoxDriver();

// Navigate to the site
driver.get("http://www.google.com");

// Find the text input element by its name
WebElement SearchTextBox = driver.findElement(By.name("q"));
WebElement SearchButton = driver.findElement(By.name("btnG"));

// Enter something to search for
SearchTextBox.sendKeys("Selenium");
// Click on the Search button
SearchButton.click();

// Google's search is rendered dynamically with JavaScript
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("selenium");
}
});

// Check that the Title is what we are expecting
Assert.assertEquals(driver.getTitle(), "selenium - Google Search");
driver.quit();
}
}

RUN THE TEST USING TestNG:

  • In Eclipse, Go to Run -> Run As ->  TestNG Test.
  • Test is executed using TestNG and the result will be displayed in TestNG view as shown in the snapshot below.

About these ads
  1. Tester
    March 15, 2013 at 11:02 am

    Very helpful…thankyou

  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: