Test NG Tutorial

Introduction of Test NG:

TEST NG is a open source test framework which is Apache Licensed.Test NG framework is derived or inspired from JUnit and NUnit frameworks and in Test NG new functionalities are added in order to easy to use.This Test NG Tutorial will give you brief idea about Test NG and how to use.

1.NG:In Test NG NG stands for Next Generation.
2.TEST NG is a testing framework and it is open source framework.
3. Purpose of Java program in Selenium is to enhance the Test scripts for AUT (Application under Test)
4.Purpose of WebDriver:To Automate Test application and recognize of elements and perform operations on elements.

Advantages of Test NG:

1.More Annotations to create Test Cases.
2.Test Cases prioritization to execute the Test cases.
3.Grouping of Number of Test cases.
4.Parallel execution of Test Cases.
5.Parameterization of Test Cases (using @DataProvider attribute).
6.We can skip Test cases while execution
7.HTML Reports generation.
8.No more Test Suite creation because testng.xml is introduced for execution.

Test NG Annotations:

Below are available TEST NG annotations where we can use in our Test Scripts.

1.@BeforeClass
2.@AfterClass
3.@BeforeTest
4.@AfterTest
5.@Test
6.@BeforeSuite
7.@AfterSuite
8.@BeforeMethod
9.@AfterMethod
10.@BeforeGroups
11.@AfterGroups

Here for Selenium WebDriver scripts we will mostly use 4 annotations inclusing Before and After,those we could learn here

@Test

@Test annotation is used in Selenium to create Test Cases in a Class,we can write number of test cases using @Test annotations,as below example.

@Test(priority=0)
public void VerifyHome(){
driver.get("http://www.wikishown.com");
String title = driver.gettitle();
Assert.assertEquals(title,"Wiki Shows - Online Information System");
}
@Test(priority=1)
public void VerifyAboutUs(){
driver.findElement(By.LinkText("ABOUT US")).click();
}

@BeforeTest


BeforeTest can be useful in Selenium script where any method should run before execution of @Test i.e before execution of Test cases under @Test methods.See example below.

@BeforeTest
public void setup(){
WebDriver driver = new FirefoxDriver();
driver.get("http://www.wikishown.com");
driver.manage().window().maximize();
}

Here in above script Selenium webdriver will initialize the driver and open the Firefox browser then it will open www.wikishown.com site and Once the website loads then browser will
maximize.

@AfterTest

AfterTest annotation can be used in Selenium scripts when ever @Test method test cases execution completes then we can close the browser and close the driver to stop the execution,see example below

@AfterTest
public void TestsClose(){
driver.quit();
driver.close();
}

In above example @AfterTest webdriver will quit the execution and driver will cloes the browser.

@BeforeClass

Same as BeforeTest but here it will run the method in BeforeClass at the very first test method execution of current class.Before running the first test method in a class BeforeClass will execute the method.See example below

Same as @BeforeTest

@BeforeClass
public void setup(){
WebDriver driver = new FirefoxDriver();
driver.get("http://www.wikishown.com");
driver.manage().window().maximize();
}


@AfterClass

Same as AfterTest once all test methods completes the test execution under @Test then it will run in the current class,see example below

@AfterClass
public void TestsClose(){
driver.quit();
driver.close();
}


Mostly we will prefer @BeforeTest and @AfterTest annotations in our Test scripts.

In Test NG some Attributes are also included some of them are with examples.

1. alwaysRun :


 It is useful in running the test methods in case it set true for alwaysRun ,in case depend means priority=0 test method fail then also it will run because alwaysRun is set as true,see example below.


@Test(priority=0)
public void VerifyHome(){
driver.get("http://www.wikishown.com");
String title = driver.gettitle();
Assert.assertEquals(title,"Wiki Shows - Online Information System");
}

@Test(priority=1 alwaysRun=true)
public void VerifyAboutUs(){
driver.findElement(By.LinkText("ABOUT US")).click();
}


Incase priority=0 is failed some cases then priority=1 will execute because alwaysRun=true

2.enabled:


Enable attributes useful in test method excution or skip the test method,in case enabled=true test method will execute the test case and in case enabled=false then that particular test case will not excute,see example below.


@Test(priority=0 enabled=false)
public void VerifyHome(){
driver.get("http://www.wikishown.com");
String title = driver.gettitle();
Assert.assertEquals(title,"Wiki Shows - Online Information System");
}
@Test(priority=1 enabled=true)
public void VerifyAboutUs(){
driver.findElement(By.LinkText("ABOUT US")).click();
}


@DataProvider:


Data-provider in TestNG framework useful in parametirization testing where using Data Provider can pass data to test method with parameters,see example below


@Test
public void Registraation(String firstName , String LastName,int mobileNo){

driver.findElement(By.name("First Name")).sendKeys(firstName);
driver.findElement(By.name("Last Name")).sendKeys(LastName);
driver.findElement(By.name("Mobile Number")).sendKeys(mobileNo);

}

}

No comments:

Post a Comment