Blogs

TestNG – Why and How #SeleniumLearningSeriesATA Part1

#SeleniumLearningSeriesATA PART-1
Selenium Webdriver is a collection of APIs, which is used to automate browsers or front end of web applications or functionalities which involves web based UI for automation testing across many platforms (Combination of Operating system and Processor) using different programming languages such as C#, Java, Perl, and PHP etc.

We write tests using Selenium Webdriver to tests features of a web based applications to minimize manual testing effort.

Why do we need a Testing Framework?

  • In this post, I will use Java- Selenium Webdriver. You write a good number of tests by creating multiple classes and reusable methods. Every test might have some pre-requisites and post-requisites which you are handling through reusable methods and calling them as required. It is not going to be easy to write complex java codes to achieve these configurations.
  • You need to establish relationship among tests i.e. output of one test scripts to another test scripts. It is possible if we run all connected tests in sequence. Obviously you can achieve it with your great doing skills but difficult for beginners.
  • Parameterized tests so that other can execute them without modifying actual codes.
  • Test report is what your managers look to see number of tests which passed or failed.
  • Grouping of tests and running tests as required without changing anything in code.
  • Proper assertion and validation required good programming skills.

Obviously, you can achieve all above points with Java codes if you are excellent coder. But there are some testing framework libraries available which can make your job easier. You just require it to plug and use. Two most famous testing frameworks are JUnit and TestNG. In this post, we will learn about TestNG.

TestNG (Testing Next Generation) :

Official website of TestNG defines it  as “TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).”

Writing a test is typically a three-step process:

  1. Write the business logic of your test and insert TestNG annotations in your code.
  2. Add the information about your test (e.g. the class name, the groups you wish to run, etc…) in a testng.xml file or in build.xml.
  3. Run TestNG.

TestNG provides a lot of flexibilities to help in automated testing. Some are listed below:

  1. Setup of pre-requisites and post-requisites at different level (tests, suite, class , method) using different annotations like @BeforeSuite, @AfterSuite, @BeforeClass and  @AfterClass etc.
  2. Grouping of tests.
  3. Helps in achieving data driven approach using @DataProvider , @Parameters etc.
  4. Parallel execution of tests.
  5. Perform desired operation based on some conditions using Listeners.
  6. Establishing relationship among tests using different attributes such as dependsOnMethods.
  7. Adding/Ignoring tests while execution
  8. A proper test execution report.
  9. A good number of built in methods to assert and verify.

Installation of TestNG:

TestNG is a JAR files which can be downloaded and add to build path of your project. If your project is Maven or Gradle you can use dependencies to automatic download of TestNG jar. You can copy dependencies from here and paste it to build file (pom.xml for maven).

Know about TestNG.xml:

TestNG.xml (You can rename it) is a runner file to run your test tests. It provides you four different levels to categorize your tests: Suite, Test, Class and Method. If helps you to configure how you want to run your tests, pre and post requisites, proper resource utilization and a combined report. You can parameterize your tests by passing required parameters to tests using testing.xml. A sample testing.xml looks as below:

1. <?xml version="1.0" encoding="UTF-8"?>
2. <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
3. <suite name="Suite">
4. <test thread-count="5" name="Test">
5. <classes>
6. <class name="IntegrationOfTestNgSelenium.TestNGWithSelenium"/>
7. </classes>
8. </test> <!-- Test -->
9. </suite> <!-- Suite -->

A sample example of TestNG test:

public class TestNGWithSelenium {

// The annotated method will be run before all tests in this suite have run.
@BeforeSuite
public void beforeSuiteMethod(){
System.out.println(“Use @BeforeSuite annotated method to suite level confguration such as Reproting.”);}

// The annotated method will be run before any test method belonging to the classes inside the tag is run.
@BeforeTest
public void beforeTestMethod(){
System.out.println(“Use @BeforeTest annotated method to test level confguration”);}

// The annotated method will be run before the first test method in the current class is invoked.
@BeforeClass
public void beforeClassMethod(){
System.out.println(“Use @BeforeClass annotated method to class level confguration”);}

// The annotated method will be run before each test method.
@BeforeMethod
public void beforeMethodMethod(){
System.out.println(“Use @BeforeMethod annotated method to Method level confguration”);}

// Marks a class or a method as part of the test.
@Test(description=”It is my first test.”)
public void testMethodOne(){
System.out.println(“It is my first test.”);}

// The annotated method will be run after each test method.
@AfterMethod
public void afterMethodMethod(){
System.out.println(“Use @AfterMethod annotated method to post Method level confguration”);}

// The annotated method will be run after all the test methods in the current class have been run.
@AfterClass
public void afterClassMethod(){
System.out.println(“Use @AfterClass annotated method to post class level confguration”);}

// The annotated method will be run after all the test methods belonging to the classes inside the tag have run.
@AfterTest
public void afterTestMethod(){
System.out.println(“Use @AfterTest annotated method to post test level confguration”);}

// The annotated method will be run after all tests in this suite have run.
@AfterSuite
public void afterSuiteMethod(){
System.out.println(“Use @AfterSuite annotated method to post suite level confguration.”);}
}

Run above code as a testing test.

Output:

Thanks for reading.
To learn more on #Selenium and #TestNG, check out the upcoming #CPSAT Training course by ATA in your city-
https://ataevents.agiletestingalliance.org/

Also Visit CP-Sat Page
https://cpsat.agiletestingalliance.org/

#HappyTesting

Leave a Comment