How to Use TestNG and Maven in Selenium (Step-by-Step)

Learn how to use TestNG and Maven in Selenium with a clear, fresher-friendly walkthrough. Set up a Maven project, add the right dependencies and run your first TestNG test. Includes common mistakes, a learning roadmap and answers to common QA automation questions.
A fresher-friendly walkthrough: set up Maven, add TestNG and run your first Selenium test the right way.
Picture a final-year student in Thane who applies for ten QA roles and gets one call back. In the interview, the panel asks, "How would you organise your Selenium tests in a project?" The student knows Selenium commands but goes blank. The interviewer is really asking about TestNG and Maven in Selenium, the two tools that turn loose scripts into a real automation project.
This story is common among freshers. At Cinute Digital, we often meet learners who can open a browser with Selenium but struggle to run, organise and report tests the way companies expect.
In this guide, you will learn what TestNG and Maven do and how they fit with Selenium. You will set up and run your first test step by step, see common beginner mistakes and get a realistic learning path.
Ready to stop writing scripts and start building a project?
To use TestNG and Maven in Selenium, create a Maven project, add the selenium-java and testng dependencies to pom.xml, write test classes in src/test/java using TestNG annotations such as @Test, and run them with mvn test or a testng.xml suite file.
What Are TestNG and Maven? The Basics Before You Start
Think of an automation project as a cricket team. Selenium is the batter: it does the actual playing by clicking, typing and reading things inside a browser. But one batter does not make a match, and in a real project each tool has one clear job.
TestNG is the umpire and scorer. It decides which test runs first, marks each one as pass or fail and prepares the scorecard, which is your report. TestNG is a Java testing framework, inspired by JUnit, and its annotations such as @Test, @BeforeMethod and @AfterMethod control how each match is played.
Maven is the team manager. It arranges the kit before the match. It downloads the libraries your project needs, creates a standard folder structure and runs your tests with a single command. Everything is described in one file called pom.xml.
Each tool also has limits. Selenium does not decide pass or fail, TestNG does not download libraries, and Maven knows nothing about browsers. You need all three working together for a clean, repeatable project.
Both TestNG and Maven live in the Java world. Selenium supports other languages too, but this setup assumes Java. If classes, methods and variables still feel new, begin with a Java programming basics course, because Java is the base on which every TestNG and Maven project is built.
You may ask, "Why not JUnit?" Both are good. TestNG is popular in Selenium job descriptions because of built-in groups, data providers and easy parallel runs, but check listings in your own city before you decide.
Without Maven, you would download jar files by hand, add them one by one and run classes from the IDE. With Maven, a teammate can clone your project and run it on any machine, which is how real QA teams work. A clean Selenium Maven project setup also makes your work easy to share on GitHub.

Why TestNG and Maven Matter for QA Jobs in India
Software testing is a steady entry route into IT for freshers across Mumbai, Pune, Bengaluru and Hyderabad. Teams in 2026 release updates faster, so more companies automate repeated checks. Automation job descriptions often list Selenium with Java, TestNG, Maven, Git and Jenkins together. This is a general observation, not a statistic, so check current job listings on Naukri or LinkedIn to see what employers in your city want.
Here are the roles this skill set usually points to, with approximate pay ranges. Treat the figures as rough guides and verify them with a current industry source such as Naukri or LinkedIn Salaries.
- Manual Test Engineer / QA Analyst (fresher): approximately ₹2.5–4.5 lakh per year
- Automation Test Engineer (0–2 years): approximately ₹3.5–7 lakh per year
- SDET (about 2–5 years): approximately ₹7–15 lakh per year
Pay varies widely by company, city, skills and interview performance, so these are approximate ranges, not promises.
Do not skip the basics while chasing automation. Interviewers often start with test cases, bug reports and the testing life cycle, so a solid grounding in manual testing fundamentals makes your scripts far more meaningful. Automation is built on good test thinking.
Whether you target a Mumbai startup or a large IT services company, the questions are similar. How do you structure tests? How do you run them? How do you read failures? Knowing TestNG and Maven in Selenium also tells an employer that you can work on a shared project, not just a single script. That is the difference between a tutorial learner and a team-ready fresher.
Some freshers worry that AI tools will replace testers. AI can help you draft test cases or code, but teams still need people who understand test design, project structure and failure analysis. Strong fundamentals stay valuable.

How to Use TestNG and Maven in Selenium: Step-by-Step Walkthrough
Let's build a tiny project together. First, see who does what.
- Check your setup. Install a current JDK and an IDE such as IntelliJ IDEA or Eclipse. Confirm that
java -versionandmvn -versionprint details in the terminal. Recent Selenium 4 releases include Selenium Manager, which fetches a matching browser driver for you, so manual driver downloads are usually unnecessary. - Create a Maven project. In your IDE, choose New Project, then Maven. Enter a groupId such as
com.yournameand an artifactId such asselenium-testng-demo. Maven createssrc/main/java,src/test/javaandpom.xml. - Add dependencies. Inside
<dependencies>inpom.xml, addselenium-java(grouporg.seleniumhq.selenium) andtestng(grouporg.testng). Take the latest stable versions from Maven Central instead of copying numbers from old blogs, then reload Maven so the jars download. These pom.xml dependencies for Selenium are the heart of your Selenium Maven project setup. - Write your first test. Create
TitleTest.javainsidesrc/test/javawith this code:
@BeforeMethodruns before every test and opens a maximised Chrome window.@TestmarksverifyHomePageTitleas a test. It opens the site, reads the page title, prints it and checks that it is not empty.Assert.assertTruedecides pass or fail. Without an assertion, a test proves nothing.@AfterMethodruns after every test and callsdriver.quit()to close the browser.
Together, these TestNG annotations in Selenium give every test the same life cycle: set up, test, clean up.
- Run it. Right-click the class and run it as a TestNG test. Or open a terminal in the project folder and type
mvn test. This is how you run Selenium tests with Maven from the command line. - Organise with testng.xml. Create a
testng.xmlfile that lists your test classes. Then point the Maven Surefire plugin to it inpom.xmlusing itssuiteXmlFilessetting. Nowmvn testruns the whole suite.
Expected output: a Chrome window opens and closes, and the console prints a line starting with Page title:. TestNG reports one test passed. With mvn test, the summary shows Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 followed by BUILD SUCCESS.
You can practise this on guided projects through a structured automation testing training programme. Check the syllabus on the course page to confirm which tools and topics it covers. Hands-on repetition builds confidence.

Common Mistakes Freshers Make with TestNG and Maven
Most beginner errors with TestNG and Maven in Selenium are setup and habit errors, not Selenium errors. Watch out for these five.
- Copying old version numbers. Outdated or mismatched versions cause confusing errors. Keep your pom.xml dependencies for Selenium and TestNG current by taking stable versions from Maven Central, and skim the release notes when you upgrade.
- Putting tests in the wrong folder. Maven's test phase looks in
src/test/java. If your classes sit insrc/main/java,mvn testmay report "No tests to run", and you will wonder what went wrong. - Skipping assertions and overusing sleeps. A script that never asserts proves nothing, and
Thread.sleepmakes tests slow and flaky. Use assertions and explicit waits instead. - Forgetting to close the browser. A missing
driver.quit()leaves browser windows open and slows your laptop. Keep it inside@AfterMethod, so cleanup happens even after a failure. - Learning only UI automation. Many defects sit behind the screen, in the APIs. Once your UI tests are stable, API testing with Postman is the logical next skill, because it checks the application directly and usually runs faster than browser tests.
Responsible practice. Automate only applications you own or have written permission to test. Do not run heavy loops against live production sites, and never hard-code real passwords or customer data in scripts. Use test accounts and keep secrets out of GitHub.

Career Path and Learning Roadmap for Selenium Testers
Once you can run a small project using TestNG and Maven in Selenium, you can apply for roles such as Automation Test Engineer, QA Engineer and, with experience, SDET. Selenium automation testing for freshers is easier when you learn in a sensible order:
- Java basics: classes, methods, collections
- Manual testing concepts: test cases, bug reports, test life cycle
- Selenium WebDriver: locators, waits, assertions
- TestNG (data providers, groups, parallel runs) and Maven
- Page Object Model and a small framework with reports
- Git and Jenkins basics
- API testing and SQL basics
With regular daily practice, many freshers need roughly four to six months to feel interview-ready for entry-level automation roles. This is approximate, and it varies with your background, practice time and project quality.
Build proof as you learn. Push your demo project to GitHub, add a short README and note what each test checks. A small, clean project beats a long list of tool names.
If you prefer structured learning with trainers, CDPL's Manual and Automation Testing Master Program is one option to explore. CDPL offers live online and classroom options, hands-on projects, mock interviews, resume support and placement assistance, though your own practice and portfolio will always shape the outcome.
Conclusion: Start Small and Keep Practising
Let's recap the three points that matter most.
First, Selenium drives the browser, TestNG organises and reports your tests, and Maven manages libraries and runs everything with one command. Second, the working flow is simple: create a Maven project, add the dependencies, write a TestNG test and run mvn test. Third, most beginner trouble comes from setup habits rather than Selenium itself, so keep folders tidy, add assertions and always close the browser.
Learning TestNG and Maven in Selenium can feel heavy on day one, but you can build it in small daily steps. Run your demo, add one new test every few days and use the TestNG annotations in Selenium until they feel natural. Then push the project to GitHub and let your work speak for you in interviews. If you would like guidance from trainers, you can book a free demo class and ask your questions directly.
You do not need to be an expert to begin. You only need to run your first test today.
FAQ Section
1. What is TestNG and Maven in Selenium?
In Selenium projects, TestNG and Maven are supporting tools. Selenium controls the browser. TestNG is a Java testing framework that runs your tests, uses annotations like @Test, checks results with assertions and creates reports. Maven is a build and dependency management tool that downloads libraries listed in pom.xml, builds the project and runs tests with commands such as mvn test. Together they turn single scripts into an organised, repeatable project.
2. How to run Selenium tests with Maven?
Keep your TestNG classes inside src/test/java, add selenium-java and testng to pom.xml, and open a terminal in the project folder. Run mvn test. Maven compiles the code, finds the tests through its Surefire plugin and prints a summary of tests run, failures and skipped tests. To run a specific suite, point the Surefire plugin to your testng.xml file in pom.xml.
3. Is TestNG better than JUnit for Selenium?
Neither is universally better. TestNG offers built-in groups, data providers, flexible test ordering and easy parallel runs through testng.xml, which is why it appears often in Selenium job descriptions. JUnit 5 is also widely used and works well for many teams. For a fresher, TestNG is a practical first choice, but check the job listings in your city and follow your target employer's stack.
4. How long does it take to learn TestNG and Maven for Selenium?
If you already know basic Java, you can usually understand Maven setup and core TestNG annotations in a few days of focused practice. Becoming comfortable with a small framework, including waits, Page Object Model and reports, often takes a few weeks more. These timelines are approximate and depend on your practice time, so build a small project and measure your own progress.
5. How much does a Selenium automation testing course cost?
Fees differ from one institute to another, so any single figure would be a guess. Instead of comparing price alone, compare the syllabus, training hours, trainer experience, hands-on projects, mock interviews and placement assistance. For CDPL, check the course page or contact the team for current fees and batch details, and ask about live online and classroom options before you decide.
Tags

A Manual Tester in TESTRIQ QA LLP and also as Corporate Trainer with CDPL. With a focused career in training and development.
Ready for Career Guidance?
At CDPL Ed-tech Institute, we provide expert career advice and counselling in AI, ML, Software Testing, Software Development, and more. Apply this checklist to your content strategy and elevate your skills. For personalized guidance, book a session today.
