Hello, everyone! Welcome to this simple guide on how to build a TestNG test with Selenium. I am here to make this very clear and easy for you. We will go through making a test for a login page, step by step, with all the details you need to succeed. Let’s start now and make automation fun and understandable!
- Before You Create a Test Case Using TestNG
- What We will Do in This Tutorial
- Step-by-Step Guide for Creating Our TestNG Test
- Step 1: Make a Java project in Eclipse
- Step 2: Add the TestNG Library
- Step 3: Add Selenium WebDriver
- Step 4: Make a Package
- Step 5: Add a TestNG Test Class
- Step 6: Write the Test Case
- Step 7: Create the TestNG XML File
- Step 8: Run the Test
- Step 9: Look at the Test Report
- How to Fix Common Problems
- More Good Stuff to Make Your Tests Better
- Your Next Steps for You
Before You Create a Test Case Using TestNG
Before we begin, let’s be sure you have all you need. Think of this like getting your tools before you make something.
- Java Development Kit (JDK): You need JDK 17 or new. Why? Because Selenium and TestNG need Java. New versions just work better and have more features, like better ways to write code. The JDK is the engine for your code. It lets your computer run Java programs. Download it from Oracle or Adoptium. To check if it’s working, open your computer’s terminal and type
java -version
. - Eclipse IDE: This is the program where we will write our code. We will use the latest version, Eclipse IDE for Java Developers. Why? It’s free, good for Java projects, and it works great with TestNG. Many developers use it for their work. When you start it, you will make a workspace folder to keep your projects. This folder helps keep everything neat.
- TestNG Plugin: This is a small program for Eclipse that helps it know about TestNG tests. Without it, you can’t run tests or see reports easily because Eclipse won’t understand the special TestNG commands. To get it, go to Eclipse, click Help > Eclipse Marketplace, and search for “TestNG for Eclipse.” After you install it, restart Eclipse to make sure it works.
- Selenium WebDriver: We’ll use Selenium 4.x, the latest version to control web browsers. We will also use a thing called WebDriverManager. Why? This is a magic tool that will automatically get the right browser driver for you. Drivers are small programs that let Selenium talk to the browser. Using this tool saves you a lot of time and prevents errors.
- Firefox Browser: You need to have the latest Firefox on your computer. You can get it from mozilla.org. We will use Firefox because it works very well with WebDriverManager. It’s also very stable for testing. Just be sure to keep it updated to match the driver.
- Basic Knowledge: You should know some basic Java, like how classes and methods work. You should also know how to look at a webpage’s code by right-clicking and choosing “Inspect.” Most errors happen because of a wrong setup or not finding the right part on the page. Knowing how to Inspect a page is a very important skill.
What We will Do in This Tutorial
We are going to make a TestNG test that logs into a website. We will use a real website: The Internet Herokuapp (the actual URL is inside the Java class file). Our test will type a username (“tomsmith”), a password (“SuperSecretPassword!”), click the login button, and then check to see if the login worked. This is a very common test in the real world for sites like banks or shopping apps. Automation testing with Selenium and TestNG can save you a lot of time. TestNG will run the test for us, and Selenium will control the browser.
By the end, you’ll be able to change this code to test any website you want!
Step-by-Step Guide for Creating Our TestNG Test
Step 1: Make a Java project in Eclipse
This is like making a new folder for your project. A Java project in Eclipse organizes all your code and libraries in one place. This helps keep everything in order and makes it easy to share with others.
- Open Eclipse and click File > New > Java Project.
- Give your project a name, like
TestNGSeleniumDemo
. Make the name clear so you know what the project is for. - Click Finish.
Step 2: Add the TestNG Library
TestNG is the brain for our test. It uses special commands (called annotations, like @Test
) to know which parts of your code are the tests. It also creates a report that tells you what passed or failed. This report is very useful for big projects.
- In Eclipse, on the left, right-click on your project.
- Go to Build Path > Add Libraries > TestNG > Next > Finish. If you don’t see TestNG, it means the plugin is not installed.
Step 3: Add Selenium WebDriver
Selenium is what really controls the browser. We will use a tool called Maven to make this simple. Maven helps you manage all the libraries your project needs automatically. You just list the libraries, and Maven gets them for you.
- Right-click your project, then go to Configure > Convert to Maven Project.
- A new file called
pom.xml
will be made. Open it up and add this code inside the<project>
tag:
<dependencies>
<!-- This is the main Selenium library -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.35.0</version>
</dependency>
<!-- This is the magic WebDriverManager library -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>6.3.1</version>
</dependency>
<!-- This is our TestNG library -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
- Save the
pom.xml
file. Eclipse will now automatically get all the needed libraries for you.
Step 4: Make a Package
A package is just a folder that helps keep your code organized. It also prevents problems if you have two files with the same name. Using a standard naming style, like com.yourname.projectname
, is a good idea for big projects.
- Right-click the java folder inside
src/main/
of your project. - Go to New > Package.
- Name it something like
com.techbeamers.testng
and click Finish.
Step 5: Add a TestNG Test Class
This is the place you will write the actual code for your test. The special commands @BeforeMethod
and @AfterMethod
do the set up and cleaning after each test. @BeforeMethod
opens the browser, and @AfterMethod
shuts it down. This makes your tests more reliable and saves computer memory.
- Now, right-click to open the package you just made.
- Go to New > Other > TestNG > TestNG Class.
- Name it
SeleniumWebdriverTest
. - Check the boxes for
@BeforeMethod
and@AfterMethod
. - Press the Finish. If you see some red lines/build errors, press Ctrl+Shift+O to fix the imports.
Step 6: Write the Test Case
Now is the time for you to copy the Java code for the SeleniumWebdriverTest.java
file from this guide. Click the download button shown below to safely get it and then copy in you IDE. When you see the code, check our comments we added to help you understand what each line does. It uses a WebDriverWait, this is better than waiting for a fixed time because it only waits until an element is ready. This will make your tests run faster and with out any issues.
Note: After download, please change the file ext. to .java.
Step 7: Create the TestNG XML File
The testng.xml
file is your test plan to add test cases. It tells TestNG which tests to run. This is a major way to control your tests. For example, you may select a few of them to run at a point of time, or set them up for running in parallel to save time.
- Select the Java project and go to New > File.
- Give this name to your file
testng.xml
. - Copy and paste this code into the new file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "[http://testng.org/testng-1.0.dtd](http://testng.org/testng-1.0.dtd)">
<suite name="DemoSuite" verbose="2">
<test name="LoginTest">
<classes>
<class name="com.techbeamers.testng.SeleniumWebdriverTest"/>
</classes>
</test>
</suite>
Step 8: Run the Test
Now for the fun part! Let’s see our test in action. As we told you last that use the TestNG XML file to run the test cases. It makes sure all the setup and cleanup happens correctly.
- Right-click on the
testng.xml
file. - Go to Run As > TestNG Suite.
- Watch the Eclipse console at the bottom. You will see – a browser will come, the test will run, and then the window will close. To confirm, check for a green message that says your test passed! If there comes any issue, check that the class name in your XML file is correct.
Step 9: Look at the Test Report
After the test runs, TestNG makes a report to tell you what happened. This report is your feedback. It is very important because it tells you what worked and what failed. It also shows you how long each test took to run.
- Look in your project folder and find a folder called
test-output
. - Inside, you will find a file called
index.html
. Open it in your web browser.
How to Fix Common Problems
- Browser won’t open: It happens when the driver wasn’t set up the right way. Make sure you have the right version of Firefox. If it still doesn’t work, try clearing the cache by running
WebDriverManager.firefoxdriver().clearDriverCache()
. - “NoSuchElementException”: This means your code could not find an element on the webpage. Double-check your code and the webpage’s source. Make sure the ID is correct. Try using a different locator like XPath as a backup.
- TestNG errors: Check if you have all the imports. Use Ctrl+Shift+O to fix them. If that fails, go to your
testng.xml
file and check for any simple mistakes like a typo.
More Good Stuff to Make Your Tests Better
Here are some pro tips to make your tests even more strong and reliable.
- Page Object Model (POM): This is a way to say “keep your code organized.” You can move all the element locators (like
By.id("username")
) into a separate class. This makes your code a lot cleaner and easier to update if the page changes. It’s a very common practice in professional teams. - Use Asserts: Instead of a simple
if-else
statement to check if your test passed, you should use TestNG’s Assert class. It gives you much better reports when a test fails. It tells you exactly what went wrong in your test. - Parallel Execution: If you have a lot of tests, you can make them run at the same time to save a lot of time! Just add
parallel="methods" thread-count="3"
to your<suite>
tag in thetestng.xml
file. This can make your tests run much faster. - Keep Your Passwords Safe: Don’t put passwords right in your code. You can put them in a separate file and read from there. This is a good security practice that you should always use.
Your Next Steps for You
You’ve built a working test—that’s great! Now you can try to:
- Change the code to test a different website.
- Try using a different browser, like Chrome.
- Learn about more advanced TestNG features like
@DataProvider
to run your test with different usernames and passwords.
I hope this guide was helpful and made automation testing easy for you! Happy testing! Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Twitter) if you gained some knowledge from this tutorial.
Enjoy testing,
TechBeamers.