TestNG Notes
TestNG Notes
TestNG
TestNG is a popular testing framework for Java that allows users to perform
automated testing for web applications. Selenium, on the other hand, is a popular
automation testing tool that enables users to automate web browsers. Combining these
two tools allows developers to create powerful automated tests for their web applications.
Features of TestNG
Dependent methods
Dependency is a feature of Testng that allows a test method to depend on the single or
group of test methods. Dependency works on the principle "depend-on-method" which
must be either in the same class or in the inherited base class. This is the most important
feature in TestNG that tells the TestNG to run the dependent test method after the
execution of a given test method. You can also con gure whether you want dependent test
method should be executed or not even after the execution of the given test method fails.
Groups
TestNG groups allow you to group the test methods. By using TestNG groups, you can
declare the methods in a group as well as you can declare the groups within a group. The
Testng group can be used to include a certain set of groups and can exclude another set
of groups.
fi
fi
fi
fi
fi
fi
fi
Ranjeet Sir
Dependent groups
Similar to the Dependent methods, test methods in a group can depend on the test
methods of another group.
Data-driven testing
TestNG allows users to perform data-driven testing. This testing allows users to execute
the same test multiple times with multiple sets of data. To achieve the data-driven testing,
DataProvider feature is used. DataProvider is a data feeder method that executes the test
method with multiple sets of data.
Multithreaded execution
Multithreaded execution is the parallel execution of tests. Multithreading means the
execution of multiple parts of software at the same time. Based on the con guration in the
XML le, multiple threads are started, and test methods are executed in them.
Multithreaded execution saves a lot of execution time.
Better reporting
Testng provides XML and HTML reports by default for test execution. You can even add
your own custom reports when required.
fi
fi
Ranjeet Sir
What is the importance of testng.xml file?
In a Selenium TestNG project, we use testng.xml file to configure the complete
test suite in a single file. Some of the features are as follows.
• testng.xml file allows to include or exclude the execution of test methods
and test groups
• It allows to pass parameters to the test cases
• Allows to add group dependencies
• Allows to add priorities to the test cases
• Allows to configure parallel execution of test cases
• Allows to parameterize the test cases
TestNG Annotation is a piece of code which is inserted inside a program or business logic
used to control the ow of execution of test methods.
@BeforeSuite is used when we have different URLs to run your test cases. Environment
variables are set in a @BeforeSuite annotated method so that before executing all the test
cases, you need to load all the environment variables for your framework, and then it starts
executing your test cases.
The @BeforeSuite annotated method is given as the rst priority, so it is executed before
all the other test methods.
@AfterSuite: The @AfterSuite annotated method is executed after the execution of all the
test methods in the Suite. The Suite is basically a testng.xml le so we can say that
@AfterSuite annotated method is executed after the execution of an XML le.
The @BeforeSuite annotation is used to set up or start the selenium drivers while the
@AfterSuite annotation is used to stop the selenium web drivers.
@AfterTest: The test method under the @AfterTest annotated method is executed after
the execution of all the test methods of the available classes which are kept inside the tag
in the testng.xml le in the suite.
@BeforeClass: The @BeforeClass annotated method runs before the execution of test
methods in a current class.
fi
fi
fl
fi
fi
fi
fi
Ranjeet Sir
@BeforeMethod is speci c to a class not to an XML le. The @BeforeMethod annotated
method will be invoked before the execution of each test method where the test method is
nothing but a test case. Suppose there are four test methods in a class then the
@BeforeMethod annotated method is executed before the execution of each test method.
If there are four test methods, then four times @BeforeMethod annotated method will be
invoked.
TestNG allows the testers to create multiple test cases into a single group through the use
of attribute 'group' in the @Test annotation. We can say that TestNG groups allow you to
add similar functionalities in the same group. For example, student_id, student_name,
student_address are the details of a student, and all these details are added in a same
group, i.e., "student details".
Example :
package Annotations;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
fi
fi
fi
fi
Ranjeet Sir
// @Beforemethod -->Login application
// @test - GoogleTitle2
// @AfterMehtod --> Logoutfrom application
// @Beforemethod -->Login application
// @Test --> googleTitle3
// @AfterMehtod --> Logoutfrom application
// @Afterclass -->Close brwoser
// @AfterTest --> DeleteCookies
// Precondition annotations
// 1.@Beforesuite - Setup system pro
// In real time project we don't need all these annotations we use onlt
// beforeclass @beforemethod @Test @Aftermethod @Afterclas
@BeforeSuite
public void Setup() {
System.out.println("@BeforeSuite -- Setup systemproperty");
}
// 2.BeforeTest
@BeforeTest
public void URL() {
System.out.println("@BeforeTest --> Enter URL");
}
// 3.BeforeClass
@BeforeClass
public void Launchbrowser() {
System.out.println("@BeforeClass --> Launchbrowser");
}
// 4 @BforeMethod
@BeforeMethod
public void Login() {
System.out.println("@Beforemethod -->Login application");
}
// @Test annotation
@Test
public void googleTitle1() {
System.out.println("@test - GoogleTitle1");
}
@Test
public void googleTitle2() {
System.out.println("@test - GoogleTitle2");
}
// Postcondition annotation
// 5.Aftersuite
@AfterMethod
public void Logout() {
System.out.println("@AfterMehtod --> Logoutfrom application");
}
// 6.@AfterCLass
@AfterClass
public void closebrpwse() {
System.out.println("@Afterclass -->Close brwoser");
}
// 7.@AfterTest
@AfterTest
public void deletecookies() {
Ranjeet Sir
@Test
public void googletitle3() {
System.out.println("@Test --> googleTitle3");
}
Ranjeet Sir
Example 2
package TestNgFeatures;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
WebDriver driver;
@BeforeMethod
public void Setup() {
System.setProperty("webdriver.chrome.driver", "/Users/ranjeetkendre/
Documents/chromedriver");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/
login");
}
@Test
public void C() {
String Title1 = driver.getTitle();
System.out.println(Title1);
System.out.println("C");
}
@Test
public void B() {
boolean logo1 = driver.findElement(By.xpath("//img[@alt=\"company-
branding\"]")).isDisplayed();
System.out.println(logo1);
System.out.println("B");
}
@Test
public void A() {
boolean logo1 = driver.findElement(By.xpath("//img[@alt=\"company-
branding\"]")).isDisplayed();
System.out.println(logo1);
System.out.println("A");
}
@AfterMethod
public void Teardown() throws InterruptedException {
Thread.sleep(4000);
driver.quit();
}
}
Ranjeet Sir
TestNG provides the feature of enabling and disabling the test cases. We can disable a set
of test cases from getting executed. For example, consider a scenario where a serious bug
occurs in a feature due to certain tests, so we need to disable the test cases from being
executed.
The 'enabled' attribute contains the boolean value. By default, its value is true. If you want
to skip some test method, then you need to explicitly specify 'false' value.
In TestNG, you can de ne multiple test cases in a single class whereas, in Java, you can
de ne only one test in a single class in the main() method. In Java, if you want to create
one more test, then you need to create another java le and de ne the test in the main()
method.
Instead of creating test cases in different classes, we recommend you to use TestNG
framework that allows you to create multiple test cases in a single class.
You can create multiple test cases with the help of @Test annotation.
2. Priority in TestNG
Priority is an attribute that tells TestNG which order the tests need to follow. When we have
multiple test cases and want to execute them in a particular order, the TestNG priority
attribute helps in executing the test cases in that order.
• The test cases get executed in ascending order of the priority list. Thus, test cases
with lower priority get executed rst.
• One test method is allowed to have only one test priority in TestNG.
• If test priority is not de ned explicitly while running multiple cases, TestNG assigns
all cases with a Default test priority, i.e., zero (0).
Example
package TestNgFeatures;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
WebDriver driver;
@SuppressWarnings("deprecation")
@BeforeMethod
public void Setup() {
System.setProperty("webdriver.chrome.driver", "/Users/
ranjeetkendre/Documents/chromedriver");
driver = new ChromeDriver();
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
Ranjeet Sir
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15,
TimeUnit.SECONDS);
driver.get("https://opensource-demo.orangehrmlive.com/web/
index.php/auth/login");
}
@Test(priority = 1)
public void B() {
boolean logo1 = driver.findElement(By.xpath("//
img[@alt=\"company-branding\"]")).isDisplayed();
System.out.println(logo1);
System.out.println("B");
}
@Test(priority = 2)
public void A() {
boolean logo1 = driver.findElement(By.xpath("//
img[@alt=\"company-branding\"]")).isDisplayed();
System.out.println(logo1);
System.out.println("A");
}
@AfterMethod
public void Teardown() throws InterruptedException {
Thread.sleep(4000);
driver.quit();
}
}
Ranjeet Sir
Syntax
@Test (priority = 1)
public void function(){
//test code
}
package ui;
import org.testng.annotations.Test;
@Test
System.out.println("Login successful");
@Test(priority = 0)
System.out.println("Register successful");
@Test(priority = -1)
@Test(priority = 1)
@Test
@Test(priority = 1)
System.out.println("Logout successful");
In the above example, the sendEmail() test method with a negative priority. Thus, it gets
executed rst. This is followed by loginTest() and meetUp() test methods which are non-
prioritized methods that are executed based on alphabetical order ‘l’ and then ‘m’.
Next, registerTest() method is executed since it has a zero priority. This is then followed
by test methods checkCalendar() and logoutTest() methods that have the same priority.
TestNG considered the alphabetical order of their method names and
executed checkCalendar() before logoutTest() test method.
fi
Ranjeet Sir
3. dependsOnMethods
When the second test method wants to be dependent on the rst test method, then this
could be possible by the use of "dependOnMethods" attribute. If the rst test method
fails, then the dependent method on the rst test method, i.e., the second test method will
not run.
Example -
4. InvocationCount in TestNG
Invocationcount is one of the feature available in TestNG. InvocationCount is used when we want
to run the same test multiple times. If we want to run single @Test 10 times at a single thread,
then invocationCount can be used.
5. timeOut
If one of the test cases is taking a long time due to which other test cases are failing. To
overcome such situation, you need to mark the test case as fail to avoid the failure of other
test cases. The timeOut is a time period provided to the test case to completely execute its
test case.
6. groups
The 'groups' attribute is used to group the different test cases that belong to the same
functionality.
7.expectedExceptions
TestNG provides functionality to test such exception scenarios by allowing the user
to specify the type of exceptions that are expected to be thrown by a test during
execution.
fi
fi
fi
Ranjeet Sir
1. Testing Parametrization
TestNG Parameters are the arguments that we pass to the test methods. There are two
ways through which we can pass the parameters to the test methods:
◦ TestNG Parameters
◦ TestNG DataProviders
TestNG Parameters. We will learn about the parameterization in the xml le.
Suppose we want to set the global variables such url settings, username, password or API
Keys, there are some values which are constant in all the test cases, in such case we use
the TestNG Parameters.
TestNG Parameters are present in the xml le. They can be applied either inside the tag
or tag. If we want to apply the parameters to all the test cases, then the parameters are
applied inside the tag. If the parameter is speci c to a particular folder, then the parameter
is applied within a tag.
Data Provider in TestNG is a method used when a user needs to pass complex
parameters. Complex Parameters need to be created from Java such as complex objects,
objects from property les or from a database can be passed by the data provider method.
The method is annotated by @DataProvider and it returns an array of objects.
Parameters using Dataprovider
@Parameters annotation is easy but to test with multiple sets of data we need to use Data
Provider.
To ll thousand’s of web forms using our testing framework we need a different
methodology which can give us a very large dataset in a single execution ow.
This data driven concept is achieved by @DataProvider annotation in TestNG.
It has only one attribute ‘name’. If you do not specify the name attribute then the
DataProvider’s name will be same as the corresponding method name.
Data provider returns a two-dimensional JAVA object to the test method and the test
method, will invoke M times in a M*N type of object array. For example, if the DataProvider
returns an array of 2*3 objects, the corresponding testcase will be invoked 2 times with 3
parameters each time
fi
fi
fi
fi
fi
fl
Ranjeet Sir
Parallel Testing
What is Parallel Testing
In parallel testing, we test different modules or applications on multiple browsers in parallel
rather than one by one.
The parallel test execution is different from sequential testing, where we test different
modules or functionalities one after the other. Even in the case of testing applications on
multiple browsers, tests are performed sequentially on various browsers. This approach of
testing is very time-consuming.
Parallel testing helps to reduce execution time and efforts and results in a faster time to
delivery.
CrossBrowser Testing
Assertions
Assertion determines the state of the application whether it is the same what we are
expecting or not. If the assertion fails, then the test case is failed and stops the execution.
◦ Hard Assertion
◦ Soft Assertion
Question
1. What is TestNG Assert and list out common TestNG Assertions?
TestNG Asserts help us to verify the condition of the test in the middle of the test run.
Based on the TestNG Assertions, we will consider a successful test only if it is completed
the test run without throwing any exception.
Some of the common assertions supported by TestNG are
TestNG Listeners
TestNG provides the @Listeners annotation which listens to every event that occurs in a
selenium code. Listeners are activated either before the test or after the test case. It is an
interface that modi es the TestNG behavior. For example, when you are running a test
case either through selenium or appium and suddenly a test case fails. We need a
screenshot of the test case that has been failed, to achieve such scenario, TestNG
provides a mechanism, i.e., Listeners. When the test case failure occurs, then it is
redirected to the new block written for the screenshot.
We can create the TestNG Listeners in two ways. First we can use the @Listeners
annotation within the class and second way to use the within the suite.
fi
Ranjeet Sir
5. How to create and run testng.xml ?
In TestNG framework, we need to create testng.xml le to create and handle multiple test classes.
We do con gure our test run, set test dependency, include or exclude any test, method, class or
package and set priority etc in the xml le.
fi
ff
fi
fi
fi
Ranjeet Sir
View Complete Post
6. What is the importance of testng.xml le?
In a Selenium TestNG project, we use testng.xml le to con gure the complete test suite in a
single le. Some of the features are as follows.
• testng.xml le allows to include or exclude the execution of test methods and test groups
• It allows to pass parameters to the test cases
• Allows to add group dependencies
• Allows to add priorities to the test cases
• Allows to con gure parallel execution of test cases
• Allows to parameterize the test cases
7. How to pass parameter through testng.xml le to a test case?
We could de ne the parameters in the testng.xml le and then reference those parameters in the
source les.
x
Create a java test class, say, ParameterizedTest.java and add a test method
say parameterizedTest() to the test class. This method takes a string as input parameter. Add the
annotation @Parameters(“browser”) to this method.
// TestNG Interview Questions
public class ParameterizedTest {
@Test
@Parameters(“browser”)
public void parameterizedTest(String browser){
if(browser.equals(“ refox”)){
System.out.println(“Open Firefox Driver”);
}else if(browser.equals(“chrome”)){
System.out.println(“Open Chrome Driver”);
}
}
}
The parameter would be passed a value from testng.xml, which we will see in the next step.
We could set the parameter using the below syntax in the testng.xml le.
Here, name attribute represents the parameter name and value represents the value of that
parameter.
Practical Example
8. What is TestNG Assert and list out common TestNG Assertions?
TestNG Asserts help us to verify the condition of the test in the middle of the test run. Based on
the TestNG Assertions, we will consider a successful test only if it is completed the test run
without throwing any exception.
Some of the common assertions supported by TestNG are
• assertEqual(String actual,String expected)
• assertEqual(String actual,String expected, String message)
• assertEquals(boolean actual,boolean expected)
• assertTrue(condition)
• assertTrue(condition, message)
• assertFalse(condition)
• assertFalse(condition, message)
For Complete Post
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
Ranjeet Sir
9. What is Soft Assert in TestNG?
Soft Assert collects errors during @Test. Soft Assert does not throw an exception when an assert
fails and would continue with the next step after the assert statement.
If there is any exception and you want to throw it then you need to use assertAll() method as a last
statement in the @Test and test suite again continue with next @Test as it is.
Practical Example
T1(){
1 sendkeys()
2 HA –passed ! 3 else ! terminate test case / failed
3 SA — passed
4 SA — failed ! 5
5 SA – passed ! 6
6 SA – Failed
softAssert.assertAll();
}
10. What is Hard Assert in TestNG?
Hard Assert throws an AssertException immediately when an assert statement fails and test suite
continues with next @Test
11. What is exception test in TestNG?
TestNG gives an option for tracing the Exception handling of code. You can verify whether a code
throws the expected exception or not. The expected exception to validate while running the test
case is mentioned using the expectedExceptions attribute value along with @Test annotation.
@Test
t1(expectedExceptions = ElementNotFoundException){
}
12. How to set test case priority in TestNG?
We use priority attribute to the @Test annotations. In case priority is not set then the test scripts
execute in alphabetical order.
// TestNG Interview Questions
package TestNG;
import org.testng.annotations.*;
public class PriorityTestCase{
@Test(priority=0)
public void testCase1() {
system.out.println(“Test Case 1”);
}
@Test(priority=1)
public void testCase2() {
system.out.println(“Test Case 2”);
}
Ranjeet Sir
}
Output:
Test Case 1
Test Case 2
13. What is Parameterized testing in TestNG?
Parameterized tests allow developers to run the same test over and over again using di erent
values.
There are two ways to set these parameters:
• using testng.xml –
• using Data Providers –
14. How can we create data driven framework using TestNG?
By using @DataProvider annotation, we can create a Data Driven Framework.
// TestNG Interview Questions
@DataProvider(name=”getData”)
public Object[][] getData(){
//Object [][] data = new Object [rowCount][colCount];
Object [][] data = new Object [2][2];
data [0][0] = “FirstUid”;
data [0][1] = “FirstPWD”;
data[1][0] = “SecondUid”;
data[1][1] = “SecondPWD”;
return data;
}
Practical Example
15. How to run a group of test cases using TestNG?
TestNG allows you to perform sophisticated groupings of test methods. Not only can you declare
that methods belong to groups, but you can also specify groups that contain other groups. Then
TestNG can be invoked and asked to include a certain set of groups (or regular expressions) while
excluding another set. This gives you maximum exibility in how you partition your tests and
doesn’t require you to recompile anything if you want to run two di erent sets of tests back to
back.
Groups are speci ed in your testng.xml le and can be found either under the or tag. Groups
speci ed in the tag apply to all the tags underneath.
@Test (groups = { “smokeTest”, “functionalTest” })
public void loginTest(){
System.out.println(“Logged in successfully”);
}
16. How to create Group of Groups in TestNG?
Groups can also include other groups. These groups are called MetaGroups. For example, you
might want to de ne a group all that includes smokeTest and functionalTest. Let’s modify our
fi
fi
fi
fi
fl
ff
ff
Ranjeet Sir
testng.xml le as follows:
17. How to run test cases in parallel using TestNG?
we can use “parallel” attribute in testng.xml to accomplish parallel test execution in TestNG
The parallel attribute of suite tag can accept four values:
tests – All the test cases inside tag of testng.xml le will run parallel
classes – All the test cases inside a java class will run parallel
methods – All the methods with @Test annotation will execute parallel
instances – Test cases in same instance will execute parallel but two methods of two di erent
instances will run in di erent thread.
18. How to exclude a particular test method from a test case execution?
By adding the exclude tag in the testng.xml
19. How to exclude a particular test group from a test case execution?
By adding the exclude tag in the testng.xml
20. How to disable a test case in TestNG ?
To disable the test case we use the parameter enabled = false to the @Test annotation.
@Test(enabled = false)
21. How to skip a @Test method from execution in TestNG?
By using throw new SkipException()
Once SkipException() thrown, remaining part of that test method will not be executed and control
will goes directly to next test method execution.
throw new SkipException(“Skipping – This is not ready for testing “);
22. How to Ignore a test case in TestNG?
To ignore the test case we use the parameter enabled = false to the @Test annotation.
@Test(enabled = false)
fi
ff
fi
ff
Ranjeet Sir
23. How TestNG allows to state dependencies?
TestNG allows two ways to declare the dependencies.
Using attributes dependsOnMethods in @Test annotations –
Using attributes dependsOnGroups in @Test annotations –
24. What are the di erent ways to produce reports for TestNG results?
TestNG o ers two ways to produce a report.
Listeners implement the interface org.testng.ITestListener and are noti ed in real time of when a
test starts, passes, fails, etc…
Reporters implement the interface org.testng.IReporter and are noti ed when all the suites have
been run by TestNG. The IReporter instance receives a list of objects that describe the entire test
run.
25. What is the use of @Listener annotation in TestNG?
TestNG listeners are used to con gure reports and logging. One of the most widely used listeners
in testNG is ITestListener interface. It has methods like onTestStart, onTestSuccess, onTestFailure,
onTestSkipped etc. We should implement this interface creating a listener class of our own. Next
we should add the listeners annotation (@Listeners) in the Class which was created.
26. How to write regular expression In testng.xml le to search @Test methods containing
“smoke” keyword.
Regular expression to nd @Test methods containing keyword “smoke” is as mentioned below.
27. What is the time unit we specify in test suites and test cases?
We specify the time unit in test suites and test cases is in milliseconds.
28. List out various ways in which TestNG can be invoked?
TestNG can be invoked in the following ways
• Using Eclipse IDE
• Using maven/ant build tool
• From the command line
• Using IntelliJ’s IDEA
29. How To Run TestNG Using Command Prompt?
C: test
Java c://testing.jar test.java
30. What is the use of @Test(invocationCount=x)?
The invocationcount attribute tells how many times TestNG should run a test method
@Test(invocationCount = 10)
public void testCase1(){
In this example, the method testCase1 will be invoked ten times
ff
ff
fi
fi
fi
fi
fi
Ranjeet Sir
31. What is the use of @Test(threadPoolSize=x)?
The threadPoolSize attribute tells to form a thread pool to run the test method through multiple
threads.
Note: This attribute is ignored if invocationCount is not speci ed
@Test(threadPoolSize = 3, invocationCount = 10) public void testCase1(){
In this example, the method testCase1 will be invoked from three di erent threads
32. What does the test timeout mean in TestNG?
The maximum number of milliseconds a test case should take.
@Test(threadPoolSize = 3, invocationCount = 10, timeOut = 10000)
public void testCase1(){
In this example, the function testCase1 will be invoked ten times from three di erent threads.
Additionally, a time-out of ten seconds guarantees that none of the threads will block on this
thread forever.
33. What are @Factory and @DataProvider annotation?
@Factory: A factory will execute all the test methods present inside a test class using a separate
instance of the respective class with di erent set of data.
@DataProvider: A test method that uses DataProvider will be executed the speci c methods
multiple number of times based on the data provided by the DataProvider. The test method will be
executed using the same instance of the test class to which the test method belongs.
ff
fi
ff
ff
fi