Test NG
Test NG
TestNG
What is TestNG?
TestNG is an automation testing framework in which NG stands for “Next
Generation”.TestNG is inspired by Junit which uses the annotations(@).
Features of TestNG we can use in selenium
-WebDriver has no native mechanism for generating reports. TestNG can generate the
report in a proper and readable format.
-Multiple test cases can be grouped more easily.
-The same test case can be executed multiple times without loops.
-Using TestNG , you can execute multiple test cases on multiple browsers.
-The TestNG framework can be easily integrated with tools like TestNG Maven, Jenkins ,
etc.
-Annotations used in the testing are very easy to understand ex:@BeforeMethod ,
@AfterMethod, @BeforeTest, @AfterTest
Points to remember for writing TestNG testcase.
1.TestNG does not require you to have a main() method.
2.Methods need not be static.
3.We used the @Test annotation . @Test is used to tell that the method under it is a test
case.
4.We needed to import the package org.testng.annotations.*.
5.We used the Assert class.
The Assert class is used to conduct verification operation in TestNG.
Assertions in TestNG are a way to verify that the expected result matches the actual result.
To use it , we need to import the org.testng.Assert package.
2.TestXML File
What is TestNG XML File?
In TestNG framework, we need to create Testng xml file to run and handle multiple test
classes. TestNG.xml file which contains all the test configuration and this XML file can be
used to run and organize our test.
In testing.xml file, we configure our test run, set test dependency, include or exclude any
test, method, class or package and set priority etc.
Testng.xml file is also is used for TestNG Parameters. TestNG Parameters are the arguments
that we pass to the test methods.
@BeforeSuite: The annotated method will run before all tests in this suite have.
@AfterSuite: The annotated method will run after all tests in this suite have.
@BeforeTest: The annotated method will run before any test method belonging to the
classes inside the <test> tag is run.
@AfterTest: The annotated method will run after all the test methods belonging to the
classes inside the <test> tag have run.
@BeforeGroups: The list of groups that this configuration method will run before. This
method is guaranteed to run shortly before the first test method that belongs to any of
these groups is invoked.
@AfterGroups: The list of groups that this configuration method will run after. This method
is guaranteed to run shortly after the last test method that belongs to any of these groups is
invoked.
@BeforeClass: The annotated method will run before the first test method in the current
class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current
class have been run.
@BeforeMethod: The annotated method will be run before each test method.
@AfterMethod: The annotated method will be run after each test method.
@Test: This we need to use for test cases
@DataProvider: Marks a method as supplying data for a test method. The annotated
method must return an Object[][] where each Object[] can be assigned the parameter list of
the test method. The @Test method that wants to receive data from this DataProvider
needs to use a dataProvider name equals to the name of this annotation.
Assignment :
Test Data
Keyword :
1.selenium
2.Java
Test Steps :
Launch the browser and open wee.google.con
Add the first keyword as input in the search box .
Verify the input value on UI to be same as from test data
Repeat the above two steps for the other 2 keywords.
TestNG will automatically try to convert the value specified in testing.xml to the type of your
parameter.
Here are the types supported.
String
Int/Integer
Boolean/Bollean
byte/Byte
char/Chracter
double/Double
float/Float
long/Long
short/Short
7.Data Providers
Similar to TestNG Parameters, DataProviders are a means to pass data to test methods in
TestNG. Using DataProvider in TestNG, we can easily inject multiple values into the same
test case. It comes inbuilt in TestNG and is popularly used in data-driven frameworks.
Syntax:
@DataProvider(name=”name of the data provider”)
Public Object[][]daaProviderfunc()
{
return new Object[][]{values}
}
The DataProvider annotation has a single attribute called name, which you can select as per
your convenience.
DataProviders are separate methods used in test functions, which means that this
annotation is not used on test functions like the testNG parameters .
The DataProvider method retures a 2D list of objects.
In case you do not define a name for the DataProvider, the DataProvider method name is
considered its default name. So, the name of the DataProvider calls the DataProvider
method.
Test Scenario:
1.Launch the browser to open www.google.com
2.Search the first keyword combination.
3.Repeat the steps 2 for the other 2 keywords combination.
Search Key Words :
Country Monument
India Qutub Minar
Agra Taj Mahal
Hyderabad Charminar
8.Parallel Testing:
Parallel testing or parallel execution, as the name suggests, is a process of running the test
case parallelly rather than one after the other.
We can execute –
All the methods with @Test annotation will execute in parallel.
All the test cases inside a java class will run parallel methods.
All the test cases inside <test> tag of the Testing xml file will run parallel.
Where can we apply Parallel Test execution in TestNG?
Methods: This will run the parallel tests on all @Test methods in TestNG.
Tests: All the test cases present inside the <test> tag will run with this value.
Classes: All the test cases present inside the classes that exist in the XML will run in parallel.
Instances: This value will run all the test cases parallelly inside the same instance.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name = "Parallel Testing Suite" parallel = "methods" thread-count=”2”>
<test name = "Parallel Tests"
<classes>
<class name = "ParallelTest" />
</classes>
</test>
</suite>
9.Listeners
It is interface in TestNG.
Types of Listeners in TestNG
1. lAnnotationTransformer
2. lAnnotationTransformer2
3. ICinfigurable
4. IConfigurationListener
5. IExecutionListner
6. IHookable
7. IInvokedMethodListner
8. IInvokedMethodListner2
9. lMethodlnterceptor
10. IReporter
11. ISuiteListner
12. ITestListner – It is very popular
ITestListners in TestNG
ITestListener:
Based on the execution/result of the test cases if you can perform some certation action like
pass,fail,skipped the you can use listeners .
This is the most frequently used TestNG listener. ITestListener is an interface implemented
in the class , and that class overrides the ITestListener defined methods. The ITestListener
listens to the desired events and executes the methods accordingly.
Note : If you can use Listeners in TestNG XML file then not need to mentation in class .
10.Batch Testing:
What is Batch Testing:
Running multiple test cases in a suite is called Batch Testing.
A test suite is a collection of the test cases. Test suites help in grouping test cases.
You can categorize test suites based on functionality, module, environment, or something
else.
In TestNG we can use XML file to perform Batch Testing.
We can use below test suite for Batch Testing.
11.Assertion
Assertion in TestNG are used for validating the test methods and to verify that expected
result and the actual result matched or not.
Types of Assertions:
1. Hard Assertion
2. Soft Assertion
Hard Assertion:
Hard Assertion is an Assertion that immediately throws the AsserException when the test
case is failed.
A Hard Assertion contains the following methods.
Soft Assert:
Soft assert does not throw an exception immediately when the assertion is failed, collects
then and carries out with the next validation. This accumulates the errors in each @Test
execution.
To use testing soft assertion, you have to use testing SoftAssert class.
Test Scenario:
1. Launch Chrome browser.
2. Open URL-https://testautomationpractice.blogspot.com/
3. Verify Title of the webpage.
4. Verify the presence of the Wikipedia icon on web page.
5. Verify the presence of Wikipedia search button web page.
12.InvocationCount
Run Test cases multiple time without using loop.
@Test annotation – invocationCount attribute
In TestNG, InvocationCount attribute is used to run single test case multiple time.
Syntax:
@Test(invocationCount=null)
Where num=number of times you want to run this test method.
16.TestNG Reports:
TestNG Reports are the default HTML reports which are generated once the test cases are
executed using TestNG.
The TestNG will generate the default report.
-index.html
-emailable-report.html
-Report class – Used to log information in reports.
Note: Selenium web driver is used for automation the web-application, but it won’t
generate any reports.
Reporter Class Syntax:
Reporter class of TestNG provides us with four different methods to log information isn’t
that interesting? Here are some methods:
-Reporter.log(String s);
- Reporter.log(String s,Boolean logToStandardOut);
- Reporter.log(String s, int level);
- Reporter.log(String s,int level,Boolean logToStandardOut);
1. Reporter.log(String s); : This method logs the string passed into your HTML Report.
Parameters : S – The message to be logged.
2. Reporter.log(String s,Boolean logToStandardOut); :This method logs the string
passed into your HTML Report.
Additionally, it also prints the same message on your console if logToStandardOut is
set to TRUE.
Parameters :
S – The message to be logged.
logToStandardOut – Print the message on standard output.
3. Eporter.log(String s , int level); : This method logs the string passed into your HTML
Report if the current verbosity equals or is greater than the one passed in the
parameter.
Parameters :
S – The message to be logged.
level –The verbosity of the message to be logged.