2
2016
How to Use TestNG Framework for Selenium WebDriver
What is test automation?
Test Automation is the use of tools and strategies that reduce human involvement or interaction in unskilled, repetitive or redundant tasks that occur in testing. Automated testing tools are capable of executing tests, reporting outcomes and comparing results with earlier test runs. Tests carried out with these tools can be run repeatedly, at any time of day.
Selenium WebDriver
Selenium WebDriver is a web automation tool that allows executing tests against different browsers. It aims to provide a friendly API that’s easy to explore and understand. This is a successor of Selenium RC. It does the same job, but in a different way, instead of injecting a JavaScript code into the browser to simulate user actions, it uses the browser’s native support for automation.
How to install Selenium WebDriver
- Install Java on your computer – Download Java – http://www.oracle.com/technetwork/java/javase/downloads/index.html
- Install Eclipse IDE – Download Eclipse IDE – http://www.eclipse.org/downloads/
- Download the Selenium Java Client Driver – Select the Java client driver out of other language drivers. These Jar files are downloaded as a ZIP file. Download Selenium Java Client Driver – http://docs.seleniumhq.org/download/
- Configure Eclipse IDE with WebDriver – Configure the Eclipse and create a new project through File > New > Java Project
Figure 1 : Install Eclipse
- Add different browser drivers to create scripts for the different browsers. For example, Internet Explorer has its own driver server which cannot be used on other browsers.
TestNG Framework
TestNG is an enhanced combination of JUnit and NUnit and it has some new functionality such as ‘Annotations’. TestNG is useful to conduct different categories of tests such as unit, functional, end-to-end, integration, etc.
Features of TestNG
- Support for annotations
- Support for Data Driven Testing
- Enables user to set execution priorities for the test methods
- Parallel testing is possible
How to install TestNG in Eclipse
- Click on the ‘Help’ on the menu bar > Choose the “Install New Software…” option.
Figure 2 : Install TestNG
- Click the Add button in the ‘Install dialog box’.
- Type “TestNG” in the ‘Name’ field and enter “http://beust.com/eclipse” to the ‘Location’ field.
- Click ‘OK’ button.
Figure 3: Set ‘Name’ and ‘Location’
- Check that “TestNG – http://beust.com/eclipse” is populated onto the “Work with:” textbox.’
- Tick on the “TestNG” check box and click on the ‘Next’ button.
Figure 4: Select the “TestNG” check box
- Click ‘Next’ till you get the succeeding dialog box.
- Accept the “terms of the license agreement” and click on the ‘Finish’ button.
- Click ‘OK’ to the ‘Security Warning’ and click ‘Yes’ button to restart eclipse.
- Click Window > Preferences and check that TestNG is successfully installed.
Figure 5: Verify TestNG installation
Create a new TestNG Project
- Click File > New > Java Project.
- Type a project name (Example: ‘Test Project’) and click ‘Next’ button.
- Click on the ‘Libraries’ tab, and then click on the ‘Add Library’ tab
- Select ‘TestNG’ and click ‘Next’ button.
- Click on the ‘Finish’ button.
- Make sure that TestNG is included on the ‘Libraries’ list.
Figure 6: Verify TestNG implementation
Now you have successfully installed the eclipse and TestNG framework. You can enjoy automation by creating a new TestNG file.
Some important tips on the TestNG:
- A main() method is not required.
- TestNG does not require static method.
- Methods can be controlled using the Annotations in TestNG (Ex: @ Before Test, @ Test, etc)
- It is necessary to import the package ‘org.testng.annotations.*’ as we use annotations in TestNG
Create the first TestNG test case
Let us now create our first test case to verify that ‘Blog’ button is displayed in the ‘Mazarin Page’. Create your code as shown below.

Figure 7: Create a test case using TestNG
Executing the test case
It is very simple to run the created test case through the TestNG. You can execute the TestNG test script as follows,
- Right click on the class
- Select “Run As” option
- Click on the “TestNG Test”
Figure 8: Execute a test case using TestNG
Check results of the test case
You can check the results in two windows and you can verify the test results through these two windows.
- Console Window
- TestNG Result Window
References:
- http://www.softwaretestinghelp.com/testng-framework-selenium-tutorial-12/
- http://www.guru99.com/all-about-testng-and-selenium.html
- http://toolsqa.com/selenium-webdriver/testng-testcase/
Author: Harshana Perera