Selenium Java Tutorial with TestNG (Free Course + Certification)

A hands-on Selenium WebDriver tutorial in Java with TestNG, from an empty Maven project to parallel runs on a cloud Selenium grid. Every demo in this video is a real run of the course code.

What you'll learn

  • Set up a Maven project with selenium-java, TestNG and Surefire (and why Surefire stays on 3.5.x)
  • Write a first test with explicit waits instead of Thread.sleep
  • TestNG annotations and where the browser belongs: one driver per test in a base class
  • Hard assertions vs SoftAssert, and TestNG's actual-first argument order
  • Data-driven tests with DataProvider
  • Suites, groups and parameters in testng.xml, cross-browser runs in Chrome and Firefox
  • Parallel execution with a ThreadLocal driver
  • Screenshots on failure with a TestNG listener
  • Running the same suite on Chrome, Firefox and Safari on the TestingBot Selenium grid

The full course, with code, quick checks and a knowledge check, is free and needs no account: Selenium Java Tutorial with TestNG. Free TestingBot Selenium TestNG certification (pass the exam and run your suite in parallel on TestingBot; issued by TestingBot, not by the Selenium project): certification details.

Chapters

  • 0:00 Course introduction
  • 0:58 Selenium and TestNG: how they fit together
  • 1:22 Chapter 1: Set up a Maven project for Selenium
  • 2:02 Chapter 2: Your first Selenium test with TestNG
  • 2:39 Chapter 3: TestNG annotations and the driver lifecycle
  • 3:13 Chapter 4: Hard assertions vs SoftAssert
  • 3:49 Chapter 5: Data-driven tests with DataProvider
  • 4:26 Chapter 6: Suites, groups and parameters in testng.xml
  • 5:20 Chapter 7: Run Selenium tests in parallel
  • 6:03 Chapter 8: Listeners, screenshots and reports
  • 6:43 Chapter 9: Run the suite on a Selenium grid
  • 9:05 Chapter 10: Knowledge check

The presenter is an AI avatar of Jochen Delabie (TestingBot), voiced by a stock AI voice.

Video transcript

Course introduction

Hi, I'm Jochen from TestingBot. This free course teaches Selenium WebDriver in Java, with TestNG as the test runner. You'll need basic Java, JDK 17 or newer, Maven and Chrome. No Selenium experience needed. Chapter by chapter, you build one Maven project. You use explicit waits instead of sleeps, give every test its own browser, and choose between hard and soft assertions. You feed tests from data providers, and drive runs from testng.xml with groups and parameters. Then you run in parallel, one driver per thread, and save a screenshot on every failure. In chapter nine, the same tests run on TestingBot's Selenium grid, on Chrome, Firefox and Safari. When you're done, you can earn the free TestingBot certification: pass the exam, and run your suite in parallel on the grid. Let's get started.

Selenium and TestNG: how they fit together

Before any code, one line runs through the whole course. Selenium WebDriver drives the browser: it opens pages, finds elements, clicks and reads text, but it has no idea what a passing test is. TestNG runs the tests: setup, order, threads, data and results, but it knows nothing about browsers.

Chapter 1: Set up a Maven project for Selenium

Chapter one sets up the Maven project: selenium-java and TestNG as test dependencies, and Surefire pinned to 3.5.6. That pin matters: from Surefire 3.6, TestNG runs on the JUnit Platform, which can't read testng.xml. There's no driver to download; Selenium Manager handles it. A throwaway test proves the wiring. mvn test, on the new project. One test runs, and passes. Maven, TestNG and Surefire are wired up.

Chapter 2: Your first Selenium test with TestNG

Chapter two drives a real browser. One test fills in Selenium's web form and checks the confirmation. The other waits for an element that appears a second after a click, with WebDriverWait. That's an explicit wait: no Thread.sleep, and no implicit wait mixed in. Here are both, in Chrome. Both tests, in a real Chrome window. The first fills in the web form, submits it, and checks the confirmation. The second clicks, then waits until the delayed element appears. Two passed.

Chapter 3: TestNG annotations and the driver lifecycle

Chapter three is about where the browser lives. The annotation you create it in decides how isolated your tests are. You create the driver in Before Method, quit it in After Method with alwaysRun, and move both into a base class. Here's the order TestNG runs its annotations in. A small class that prints a line from every annotation. The order matches the chapter: suite, test, class, and Before Method and After Method around each test.

Chapter 4: Hard assertions vs SoftAssert

Chapter four. A hard assertion stops the test at the first failure. A soft assertion collects every failure and reports them together, as long as you call assertAll. You'll also see why TestNG puts the actual value first. Here, one soft assertion reports two problems at once. The soft assertion test, with two expectations changed to be deliberately wrong. One failed test, and its message lists both problems: the second checkbox, and the read-only value.

Chapter 5: Data-driven tests with DataProvider

Chapter five. Many tests are the same flow with different inputs. A data provider keeps one test method and feeds it rows; each row becomes its own result, with a fresh browser. You'll also share providers, read rows from a file, and see why an empty provider fails nothing. Four rows, four results. One test method, and four rows of data. Each row gets a fresh browser, types its text and submits the form. Four rows, four passing results.

Chapter 6: Suites, groups and parameters in testng.xml

Chapter six. Annotations describe tests; testng.xml describes a run. You tag tests as smoke or regression, park flaky ones in a quarantine group, pass the browser in as a parameter, and use dependsOnMethods so one failure doesn't cascade. Here's one suite file running the same tests in Chrome and Firefox. One suite file, with a test block per browser. The Chrome block runs first: three classes, six tests. Then the Firefox block runs the same six. Twelve tests, all passed.

Chapter 7: Run Selenium tests in parallel

Chapter seven. Parallel runs are one line in testng.xml. Making tests safe on those threads is the real work, and the rule is simple: every thread gets its own WebDriver. You move the driver into a ThreadLocal, and find what else must not be shared. Here, three threads plus a parallel data provider run four browsers at once. parallel equals methods, three threads, and a data provider pool of two. Four browsers start at once: two test methods, and two data provider rows. Six tests, all passed, in about four seconds.

Chapter 8: Listeners, screenshots and reports

Chapter eight. A stack trace doesn't show what the page looked like. A TestNG listener runs on every test event without touching the tests, so it's the place to save a screenshot on failure, before the browser closes. You'll also see why retries are a bandage, not a fix. Here's a failure, and its screenshot. The listener is registered in testng.xml, and one test now expects the wrong text. It fails, and the listener saves a screenshot before the browser closes. Here it is: the page said Received, not Saved.

Chapter 9: Run the suite on a Selenium grid

Chapter nine moves the suite to TestingBot's Selenium grid, and the tests themselves don't change. A driver factory swaps ChromeDriver for RemoteWebDriver when you pass one system property. Each session is named after its test, grouped in a build, and reports pass or fail to the dashboard. Here it is, running for real. First, your credentials. In the TestingBot member area, open Settings, then Security. The API Credentials card has your key and secret, each with a copy button. Keep them out of your code. Export them as TB_KEY and TB_SECRET; the driver factory reads them from the environment. In CI, store them as secrets. Three browser blocks, in parallel, on the TestingBot grid. Safari and Firefox on macOS Sonoma start first. This demo account runs two sessions at a time, so the Chrome block waits in the queue until a slot is free. Then Chrome 157 on Windows 11 runs the same two tests. Six sessions, all passed. In the TestingBot dashboard, the build groups all six sessions: two tests on three browsers, every one passed. They ran in parallel, so the whole build finished in about thirty seconds. The more sessions you run in parallel, the faster you get results. Click a test to open it: the environment, here Safari 18 on macOS Sonoma, the duration, and every command Selenium sent. Each command shows its parameters, and the session video shows what the browser did. Here, Selenium types into the form. And here the form is submitted, and the page says Received. Where a test runs is just capabilities: pick the browser, version and platform, set the screen resolution, and add options such as geolocation. The capabilities generator writes the code for you.

Chapter 10: Knowledge check

That's the course. The last chapter is a knowledge check: ten questions, each with a short explanation. If you'd like something to show an employer, take the free TestingBot certification: pass the exam, and run your suite in parallel on the grid. Both are linked from the course page.

Ready to start testing?

Start a free trial