Java: Guide: TestNG Workbook 2012

TestNG Workbook(2012)

Links

http://qtpselenium.com/selenium-tutorial/junit-selenium-training/

Eclipse Setup

  • testng.org/doc/download.html
  • Show>View>TestNG

 

 

 

 

Creating a New TestNG Class

 

  • New > TestNG Class

 

 

Resulting Code

package test;

 

import org.testng.annotations.Test;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.BeforeTest;

 

public class YahooTest {

  @Test

  public void f() {

  }

  @BeforeMethod

  public void beforeMethod() {

  }

 

  @BeforeTest

  public void beforeTest() {

  }

 

}

 

 

Annotations

 

 

@BeforeSuite

@BeforeTest

@BeforeMethod

@Test

@AfterMethod

@AfterTest

@AfterSuite

 

 

Running Test Suites with tesing.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Yahoo">

    <test name="Yahoo Mail Test">

            <classes>

                    <class name="test.YahooTest" /></classes>

    </test>

    <test name="Yahoo News Test">

            <classes>

                    <class name="test.YahooNewsTest" /></classes>

    </test>

</suite>

 

Skipping Tests

        @BeforeTest

    public void xyz(){

            System.out.println("Before Executing Yahoo News Test");

            throw new SkipException("Skipping the test because of some reason");

    }

 

Parameterization of Tests

@DataProvider

@Test(dataProvider=”RegisterData”)

 

        @DataProvider

    public Object[][] registerData(){

            //rows - number of iterations

            //cols - data for each test

            Object[][] data=new Object[3][4];

           

            // first row

            data[0][0]="user1";

            data[0][1]="pass1";

            data[0][2]="email1";

            data[0][3]="city1";

 

            // second row

            data[1][0]="user2";

            data[1][1]="pass2";

            data[1][2]="email2";

            data[1][3]="city2";

 

            // third row

            data[2][0]="user3";

            data[2][1]="pass3";

            data[2][2]="email3";

            data[2][3]="city3";

           

            return data;

 

        @Test(dataProvider="registerData")

    public void testRegister(String username, String password, String email, String city){

            // selenium code

            // username

            // password

            // email

            // city

           

            System.out.println(username + " -- " + password + " -- " + email + " -- " + city);

 

 

 

Assertions

 

Assert.assertEquals - message if eval is false

Assert.assertTrue - message if true

Assert.assertFalse - message if false

            Assert.assertEquals("Good", "Bad");

            Assert.assertTrue(6>1, "Error message");

            Assert.assertFalse(1>9, "Error message");

 

 

 

Selenium WebDriver

 

ANT with TestNG

 

  • Saxon
  • This has SaxonLiaison.jar
  •  
  •  
  • ReportNG
  • XSLT XSL
  • Another Ant Build.xml link
  • Many Jars
  •  
Tags