Wednesday, February 27, 2013

Selenium : Batch Execution in TestNG

Batch Execution in TestNG

I will be discussing on a Simple partial framework in using Eclipse Helios.

Pre-Requistes:
  1. You should have Eclipse Helios.
  2. TestNg plug-in should be installed to Eclipse.

Step 1. Framework skeleton

  1. Create a project with any name inside eclipse.
  2. Inside that project create a package by name "package_name" .
  3. Inside "package_name" create 3 Java files (Class Files) of the following names :- a) Initial  b) Sample  c) Sample2

Step 2 .Create "testng.xml"

Here is a code that you use :- 

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

<suite name="Can Be any name" verbose="1" parallel="false" >

<test name="Test name is Some Name">
<classes>
<class name="package_name.Initial" />
</classes>
</test>


<test name="Test name is Some Other Name">
<classes>
<class name="package_name.Sample" />
</classes>
</test>


<test name="Test name is Another Name">
<classes>
<class name="package_name.Sample2" />
</classes>
</test>

</suite>

  1.  Paste this code in text file.
  2. Save as "testng.xml".
  3. Drag and Drop this file in the project you created in Eclipse.

 

Step 3   Code in a) Initial  b) Sample  c) Sample2

Copy paste the following codes in each of the files .

----------------------------Initial.Jar ----------------------------------------
package package_name;

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

public class Initial
{

@BeforeSuite//Can only be used 1 Time in the Suite ,executes the very first
public void Initialization()
{
System.out.println("-----------@BEFORESUITE---------------\n");
}

@AfterSuite//Can only be used 1 Time in the Suite ,executes the very end
public void Destroy()
{
}
}


---------------------------------------Sample.jar---------------------------------------------------
package package_name;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class Sample
{
@BeforeTest //BEFORE TEST
public void SampleBeforeTest()//Executed only 1 at the beginning of the file
{
System.out.println(" BeforeTest--Sample ");
}
@BeforeMethod //BEFORE METHOD
public void SampleBeforeMethod()//, Executed each time before test
{
System.out.println("BeforeMethod--Sample ");
}

@Test (dataProvider = "registerData")//Parameterized data
public void SampleTest1A(String Some_Data)
{
System.out.println(Some_Data);

}
@DataProvider //Parameterization
public Object[][] registerData()
{
Object[][] Data=new Object[2][1];// execute 2 times , 1 data each time
//Rows = No of times test should be repeated
//Columns = No of data
Data[0][0]="SampleTest1A--Sample , Parameterized !!";
Data[1][0]="SampleTest1B--Sample , Parameterized !!";
return Data;
}

@AfterTest //AFTER TEST
public void SampleAfterTest()//Executed only once in this file after this file is completed
{
System.out.println(" AfterTest--Sample \n");

}
@AfterMethod //AFTER METHOD
public void SampleAfterMethod()//, Executed each time after every test is executed
{
System.out.println("AfterMethod--Sample \n");
}
}

---------------------------------------------Sample2.jar--------------------------------------------

package package_name;
import org.testng.Assert;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Sample2
{
@BeforeTest //@BeforeTest runs only 1 time before test
public void SampleBeforeTest2()
{
System.out.println(" BeforeTest--Sample2\n");
//throw new SkipExecution("Skipping test");
}
@BeforeMethod //@BeforeMethod runs each time before @test
public void SampleBeforeMethod2()
{
System.out.println("BeforeMethod--Sample2");
}
@Test // @Test runs only once
public void SampleTest2A()
{
System.out.println("SampleTest2A--Sample2 ");
}
@Test //@Test runs only once
public void SampleTest2B()
{
System.out.println("SampleTest2B--Sample2");
throw new SkipException("Skipping test -- SampleTest2B");
}
@AfterTest //@AfterTest runs only 1 time at the end .
public void SampleAfterTest2()
{
System.out.println(" AfterTest--Sample2\n");
System.out.println(" ---Over of Sample2--- \n");
}
@AfterMethod //@AfterMethod runs each time After @test
public void SampleAfterMethod2()
{
System.out.println("AfterMethod--Sample2 \n");
}
}


Step 4 Run the Skeleton.

  1. Right click on the testng.xml file and  Runas>TestNG test.
  2. Press f5 (Refresh) or Right click on Package Explorer>Refresh
  3. You should be able to see "test-output" folder
  4. " test-output>index.html " gives result in html format.

No comments:

Post a Comment