Thomas Sundberg

July 11, 2010

Parameterized JUnits tests

This post is available at http://www.thinkcode.se/blog/2010/07/11/parameterized-junits-tests

6 Comments »

  1. […] This post was mentioned on Twitter by Alexandru Bolboaca. Alexandru Bolboaca said: RT @thomassundberg: New blog post: http://ht.ly/29SDe Parameterized JUnit […]

    Pingback by Tweets that mention Parameterized JUnits tests « Thomas Sundberg -- Topsy.com — July 12, 2010 @ 09:49

  2. The parameterized JUnit test test works as expected if there is one @Test because driver.close() will tidy up nicely. However, when there are 2 or more it becomes more problematic (we can’t call driver.close() within the tests if we want to run more than one @Test).

    I’ve tried using driver.quit() in an @AfterClass annotation but that leaves all but the last browser session windows.

    I’ll post my code below and reply back when/if i find a solution. Thanks!


    package com.clickfox.selenium.wd.util;

    import static junit.framework.Assert.assertTrue;

    import java.util.Collection;
    import java.util.LinkedList;
    import java.util.List;

    import org.junit.AfterClass;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.ie.InternetExplorerDriver;

    @RunWith(Parameterized.class)
    public class ParameterizedClassExample
    {
    private static WebDriver driver;

    public ParameterizedClassExample(WebDriver driver)
    {
    this.driver = driver;
    }

    @Parameterized.Parameters
    public static Collection drivers()
    {
    List drivers = new LinkedList();
    drivers.add(new WebDriver[] { new ChromeDriver() });
    drivers.add(new WebDriver[] { new InternetExplorerDriver() });
    drivers.add(new WebDriver[] { new FirefoxDriver() });

    return drivers;
    }

    // @After
    // public void cleanup()
    // throws Exception
    // {
    // driver.quit();
    // }

    @AfterClass
    public static void teardown()
    {
    System.out.print("This is the AfterClass.");
    driver.quit();
    }

    @Test
    public void googleSearch()
    {
    driver.get("http://www.google.com");

    WebElement searchArea = driver.findElement(By.name("q"));
    searchArea.sendKeys("JUnit");

    WebElement searchButton = driver.findElement(By.name("btnG"));
    searchButton.click();

    String pageSource = driver.getPageSource();
    assertTrue(pageSource.contains("JUnit"));

    // driver.close();

    }

    @Test
    public void googleSearch2()
    {
    driver.get("http://www.google.com");

    WebElement searchArea = driver.findElement(By.name("q"));
    searchArea.sendKeys("JUnit");

    WebElement searchButton = driver.findElement(By.name("btnG"));
    searchButton.click();

    String pageSource = driver.getPageSource();
    assertTrue(pageSource.contains("JUnit"));

    // driver.quit();

    }

    }

    Comment by rhy — September 7, 2012 @ 22:49

    • Hi!

      What about storing all driver instances in a static list and close them in the after class method? I just verified that this code doesnt leave any browser windows running.

      package com.clickfox.selenium.wd.util;

      import org.junit.AfterClass;
      import org.junit.Test;
      import org.junit.runner.RunWith;
      import org.junit.runners.Parameterized;
      import org.openqa.selenium.By;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.WebElement;
      import org.openqa.selenium.firefox.FirefoxDriver;

      import java.util.Collection;
      import java.util.LinkedList;
      import java.util.List;

      import static junit.framework.Assert.assertTrue;

      @RunWith(Parameterized.class)
      public class ParameterizedClassExample {
      private static List allDrivers = new LinkedList();
      private WebDriver driver;

      public ParameterizedClassExample(WebDriver driver) {
      this.driver = driver;
      allDrivers.add(driver);
      }

      @Parameterized.Parameters
      public static Collection drivers() {
      List drivers = new LinkedList();
      drivers.add(new WebDriver[]{new FirefoxDriver()});
      drivers.add(new WebDriver[]{new FirefoxDriver()});

      return drivers;
      }

      @AfterClass
      public static void teardown() {
      System.out.print(“This is the AfterClass.”);

      for (WebDriver webDriver : allDrivers) {
      webDriver.quit();
      }
      }

      @Test
      public void googleSearch() {
      driver.get(“http://www.google.com”);

      WebElement searchArea = driver.findElement(By.name(“q”));
      searchArea.sendKeys(“JUnit”);

      WebElement searchButton = driver.findElement(By.name(“btnG”));
      searchButton.click();

      String pageSource = driver.getPageSource();
      assertTrue(pageSource.contains(“JUnit”));
      }

      @Test
      public void googleSearch2() {
      driver.get(“http://www.google.com”);

      WebElement searchArea = driver.findElement(By.name(“q”));
      searchArea.sendKeys(“TestNG”);

      WebElement searchButton = driver.findElement(By.name(“btnG”));
      searchButton.click();

      String pageSource = driver.getPageSource();
      assertTrue(pageSource.contains(“TestNG”));
      }
      }

      HTH
      Thomas

      Comment by Thomas Sundberg — September 8, 2012 @ 10:08

  3. Actually there have been some improvements concerning parameterized tests in JUnit. Please have a glance at JUnit Params from Pragmatists (http://code.google.com/p/junitparams). The general idea is more or less the same, but the lib makes it easier and more convenient to define the params.

    Comment by Paweł Rychlik — October 26, 2012 @ 10:49

    • Interesting, I don’t think I have heard of JUnit Params before.

      /Thomas

      Comment by Thomas Sundberg — October 26, 2012 @ 12:52

  4. Wow. no solution to the static driver parameters problem after 2 years. It is impossible to parameterize and cleanup Selenium drivers in Junit. It can only be done in TestNG

    Comment by Alex — August 28, 2014 @ 23:54


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: