Selenium Java Training - Session 22 - TestNG (Part 2)
Selenium Java Training - Session 22 - TestNG (Part 2)
(Part 2)
TestNG (Part 2)
• @BeforeMethod
◦Methods annotated with @BeforeMethod annotation will be executed before the @Test
annotated methods.
◦ Used with the methods having the code, which is required to be executed before
executing the code in test methods.
▪ Example: Opening Browser and Application before actually performing any tests on
them.
◦ Demonstrate a program which uses @BeforeMethod and a single @Test method -
Demonstrate here
◦ Demonstrate a program which uses @BeforeMethod and multiple @Test methods -
Demonstrate here
• @AfterMethod
◦ Methods annotated with @AfterMethod annotation will be executed after the @Test
annotated methods.
◦ Used with the methods having the code, which is required to be executed after executing
the code in test methods.
▪ Example: Closing the Application/Browser after the tests are performed on them.
◦ Demonstrate a program which uses @AfterMethod and a single @Test method -
Demonstrate here
◦ Demonstrate a program which uses @AfterMethod and multiple @Test methods -
Demonstrate here
• @BeforeClass
◦ Methods annotated with @BeforeClass annotation will be executed before executing the
Class
◦ Used with the methods having the code, which is required to be executed before
executing the Class code
▪ Example: Instead of opening application for each and every test, if we want to open the
application only once before all the tests in a Class are executed.
◦
Demonstrate a program which uses @BeforeClass along with other annotated methods -
Demonstrate here
• @AfterClass
◦ Methods annotated with @AfterClass annotation will be executed after executing the Class
◦ Used with the methods having the code, which is required to be executed after the
executing the Class code
▪ Example: Instead of closing application for each and every test, if we want to close the
application only once after all the tests inside the Class got executed.
◦ Demonstrate a program which uses @AfterTest along with other annotated methods -
Demonstrate here
• Executing the Java class files in batch using TestNG.xml
Instead of running the Classes individually, we can use testng.xml file First create multiple classes
(Say ClassOne, ClassTwo,Class Three, ClassFour etc) having @Test methods and execute the
classes individuallyCreate a testng.xml file in the Project and execute all the classes at a go using
testng.xml fileRight click on the Project > Select TestNG > Convert To TestNGSuite name in testng
file - suite tag - say paymentsTest module name in testng file - test tag - say netbankingCreate
multiple test modules in testng.xml fileWe can specify a group of classes under classes and class
tags
• @BeforeTest
• @AfterTest
• @BeforeSuite
• @AfterSuite
• Commenting in testng.xml file
• Excluding a method from execution by changing the testng.xml file
<class name="demopack.DemoMavenProject.Demo"> <methods> <exclude
name="demoMethodTwo"/> </methods> </class>
• Or, we can include a specific method that only needs to executed by changing the testng.xml file
<class name="demopack.DemoMavenProject.Demo"> <methods> <include
name="demoMethodTwo"/> </methods> </class>
• Exclude or include with regular expressions
<class name="demopack.DemoMavenProject.Demo"> <methods> <include name="demo.*"/>
</methods> </class>
• Executing the tests at package level
Under test tags, specify the package name - <test> <packages> <package name="xyz"/>
</packages> </test>
• Groups
include a group - view hereexclude a group (Simply change include to exclude in the above
example here)@Test(groups={"smoke"})
• More attributes
dependsOnMethods - before executing the test, the dependent tests will be executed - View
hereenabled=false to stop it from executing- View heretimeOut=5000 - View here
By,
Arun Motoori
Arun Motoori