Introducing pre and post methods

Rate this post

Introducing pre and post methods In the previous test script you can see that we opened and closed the browser from within the test. You can imagine that we have to do this for every test script. So why we don’t move these pieces of code to a common place. TestNG is a very flexible testing framework which comes with a lot of before and after annotations. We can apply these annotations on different levels.

<code lang="bash">
@BeforeSuite
  @BeforeClass
    @BeforeMethod
      @Test
    @AfterMethod
    [ ]
    @BeforeMethod
      @Test
    @AfterMethod
  @AfterClass
@AfterSuite
</code>

In practice

The code will look like this, after we applied the before/after methods.

<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.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class FirstTestBeforeAndAfter {
  private WebDriver driver;

  @BeforeMethod
  public void setUp() {
    // Open browser and navigate to website
    driver = new FirefoxDriver();
    driver.get("http://techblog.polteq.com/testshop/index.php");
  }

  @AfterMethod
  public void tearDown() {
    // Quit browser
    driver.quit();
  }

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

We can also extend the test classes from a class containing these before and after methods, since we need the before and after methods for all test scripts in multiple classes. The base class and the extended class code will look as follows.

<code lang="java">
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public class DriverBase {
  protected WebDriver driver;

  @BeforeMethod
  public void setUp() {
    driver = new FirefoxDriver();
    driver.get("http://techblog.polteq.com/testshop/index.php");
  }

  @AfterMethod
  public void tearDown() {
    driver.close();
  }
}
</code>
<code lang="java">
@Test
public class LoginTest extends DriverBase {
  
  // Test scripts here...

}
</code>

One thought on “Introducing pre and post methods”

  1. The code for FirstTestBeforeAndAfter contains an error at line 15: WebDriver driver = new FirefoxDriver();
    This will cause a null pointer exception at line 24 and 30 because there’s instance of WebDriver that’s not been used.
    Line 15 should read: driver = new FirefoxDriver();
    so that this instance is used across the methods.

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.