TestNG is broadly employed in Selenium for automation, as most of you already know. It’s an automated structure that’s important for all testers to understand, particularly the annotations used in conjunction with TestNG.
In plain language, TestNG annotations can be described as lines of code that are added within our program or business logic to govern the execution process of the methods beneath.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
This article will primarily explore the importance and applications of various types of annotations.
Below is an outline of the setup I used for my project. You don’t have to strictly follow this for your own project.
Your specific project requirements dictate the choice of annotations. However, the flow of their execution remains constant.
Prerequisites:
- TestNG installed in Eclipse. Refer to this step-by-step guide for installation.
- JDK – Java Development Kit
- Java 1.5 version or above is required to utilize annotations
Before crafting a new test script or setting up a project, you should first grasp the order in which annotations are arranged. This order is unaltered during execution.
For instance, when you compile and run the below script, you’ll notice that the execution order follows this sequence:
- BeforeSuite
- BeforeTest
- BeforeClass
- BeforeMethod
- Test Case 1
- AfterMethod
- BeforeMethod
- Test Case 2
- AfterMethod
- AfterClass
- AfterTest
- AfterSuite
Example:
public class test { @BeforeMethod public void beforeMethod() { System.out.println(" Before Method will execute before every test method"); } @AfterMethod public void afterMethod() { System.out.println("After Method will execute after every test method "); } @BeforeClass public void beforeClass() { System.out.println("Before Class will always execute prior to Before Method and Test Method "); } @AfterClass public void afterClass() { System.out.println("After Class will always execute later to After Method and Test method"); } @BeforeTest public void beforeTest() { System.out.println("Before Test will always execute prior to Before Class, ,Before Method and Test Method "); } @AfterTest public void afterTest() { System.out.println("After Test will always execute later to After Method, After Class "); } @BeforeSuite public void beforeSuite() { System.out.println(“Before Suite will always execute prior to all annotations or tests in the suite."); } @AfterSuite public void afterSuite() { System.out.println("After suite will always execute at last when all the annotations or test in the suite have run."); } @Test public void testCase1() { System.out.println("This is my First Test Case 1"); } @Test public void testCase2() { System.out.println("This is my Second Test Case 2"); } }
The following steps encapsulate the test script process:
- Write your test’s business logic and include the above TestNG annotations in your code
- Include the details about your test (e.g., the class name, groups, methods you plan to execute, etc.) in a testng.xml file.
- Run TestNG
Nevertheless, the question remains: What kind of content should go in these annotations?
Following, let’s examine some important actions that can be achieved with the annotations:
#1) @Test
Our automation script will house its primary business logic in this section. This is where we pen down everything we want to automate. We can also pass attributes to our test method.
Below are the attributes that can be passed to our Test method:
- alwaysRun : This is deployed if we need to ensure that a method is executed consistently, even if the method’s parameters fail. When set to true, this test method will always be run. E.g., @Test(alwaysRun = true)
- dataProvider: TestNG dataProvider is used when we have to provide data for parameterization. E.g., @Test(dataProvider = “Hello”).
- dataProviderClass: This class sends the data to the data provider. In our instance, the dataProvider class name is “Hello”.
- dependsOnGroups: It is a set of groups that the method is dependent on. E.g., @Test (groups = { “City” ,”State” })
- dependsOnMethods: This command is utilized for method execution which is based on a dependent method. E.g., @Test (dependsOnMethods = { “OpenBrowser” ,”database is up” })
- description: It is the method’s description. E.g., @Test(description = “test method”)
- invocationCount: It signifies the number of times a method should be invoked, working like a loop. E.g., @Test(invocationCount = 7). As such, this method will be executed seven times.
- invocationTimeOut: This attribute signifies the maximum number of milliseconds all the invocationCount should take to complete. This attribute will be ignored if invocationCount is unspecified. E.g., @Test(invocationCount =7,invocationTimeOut = 30 )
- priority: This command sets the preference of the test method. Methods with a lower priority level will be scheduled first. E.g., @Test(priority =1 )
#2) @BeforeSuite and @AfterSuite
The @BeforeSuite annotated method is where you can setup and start selenium drivers, and use the @AfterSuite annotated method to stop Selenium drivers.
Example:
public class TestSuiteSetup () { @BeforeSuite(alwaysRun = true) public void setupSuite() { WebDriver driver = new FirefoxDriver(); } @AfterSuite(alwaysRun = true) public void tearDown() { driver().close(); } }
#3) @BeforeClass and @AfterClass
In the @BeforeClass annotated method, you can set up your Firefox properties, initialize your driver, and more. Use the @AfterClass annotated method to stop the driver.
Example:
@BeforeClass(description = "Set capabilities for your Firefox browser and set the time it should wait for a page to load.") public static void firefoxSetUp() throws MalformedURLException { DesiredCapabilities capability = DesiredCapabilities.firefox(); driver = new FirefoxDriver(capability); driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); driver.manage().window().setSize(new Dimension(1920, 1080)); } @AfterClass(description = "close your firefox driver") public void afterclass(){ driver.close(); }
#4) @BeforeMethod and @AfterMethod
Using the @BeforeMethod annotated method, you can verify the database connection before the execution of your test method. The @AfterMethod annotated method can be used to terminate your database connection.
Example:
@BeforeMethod(description="connect to the database") public void beforemethod() throws SQLException{ //check if the database connection is up String databaseurl = "jdbc:oracle://192.168.1.258/myDB"; DriverManager.getConnection(databaseurl, "username", "password"); }
@AfterMethod(description="close the database connection") public void aftermethod() throws SQLException{ //check if the database connection is closed String databaseurl = "jdbc:oracle://192.168.1.258/myDB"; Connection connect = DriverManager.getConnection(databaseurl, "username", "password"); if(connect!=null) connect.close(); }
#5) @BeforeTest and @AfterTest
The @BeforeTest method is where you can establish your Firefox profile preferences, while the @AfterTest method can contain code to generate the test result and email it to relevant stakeholders.
Example:
@BeforeTest (description="set your Firefox profile preferences according to your project requirements") public void single_run(){ FirefoxProfile firefoxProfile = new FirefoxProfile(); firefoxProfile.setPreference ("browser.download.folderList",2); firefoxProfile.setPreference ("browser.download.manager.showWhenStarting",false); firefoxProfile.setPreference ("browser.download.dir","E:reports"); firefoxProfile.setPreference ("browser.helperApps.neverAsk.saveToDisk","csv"); driver = new FirefoxDriver(firefoxProfile); String baseUrl = "www.gmail.com"; } @AfterTest (description="") public void teardown(){ //a code that will send the test details report }
An essential prerequisite while using these annotations is ensuring your system operates on Java 1.5 version or higher. Or else, Eclipse might give an error pointing out that annotations aren’t supported on your system.
Let’s imagine a scenario where despite having the appropriate version of Java required to handle annotations, you’re still encountering errors.
Something like the following:
Syntax error, annotations are only accessible if the source level is 1.5 or above.
What do we do next? There are three possible ways to address this issue.
Let’s examine them one at a time:
Option #1:
- Go to Eclipse and right-click on your project
- Choose Properties
- Select Java Compiler
- Ensure your Compiler Compliance Level is 1.5 or higher
- Save your settings, and voila, the problem is resolved.
Option #2:
- Go to the Window tab in Eclipse
- Pick Preferences
- Click on Java followed by Compiler
- Ensure your Compiler Compliance Level is 1.5 or higher
- Save your settings, and the issue will disappear.
Option #3:
Verify your Java Home Path by setting the correct Java environment path variable.
Conclusion:
In this post, I’ve outlined some crucial annotations and attributes that are frequently employed by testers. However, there are additional, less commonly used annotations in TestNG, such as @AfterGroups, @BeforeGroups, etc., which are useful when dealing with groups in your project test script.
So, employ the above annotations as per your needs. It’s typically recommended not to conduct your project setup within the test method. Instead, the test method should be reserved for writing the crucial business logic meant for testing.
Ensure your system runs on Java 1.5 version or higher is critical; otherwise, Eclipse may display an error indicating that your system doesn’t support annotations.
I hope this article proves helpful in understanding TestNG annotations. Feel free to share your thoughts, comments, or questions.