Archive

Archive for February, 2014

Test Automation using Java, Selenium WebDriver and TestNG

February 16, 2014 2 comments

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.

Advertisements

Test Automation using C#, Selenium WebDriver and NUnit

February 8, 2014 67 comments

SOFTWARE REQUIREMENTS:

  • Windows 7 as OS
  • Microsoft Visual Studio 2010 as IDE
  • NUnit as Unit Testing Tool
  • Selenium as a Test Automation Tool

SET-UP INSTRUCTIONS:

To install the Selenium WebDriver project:
  • Create a new Project with C# Class Library and save it (‘TestAutomation’ in my example).
  • Rename the class ‘Class1.cs’ to ‘Driver.cs’.
  • Download and install NuGet Package Manager using Tools -> Extension Manager. Restart MS Visual Studio in order for the changes to take effect.
  • Go to Package Manager Console install the latest version of Selenium WebDriver by running the command Install-Package Selenium.WebDriver -Version 2.20.0
  • Install the latest version of Selenium WebDriver Support Classes by running the command Install-Package Selenium.Support
 
 
Referencing the NUnit Library:
  • Download the latest NUnit framework from the site: http://www.nunit.org
  • Install the NUnit software on your machine.
  • In Visual Studio, Go to the Project -> Add Reference menu item.
  • When the Add Reference dialog appears, click on ‘Browse’ and navigate to C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\framework and select nunit.framework.dll.
  • Finally, the solution Explorer should look like this:

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:

  • Define the browser on which the tests needs to be executed in [SetUp] attribute.
  • In the [Test] attributenavigate to the google search page.
  • In 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).
  • In the [Test] attribute, input the search text and submit the form.
  • In the [Test] attribute, validate the result using Assert method.

TEST EXECUTION INSTRUCTIONS:

  • Copy and paste the code snippet below into your Visual Studio 2010.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace TestAutomation
{
[TestFixture]
 public class Driver
 {
 IWebDriver driver;

[SetUp]
 public void Setup()
 {
 // Create a new instance of the Firefox driver
 driver = new FirefoxDriver();
 }

[TearDown]
 public void Teardown()
 {
 driver.Quit();
 }

[Test]
 public void GoogleSearch()
 {
 //Navigate to the site
 driver.Navigate().GoToUrl("http://www.google.com.au");
 // Find the text input element by its name
 IWebElement query = driver.FindElement(By.Name("q"));
 // Enter something to search for
 query.SendKeys("Selenium");
 // Now submit the form
 query.Submit();
 // Google's search is rendered dynamically with JavaScript.
 // Wait for the page to load, timeout after 5 seconds
 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
 wait.Until((d) => { return d.Title.StartsWith("selenium"); });
 //Check that the Title is what we are expecting
 Assert.AreEqual("selenium - Google Search", driver.Title);
 }
 }
}
Running the tests using NUnit:
  • In Visual Studio, Go to Projects -> Project Properties (‘TestAutomation Properties’ in my example).
  • Click on the ‘Debug’ tab.
  • Set the ‘Start external program’ to the location of NUnit exe file (C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\nunit-x86.exe) as shown in the snapshot below.
  • Build the solution, Go to ‘Build -> Build Solution’ (hit the F6 key) in Visual Studio.
  • Execute the test, Go to ‘Debug -> Start Debugging’ (hit the F5 key) in Visual Studio. Visual Studio invokes the NUnit application.
  • In NUnit, click on ‘File -> Open Project’ and choose the location of the TestAutomation.dll file as shown in the snapshot below.
  • In NUnit, click on the ‘Run’ button to run the tests. Test is executed using NUnit and the result will be displayed in NUnit GUI window as shown in the snapshot below.
 
 
%d bloggers like this: