TestNG Annotations in Selenium WebDriver

TestNG Annotations in Selenium WebDriver –

TestNG Annotations are the repeated word used by developers; They are a piece of code that is inserted into the program or business logic used to control the flow of methods in java. Annotations play a major role in TestNG – The abbreviation for TestNG is Test Next Generation most of us know it is an automation framework widely used by Selenium. As I said before annotation plays an important role in TestNG, Testers need to understand the working and uses of each annotation while they are working on TestNG.

In this tutorial, I will focus on different types of TestNG annotations for Selenium Webdriver and their uses with the example. Before getting into this topic, we will know a few points about Selenium Webdriver.

What is Selenium Webdriver?

It is a group of open source API’s used for automating web application testing. The new feature of selenium is the inclusion of the WebDriver API. Selenium Webdriver was developed for the better support on the dynamic web pages.

Selenium 1.0 + Webdriver = Selenium 2.0

The main advantage of selenium web driver is they are simple to use, and programming interface is easy to understand with basic knowledge of programming languages and improved support on web-app testing problems.

Prerequisites to Write Test with TestNG?

  • Install the TestNG in Eclipse.
  • Java Development Kit(JDK)
  • Need To Add TestNG Maven dependency for your pom.xml
  • Start Writing test scenario with the help of TestNG Annotations (Note: Annotations can be used from java version 1.5 or higher versions)
  • Convert your test code into the testng.xml file
  • Compile and run your test.

List TestNG Annotations

Annotations differ from project to project depending on their requirement. Though the requirement changes the flow of execution will be the same for every single project. Here, I will list out you the TestNG Annotations and will explain one by one with examples.

  1. @BeforeSuite
  2. @BeforeTest
  3. @BeforeClass
  4. @BeforeMethod
  5. @Test
  6. @AfterMethod
  7. @AfterClass
  8. @AfterTest
  9. @AfterSuite

Workflow of TestNG is shown below:

The below workflow will be in this process, Here @Test is the base annotation in this TestNG workflow. Continuous with @Method which executes before and after execution of @Test. Now, @Class will executes before and after the execution of @Method and so on.

<BeforeSuite>
     <BeforeTest>
           <BeforeClasses>
                 <BeforeMethod>
                            <Test>
                </AfterMethod>
          </AfterClasses>
      </AfterTest>
</AfterSuite>

Now, Let see one by one from the list of Annotations:

1) @Test

In any automation script Test annotation is the important part, where we write code/business logic. If you need to automate something we need to insert that particular code into the test method, Where this test method executes Test by passing attributes. Here are some attributes which are used to pass in the test methods.

# dependsOnGroups: In this attribute, we can get a group of the list to a particular method depends on.

Example: @Test (groups = { “Organization” ,”Employee” })

# alwaysRun: This attribute can be used whenever we get a situation to run method continuously, even if the parameters of the process fails.

Example: @Test(alwaysRun = true)

# dataProviderClass: dataProviderClass is class used to provide the data to the dataProvider, So let’s give the class name as “Computer.”

# dataProvider: It is used for providing any data to the parameterization.

Example: @Test (dataProvider = “Computer”)

# dependsOnMethods: methods are used to execute it’s dependent method, In the same way, dependsOnMethods works.

Example: @Test (dependsOnMethods = { “start”, “init” })

2) @BeforeMethod and @AfterMethod

In @BeforeMethod allows the method to execute before the execution of each @test methods, Whereas @afterMethod is executed after the execution of each @test methods. In this

Code:

@BeforeMethod

public void accountLogin()
{

System.out.println(“Account has been logged in”)
}

@AfterMethod

public void accountLogout ()
{

System.out.println(“Account has been logged out”)
}

@test(priority=0)
public void updateProfile()
{

System.out.println(“Profile has been updated using the updateProfile method”)

}

@test(priority=1)
public void bankBlance()
{

System.out.println(“Bank balance will be shown using the bankBlance method” )
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Output:

As, I as said before @BeforeMethod executes each time for the @Test method, the output for the above code will be as follow. Now, accountLogin() method will be executed before execution of updateProfile() and then accountLogout () method will be the last step. Again accountLogin() method will be executed before execution of bankBalance() and then stops with accountLogout() method.

For First @Test Method:

“Account has been logged in.”
“Profile has been updated using the updateProfile method.”
“Account has been logged out.”

For Second @Test Method

“Account has been logged in.”
“Bank balance will be shown using the bank balance method.”
“Account has been logged out.”

3) @BeforeClass and @AfterClass

The method annotated with @BeforeClass will execute only once before the first test method in that particular class is invoked. In this, you can initialization or configuration setup for all the conventional test methods. IN @AfterClass annotation will be executed only once after all the test methods of that particular class have been invoked. In the below output you can see that @BeforeClass & @AfterClass are executed at the very beginning and very end, So we can conclude that they both implement only once.

Code:

@BeforeClass

public void accountLogin()
{

System.out.println(“Account has been logged in”)
}

@AfterClass

public void accountLogout()
{

System.out.println(“Account has been logged out”)
}

@Test(priority=0)
public void updateProfile()
{

System.out.println(“Profile has been updated using updateProfile method”)

}

@Test(priority=1)
public void bankBlance()
{

System.out.println(“Bank balance will be shown using the bankBlance method” )
}

Output:

When we run the above code the output will be accountLogin() method is executed before executing updateProfile() & void bankBlance() method at last accountLogout () method is executed.

“Account has been logged in.”
“Profile has been updated using the updateProfile method”
“Bank balance will be shown using the bankBlance method”
“Account has been logged out.”

4) @BeforeTest and @AfterTest

TestNG method that is annotated with @BeforeTest will run before any test methods to that particular classes inside the test tags is run, @BeforeTest methods run after @beforeSuite. For frameworks like smoke testing @BeforeTest is used for creating an initial set-up for the set of data. Whereas @AfterTest annotation will run once after the tests are run, it helps to clean all the data.

Code:

public class Testngfile {
public String basePath = “https://mindmajix.com/selenium-training”;
String path = “D:\\svcdriver.exe”;
public WebDriver automation;@BeforeTest
public void openBrowser() {
System.out.println(“opening chrome browser”);
System.setProperty(“webdriver.firefox.nicole”, path);
automation = new FirefoxDriver();
automation.get(basePath);
}
@Test
public void welcomePage() {
String welcomeMessage = “Hello Connections”;
String requiredMessage =automation.getTitle();
Assert.assertEquals(requiredMessage, welcomeMessage);
}
@AfterTest
public void discontinueBrowser(){
automation.close();
}
}

5) @BeforeSuite and @AfterSuite

With the help of @BeforeSuite annotation, we can set up and start the selenium webdrivers. The @AfterSuite annotation is used to stop the selenium drivers.

Code:

public class SuiteInitialization() {

@BeforeSuite(alwaysRun = true)
public void initializationSuite() {
WebDriver automation = new FirefoxDriver();
}

@AfterSuite(alwaysRun = true)
public void discontinue() {
automation().close();
}
}

When you are working with annotation, it’s essential to check whether your system is installed with Java 1.5 version or higher version, Because annotation works only with Java 1.5 version or the above. In case, You are working with annotation on older versions of Eclipse it through an error saying that annotations are not supported on your system.

Final Words:

In this tutorial, We have discussed some of the important testng annotations and few attributes which are frequently used by testers. Here are some more annotations which are used but not regularly such annotation are @AfterGroups and @BeforeGroups. However, they are significant when you are working with groups in you are the project. From the above annotations pick the right one for your project and use them according to your requirement.

 

–> This is a Guest Article shared by – Gnanasekar (GnanaSekar is working as a Technical Content Contributor & SEO Analyst for Mindmajix. He holds a Bachelor’s degree in Electrical & Electronics Engineering from Anna University. He can be contacted at gnanasekar.6914@gmail.com. Connect with Gnanasekar on LinkedIn.)

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories