SiKing

February 28, 2013

What is wrong with GroovyTestCase and Selenium?

Filed under: automation — SiKing @ 2:58 pm
Tags: ,

Sometime ago I had a problem with running Selenium with GroovyTestCase framework. Recently I started a new project and have spent some time looking at this again.

Short answer: user error! 😮

Long answer:

The problem

I instantiated all my stuff like this:

class MyTestSuite extends GroovyTestCase {
      WebDriver driver = new FirefoxDriver()
      // ... some other global variables go here

      protected void setUp() throws Exception {
            super.setUp()
            // ... some other code to run at the start of each test
      }

      protected void tearDown() throws Exception {
            driver.quit()
            super.tearDown()
      }

      void test_one() {
            driver.get("some_website")
            // ... more test code goes here
      }

      void test_two() {
            driver.get("some_other_website")
            // ... more test code goes here
      }
}

Each time I ran any of the tests, it launched like four browser windows and ran the test in one of them. 😕

new FirefoxDriver() launches the new browser, as it is suppose to. The framework, I guess, launched the class four times, once for each of my methods, and Groovy automatically places that code in the constructor. It then started parsing for all the work (all the test* methods).

The solution

You need to launch the browser (only once per test) from the setUp(), like so:

class MyTestSuite extends GroovyTestCase {
      WebDriver driver
      // ... some other global variables go here

      protected void setUp() throws Exception {
            super.setUp()
            driver = new FirefoxDriver()
            // ... some other code to run at the start of each test
      }

      protected void tearDown() throws Exception {
            driver.quit()
            super.tearDown()
      }

      void test_one() {
            driver.get("some_website")
            // ... more test code goes here
      }

      void test_two() {
            driver.get("some_other_website")
            // ... more test code goes here
      }
}

HTH, 😉

Advertisements

Leave a Comment »

No comments yet.

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

Create a free website or blog at WordPress.com.

%d bloggers like this: