TestNG Interview Questions and Answers

TestNG Testing Framework Interview Questions and Answers

1) What is TestNG?

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use.

2) What is the use of TestNG in Selenium?

Using TestNG framework in Selenium we can,

> Generate Detailed (HTML) Test Reports.

> Group Test cases.

> Parallel Test execution.

> Parameterize Tests

> Execute multiple classes / programs using XML file etc…

3) How to install TestNG in Eclipse?

In Eclipse IDE,

Help menu -> Install New Software -> Click Add

> Enter name as “TestNG”

> Enter url as: “http://beust.com/eclipse/”

> Select “TestNG”

> Next > Next > accept the Agreement > finish

4) Give a TestNG Program example?

public class TestNGexample {

@Test
public void verifyTitle() {
WebDriver driver = new FirefoxDriver();
driver.get(“http:\\gmail.com”);
String Actual = driver.getTitle();
Assert.assertEquals(Actual, “Gmail”);
}
}

Note:
> main method is not required for TestNG programs.

> TestNG program contains methods that contain @ annotations.

> If we don’t write @Test Annotation then the method won’t be executed.

TestNG for Selenium Interview Questions

5) What are the important TestNG Annotations?

Important TestNG Annotations are:

@Test – Annotation for every Test (Method)

@BeforeMethod – pre-condition for every test case in a program.
@AfterMethod – Post condition for every test case in a program.

@BeforeClass – pre condition for all test cases in a program/class
@AfterClass – post condition for all test cases in a program/class

@BeforeTest – pre condition for all test cases in multiple classes/programs
@AfterTest – post condition for all test cases in multiple classes / programs

6) How to create multiple Test cases in a program/class?

public class TestNGexample {

@Test
public void testA () {
Assert.assertEquals(“Gmail”, “Gmail”);
}

@Test
public void testC() {
Assert.assertEquals(“Gmail”, “Gmail”);
}

@Test
public void testB() {
Assert.assertEquals(“abc”, “abc”);
}
}

As per TestNG program:

A
C
B
———————
Execution Flow:

A
B
C
————————-
If you want to control the Test execution flow then use “priority” Attribute.

Syntax:

@Test (priority = number”)

Example:

public class TestNGexample {

@Test (priority=1)
public void testA () {
Assert.assertEquals(“Gmail”, “Gmail”);
}

@Test (priority=2)
public void testC() {
Assert.assertEquals(“Gmail”, “Gmail”);
}

@Test(priority=3)
public void testB() {
Assert.assertEquals(“abc”, “abc”);
}
}

7) How to execute multiple program or classes?

Using XML file we can execute multiple Java programs or classes at a time.

8) How to create XML file in TestNG?

Create XML file

Select Java Project and Right click

> New
> Other
> Enter XML and Select XML file
> Enter File Name
> Finish

9) Give an example for executing multiple programs or classes?

XML File
————-
<?xml version=”1.0″ encoding=”UTF-8″?>
<suite name = “Ecommerce suite”>
<test name =”Sanity Tests”>
<classes>
<class name=”javaExamples.Class1″/>
<class name=”javaExamples.Class2″/>

</classes>
</test>
</suite>
———————
Class 1:

public class Class1 {
@BeforeClass
public void login(){
System.out.println(“Login Successful”);
}
@AfterClass
public void logout(){
System.out.println(“Logout Successful”);
}
@Test (priority=1)
public void search(){
System.out.println(“Search Successful”);
}
@Test(priority=2)
public void advancedSearch(){
System.out.println(“Advanced Search Successful”);
}
@Test(priority=3)
public void buyProducts(){
System.out.println(“Buying Products Successful”);
}
}
——————————-
Class 2:

public class Class2 {
@BeforeClass
public void login(){
System.out.println(“Login Successful”);
}
@AfterClass
public void logout(){
System.out.println(“Logout Successful”);
}
@Test
public void accountSummary(){
System.out.println(“Account Summary Successful”);
}
@Test
public void fundTransfer(){
System.out.println(“Fund Transfer Successful”);
}
@Test
public void billPayment(){
System.out.println(“Bill Payment Successful”);
}
}
——————————–
10) Give an example for parallel Test execution?

XML File

<suite name = “Parallel Test suite” parallel = “classes” thread-count = “2”>
<test name = “Sanity Test”>

<classes>
<class name = “seleniumTests.Class1″/>
<class name = “seleniumTests.Class2″/>
</classes>

</test>
</suite>
————————
parallel = “methods”: TestNG will run all the methods in separate threads.
parallel = “classes” : TestNG will run all the methods in the same class in the same thread.
parallel = “tests” : TestNG will run all the methods in the same <test> tag in the same thread.
————————
Class 1

public class Class1 {
@BeforeTest
public void login(){
System.out.println(“Login Successful”);
}
@AfterTest
public void logout(){
System.out.println(“Logout Successful”);
}
@Test (priority = 1)
public void search(){
System.out.println(“Search Successful”);
}
@Test (priority = 2)
public void advancedSearch(){
System.out.println(“Advanced Search Successful”);
}
@Test (priority = 3)
public void buyProducts(){
System.out.println(“Buying Products Successful”);
}
@Test (priority = 4)
public void testCase(){
System.out.println(“Test Case in “+getClass().getSimpleName()
+ ” With Thread id: ” + Thread.currentThread().getId());
}
}
—————————————–
Class 2

public class Class2 {
/*@BeforeClass
public void login(){
System.out.println(“Login Successful”);
}
@AfterClass
public void logout(){
System.out.println(“Logout Successful”);
}*/
@Test (priority = 1)
public void accountSummary(){
System.out.println(“Account Summary Successful”);
}
@Test (priority = 2)
public void fundTransfer(){
System.out.println(“Fund Transfer Successful”);
}
@Test (priority = 3)
public void billPayment(){
System.out.println(“Bill Payment Successful”);
}
@Test (priority = 4)
public void testCase(){
System.out.println(“Test Case in “+getClass().getSimpleName()
+ ” With Thread id: ” + Thread.currentThread().getId());
}
}
—————
11) Give an Example for grouping Test cases?

XML File

<suite name =”suite”>
<test name =”test”>

<groups>
<run>
<include name = “sanity”/>
</run>
</groups>

<classes>
<class name = “seleniumTests.GroupTests”/>
<class name = “seleniumTests.GroupTests2″/>
</classes>

</test>
</suite>
——————————-
Java Program/Class file

@BeforeTest (groups ={“sanity”,”regression”})
public void login(){
System.out.println(“Login Successful”);
}
@AfterTest (groups ={“sanity”,”regression”})
public void logout(){
System.out.println(“Logout Successful”);
}

@Test (groups ={“sanity”})
public void search(){
System.out.println(“Search Successful”);
}
@Test (groups ={“sanity”,”regression”})
public void advancedSearch(){
System.out.println(“Advanced Search Successful”);
}
@Test (groups ={“sanity”, “regression”})
public void buyProducts(){
System.out.println(“Buying Products Successful”);
}
@Test (groups ={“regression”})
public void abcd(){
System.out.println(“Abcd Successful”);
}
@Test (groups ={“regression”})
public void xyza(){
System.out.println(“Xyza Successful”);
}
@Test (groups ={“regression”})
public void asdf(){
System.out.println(“Asdf Successful”);
}
}
————
12) Give an example for Data Driven Testing?

If you want work with excel, then download third party jar files

Ex: jxl

public class DataDriven {
@Test (dataProvider=”testdata”)
public void Addition(String val1, String val2, String val3){
int a = Integer.parseInt(val1);
int b = Integer.parseInt(val2);
int c = Integer.parseInt(val3);
int result = a + b + c;
System.out.println(result);
}

@DataProvider(name=”testdata”)
public Object [] [] readExcel() throws BiffException, IOException {
File f = new File(“C:/Users/gcreddy/Desktop/Input.xls”);
Workbook w = Workbook.getWorkbook(f);
Sheet s = w.getSheet(0);
int rows = s.getRows();
int columns = s.getColumns();
//System.out.println(rows);
//System.out.println(columns);
String Inputdata [] [] = new String [rows] [columns];
for (int i = 0; i<rows; i++){
for (int j = 0; j<columns; j++){
Cell c = s.getCell(j, i);
Inputdata [i] [j] = c.getContents();
//System.out.println(Inputdata[i][j]);
}
}
return Inputdata;
}
}

(1762)

You may also like...

%d bloggers like this: