Monday, 18 February 2013

Configure WebDriver using TestNG and ReportNG

This post contains the information for configure the Eclipse with Selenium Webdriver, TestNG, and ReportNG. 

Pre-Requisite –

Java JDK should be installed on system. To verify and set the JAVA_HOME, follow the below steps –
For verification of Java installation on System - 

                C:\Users>java -version
java version "1.7.0_06"
Java(TM) SE Runtime Environment (build 1.7.0_06-b24)
Java HotSpot(TM) Client VM (build 23.2-b09, mixed mode, sharing)
               
Configure Eclipse -

  • Download Eclipse (eclipse-SDK-4.2-win32.zip) from the site www.eclipse.org.
  • Extract the zip file and put the eclipse folder in any directory (In my case – D:\eclpse for Windows).
  • Launch the eclipse.exe file. Shows below screen.
 
  • Enter the workspace location and Click on Ok button.
  • Eclipse editor will launch.  Close the Welcome screen. Shows below screen.


Configure WebDriver + TestNG + ReportNG in Eclipse -
1)      Go to Help->Install New Software..
2)      Shows below Available Software screen.
3)      Click on Add button. Enter below TestNG repository information.
4)      Click on Ok button. Shows below screen.
5)      Select TestNG check box. Click on Next button. Shows below screen.
6)      Click on Next button. Shows below screen –
7)      Accept the license and click on Finish button. Shows below screen –
8)      After few minutes, TestNG plugin install completes. You need to restart the eclipse for updating the installed plugin.
9)      Launch the eclipse again. Click File > New > Java Projects.
10)   Shows below screen.  Enter the project name (ex – TestLink) and click on Finish.
11)   New project will create. Shows below screen.
12)   Right click on the project name and Select New->Folder. Create a folder “lib” under the project name.
13)   Download the below jar files and placed under above created “lib” folder.
selenium-java-2.29.0.jar
selenium-java-standalone-2.29.0.jar
testng-6.5.2.jar
reportng-1.1.3.jar
guice-3.0.jar
velocity-dep-1.4.jar
14)   Right click on project name and select Properties. Click on Java Build Path.
15)   Click on Libraries tab.
16)   Click on Add External Jars. Add all the jar files from above create “lib” folder. It looks like the below screen –
17)   Click on Ok button.

TestNG Script Execution –
1)      Execute the script testNGExample.java
Script Name - testNGExample.java
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class testNGExample
{
                public static WebDriver driver;
                public static WebElement tlElement;
               
                @BeforeTest
    public void startDriver()
                {
                                driver= new FirefoxDriver();
                                driver.get("http://localhost/testlink/login.php");
                                driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
                }
                @Test
    public void test1()
                {
        System.out.println("@Test test1()");
        driver.findElement(By.xpath("//input[@name='tl_login']")).sendKeys("admin");
                                driver.findElement(By.xpath("//input[@name='tl_password']")).sendKeys("admin");
                                driver.findElement(By.xpath("//input[@name='login_submit']")).click();
    }
                 
    @Test(dependsOnMethods="test1")
    public void test2()
    {
                System.out.println("@Test test2()");
                driver.switchTo().frame("mainframe");
                                driver.findElement(By.xpath("//a[contains(text(),'Test Project Management')]")).click();
                                driver.findElement(By.xpath("//input[@name='create']")).click();
                                tlElement=driver.findElement(By.xpath("//input[@name='active']"));
                                tlElement.click();
    }
@AfterTest
    public void stopDriver()
    {
                                driver.switchTo().defaultContent();
                                driver.switchTo().frame("titlebar");
                                driver.findElement(By.xpath("//a[contains(text(),'Logout')]")).click();
                                driver.quit();
  }
}

2)      Right Click on script and select Run As->TestNG Test.
3)      TestNG Report (Check the file “emailable-report.html” under test-output folder. You can also check the file “index.html”)-

TestNG+ReportNG script execution
1)      Get the “reportNG.xml” file and placed under the above created project folder. It looks like below –
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite verbose="0" name="TestLink Suite">
<listeners> 
    <listener class-name="org.uncommons.reportng.HTMLReporter"/>
 </listeners>
<test verbose="2" name="TestProjectManagement" preserve-order="false">
<classes>
                <class name="reportNGExample"/>
</classes>
</test>
</suite>

2)      Now time to run the script. We have used the “reportNGExample.java” script.
Script Name - reportNGExample.java
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.Reporter;


public class reportNGExample
{
                public static WebDriver driver;
                public static WebElement tlElement;
               
                @BeforeTest
    public void startDriver()
                {
                                driver= new FirefoxDriver();
                                driver.get("http://localhost/testlink/login.php");
                                driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
                                Reporter.log("1.Open Login screen");
                }
                @Test
    public void test1()
                {
        System.out.println("@Test test1()");
        driver.findElement(By.xpath("//input[@name='tl_login']")).sendKeys("admin");
        Reporter.log("2.Enter user name");
                                driver.findElement(By.xpath("//input[@name='tl_password']")).sendKeys("admin");
                                Reporter.log("3.Enter Password");
                                driver.findElement(By.xpath("//input[@name='login_submit']")).click();
                                Reporter.log("4.Login into TestLink");
    }
                 
    @Test(dependsOnMethods="test1")
    public void test2()
    {
                System.out.println("@Test test2()");
                driver.switchTo().frame("mainframe");
                                driver.findElement(By.xpath("//a[contains(text(),'Test Project Management')]")).click();
                                Reporter.log("1.Click on Project Management");
                                driver.findElement(By.xpath("//input[@name='create']")).click();
                                Reporter.log("2.Click on create button");
                                tlElement=driver.findElement(By.xpath("//input[@name='active']"));
                                tlElement.click();
                                Reporter.log("3.Click on Active checkbox");
    }
               
                @AfterTest
    public void stopDriver()
    {
                                driver.switchTo().defaultContent();
                                driver.switchTo().frame("titlebar");
                                driver.findElement(By.xpath("//a[contains(text(),'Logout')]")).click();
                                driver.quit();
    }
}

3)      For executing this, right click on the “testNG.xml” and select “TestNG Suite”.
4)      Verify the report. Go to the project Folder/test-output/html. Open the “index.html”. The report looks like the below screen




No comments:

Post a Comment