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 {
  @Test
  public void login() {
    // Open browser and navigate to website
    WebDriver driver = new FirefoxDriver();
    driver.get("http://techblog.polteq.com/testshop/index.php");
    // Navigate to login page
    driver.findElement(By.className("login")).click();
    // Fill in the form
    driver.findElement(By.id("email")).sendKeys(
        "tester@test.com");
    driver.findElement(By.id("passwd")).sendKeys("tester");
    driver.findElement(By.id("SubmitLogin")).click();

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

    // Quit browser
    driver.quit();
  }

}

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.