• Blog Home
  • Tech Talk
    • Best Practices
    • Java
    • .NET
    • Mobile
    • UI/ UX
    • Systems Engineering
    • Quality Assurance
  • ClubM

Sign in

  • Mazarin Corporate Site »
Mazarin Blog
stay connected
Join us on Facebbook! Follow Us on Twitter! Subscribe to our RSS Feed!
Feb
2
2016
Tech Talk

How to Use TestNG Framework for Selenium WebDriver

Author Article by Harshana Perera    Comments No Comments

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

  1. Install Java on your computer – Download Java – http://www.oracle.com/technetwork/java/javase/downloads/index.html
  2. Install Eclipse IDE – Download Eclipse IDE – http://www.eclipse.org/downloads/
  3. 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/
  4. Configure Eclipse IDE with WebDriver – Configure the Eclipse and create a new project through File > New > Java Project

     

    Install Eclipse - Selenium

    Figure 1 : Install Eclipse

  5. 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

  1. Click on the ‘Help’ on the menu bar > Choose the “Install New Software…” option.

     

    Install TestNG

    Figure 2 : Install TestNG

  2. Click the Add button in the ‘Install dialog box’.
  3. Type “TestNG” in the ‘Name’ field and enter “http://beust.com/eclipse” to the ‘Location’ field.
  4. Click ‘OK’ button.

     

    Set 'Name' and 'Location'

    Figure 3: Set ‘Name’ and ‘Location’

  5. Check that “TestNG – http://beust.com/eclipse” is populated onto the “Work with:” textbox.’
  6. Tick on the “TestNG” check box and click on the ‘Next’ button.

     

    Select the "TestNG" check box

    Figure 4: Select the “TestNG” check box

  7. Click ‘Next’ till you get the succeeding dialog box.
  8. Accept the “terms of the license agreement” and click on the ‘Finish’ button.
  9. Click ‘OK’ to the ‘Security Warning’ and click ‘Yes’ button to restart eclipse.
  10. Click Window > Preferences and check that TestNG is successfully installed.

     

    Verify TestNG installation

    Figure 5: Verify TestNG installation

 

Create a new TestNG Project

  1. Click File > New > Java Project.
  2. Type a project name (Example: ‘Test Project’) and click ‘Next’ button.
  3. Click on the ‘Libraries’ tab, and then click on the ‘Add Library’ tab
  4. Select ‘TestNG’ and click ‘Next’ button.
  5. Click on the ‘Finish’ button.
  6. Make sure that TestNG is included on the ‘Libraries’ list.

     

    Verify TestNG implementation

    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.

 

Create a test case using TestNG

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,

  1. Right click on the class
  2. Select “Run As” option
  3. Click on the “TestNG Test”

     

    Execute a test case using TestNG

    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

Related Post

Test Automation of Mobile Applications using Appium
What is NFC – The Ultimate Guide
How To Start Cloud Computing with AWS
Serverless Architecture with AWS Lambda
Mazarin Foodies 2014
Company Culture
Mazarin Kite Masters embellished Colombo Galle Face Sky
Let it Snow ’13
Tags: Mazarin, QA testing, Selenium, Selenium WebDriver, test automation, TestNG, TestNG framework
Did you enjoy reading this article? Share it! Share on Facebook Tweet this! Bookmark on Delicious StumbleUpon Digg This!

Related Posts

  • Serverless Architecture with AWS Lambda
  • Let’s move to NoSQL Databases with MongoDB – Mazarin
  • Without Redux and with Redux application state behaviorProductive Development With React Redux
  • Elements of CultureCompany Culture
avatar

About the Author: Harshana Perera

Leave a comment

Click here to cancel reply.

CAPTCHA
Refresh

*

Follow Us on Twitter!

Related Post

Sass and LESS: An Introduction to CSS Preprocessor...
Azure Functions – Learn more about it
Firebase – Mobile Application Development &#...
Serverless Architecture with AWS Lambda
Let’s move to NoSQL Databases with MongoDB &...
Productive Development With React Redux
Beginners’ Guide to CSS (CSS for dummies)
Company Culture
What is Docker ? Getting Started with Docker
Hybrid Mobile App Development with Ionic and Angul...
Test Automation of Mobile Applications using Appiu...
What Power BI Can Do – Major Benefits
Data Mining using SQL Server Analysis Server
Learn Cucumber Test Automation with Ruby Core Fram...
How to Succeed With Designing Scalable Web Apps
Importance of Big Data and Managing Data with Elas...
An Introduction to Node.js – Kickstarter
MS SQL Server BI (Business Intelligence)
How To Start Cloud Computing with AWS
What is NFC – The Ultimate Guide
Avatars by Sterling Adventures

Team Mazarin

A team of individuals dedicated to share common goals and vision of the company. Mazarin's endowed team consists of Managers, Software Engineers, User Interface Engineers, Business Analysts, Finance and Administration. We are a blend of quality people. We strive to maintain the open culture and work in close association. The way we work enables everyone to contribute while feeling contented sharing opinions and ideas to deliver the best software solutions.

Read More

Mazarin © 2023. All Rights Reserved.