Assert your result

TestNG provides some assertions. In this section you will see how to apply them.

In practice

The previous test will look like this when we add an assertion.

<code lang="java">
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class FirstTestAssert {

  public class LoginTest {
    @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();
      // Assert if element is displayed
      Assert
          .assertTrue(driver.findElement(
              By.cssSelector("ul.myaccount_lnk_list"))
              .isDisplayed());
      // Quit browser
      driver.quit();
    }
  }
}
</code>

See also

More advanced assertions are explained in chapter 7, using the Hamcrest library.

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.