Write your test

To write a tests with TestNG we have to annotate methods or classes with the @org.testng.annotations.Test annotation.

Annotations on class level mean that all public methods within the class are considered test methods.

Annotations on method level mean that a annotated method is considered a test method.

In practice

A test method can look like the following code.

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class FirstTest {
  public void login() {
    // Open browser and navigate to website
    WebDriver driver = new FirefoxDriver();
    // Navigate to login page
    // Fill in the form

    Dimension dim = driver.manage().window().getSize();
    System.out.println("Window width: " + dim.getWidth());

    // Quit browser


Make sure your class (containing the tests) is named like *Test.java, then it will be automatically run by the Maven Surefire plugin. See Section: 4.7 Execute your test, Paragraph “Use Maven to execute your tests”.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.