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, 😉

August 22, 2011

Groovy Selenium WebDriver and SoapUI, part 3

Filed under: automation — SiKing @ 4:25 pm
Tags: , ,

So I got my environment set up and I have been busy coding up new Selenium-WebDriver test suite for a few weeks now.

I first wanted to just state that as great of a tool as SoapUI is, their community support just plain sucks! Any time I navigate to their discussion fora, I can actually hear the crickets in the distance. If you need some help, may I recommend the Service Testing using soapUI (needs login) group at LinkedIn.

The next bit of complaint that I have with SoapUI, is how poorly it integrates with Eclipse!

Now back to your regular programming …

the quick and dirty

So the most obvious, and perhaps the easiest way, to get Selenium and SoapUI to cooperate is:

  1. Install SoapUI.
  2. Download Selenium (you need the selenium-server-standalone-2.*.jar) and drop it into your SoapUI installation (into %SOAPUI_HOME%\bin\ext).
  3. Fire up SoapUI; start a new Project; create a new test case; add a new Groovy step; copy-paste the sample code into the step. I made a few modification: drop the package line, drop the class Selenium2Example and void main lines along with the closing brackets, and change the System.out.println to log.info. My final (full) test code is below.
  4. Click Play. You should see Firefox starting up, navigating to Google, and afterwards you should see the SoapUI log entries.
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.support.ui.ExpectedCondition
import org.openqa.selenium.support.ui.WebDriverWait

        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new FirefoxDriver()

        // And now use this to visit Google
        driver.get("http://www.google.com")

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"))

        // Enter something to search for
        element.sendKeys("Cheese!")

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit()

        // Check the title of the page
        log.info("Page title is: " + driver.getTitle())
        
        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!")
            }
        });

        // Should see: "cheese! - Google Search"
        log.info("Page title is: " + driver.getTitle())
        
        //Close the browser
        driver.quit()

Success, the two can talk to each other. ❗ You will probably notice some errors, due to Google updating their site and the above code no longer works there; however, the proof of concept is there. For a better example, see my sample code.

the difficult way

I did not get as much chance to play with the SoapUI as I would have liked, but I wanted to get this published. The above will work for simple Selenium steps. However, for more complex steps you probably want a little more.

Unfortunately, I have not had a chance to explore this yet, but the general idea is:

  • SoapUI is a java project, it must be in some .jars somewhere, hopefully in just one.
  • Import that into your Groovy (Java) project.
  • Then you should be able to call appropriate functions from your code.

All this sounds quite easy, but I am certain that it will need more than what I have here. If anyone manages to get this to work, I would be really curious to hear from you.

August 8, 2011

Groovy Selenium WebDriver and SoapUI, part 2

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

I initially assumed that I would have this post up within a week of the previous, but life got in the way as it so often does. 😐

Now that I got everything set up, it’s time to move on. In this second part, I am going to concentrate on getting Selenium 2 WebDriver going with Groovy. Selenium 2 out of the box supports .NET and Java, so why Groovy? There are several reasons: 1. SoapUI (to be discussed later) supports Groovy natively, 2. I like scripted languages for test automation better than compiled languages, and 3. why not?

I had been working with Selenium RC and .NET for some time, and had put together the basis of an automation framework. So my first step was to rewrite everything in Java, and just call it Groovy. All this was actually surprisingly easy to accomplish, especially with my very limited knowledge of both Java and Groovy. I want to stress that what I have here (the previous two links) is just the beginnings of a test framework. It meets my initial requirements: that it run in Groovy, and that it use 100% WebDriver. I am certain that it can (and will) be further optimized, but I would hope that anyone starting with this will get a good idea of where this is heading.

still to do:

  • Get everything running from the command line.
  • Use it in a real-world project. :mrgreen:
  • Make everything more Groovy.

couple of surprises along the way:

  • Man, love the lack of Selenium server. No more Java memory crashes. 👿 Yay!
  • In JUnit4 the signature of asserts changed from (NUnit’s) Assert.AreEqual(expected, actual, message) to Assert.assertEquals(message, expected, actual). The message goes at the beginning?
  • In webDriver, getting the value of a textbox changed from selenium.GetValue("name=q") to txtSearch.getAttribute("value"). Nice!

July 23, 2011

Groovy Selenium WebDriver and SoapUI, part 1

Filed under: automation,linux,windows — SiKing @ 4:13 pm
Tags: , ,

I recently started a new job and a new project that called for me to make use of some things I have used separately over the past few years: combine SoapUI and Selenium into one framework, and make them work together – actually pass information from one to the other and back. While I am at it, I thought I would dust off some skillz from a box that I have not been in for some time: Eclipse and Java (I know the title says Groovy, I’ll get to that).

setting it all up on Windows

Of course at work they must run Windows. 😦

  1. Download and install (unpack?) Eclipse Classic 3.7, and run the Check for Updates. Mental note: gotta look at Eclipse for Testers, someday.
  2. You need to give it Java: set JAVA_HOME and add %JAVA_HOME%\bin to the PATH. The first one that I tried – there were like half-dozen different versions on my machine 🙄 – Eclipse complained that it is missing something called jvm.dll. Better get the real thing.
  3. If you create a shortcut to launch Eclipse from your desktop, I found that it is a good idea to set the “Start in:” field to the same thing as what your workspace is.
    Eclipse shortcut properties
  4. Install SoapUI. This plugin needs some post-install work/commentary. If you also run the SoapUI IDE, especially a different version than what you just downloaded, the two will share one %userProfile%\soapui-settings.xml and there could be collisions; I would really like to find a way to relocate this file for the Eclipse plugin. Also, if you did not start Eclipse from your workspace – point 3 above – then you are going to 1) possibly overwrite your %userProfile%\default-soapui-workspace.xml, and 2) possibly pollute your Eclipse installation with three *.log files that SoapUI always creates on startup. Lastly: what used to be %SOAPUI_HOME%\bin\ext (external jars that should be added to soapUI classpath, for example JDBC drivers) is now %userProfile%\ext; another thing I would really like to relocate for the Eclipse plugin.
  5. Install Groovy. The instructions say to use http://dist.codehaus.org/groovy/distributions/greclipse/snapshot/e3.7/, but if you read between the lines, you will notice this is the development build and as such it changes often, like almost daily. I used http://dist.springsource.org/release/GRECLIPSE/e3.7/ and it seems to have worked right out of the box.
  6. Install SVN. I am not sure why Eclipse still comes pre-installed with CVS; does anyone still use this? When you’re done, make sure it worked. Normally there are problems.

setting it all up on Linux

Of course at home I run Linux. Surprisingly the Linux setup was a little more work. 😕

  1. I run Linux Mint 9 (Ubuntu Lucid Lynx – LTS); the repos have only Eclipse 3.5, which is way too outdated by now. Download and install (unpack?) Eclipse Classic 3.7, and run the Check for Updates.
  2. LM9 comes set up with OpenJDK. Eclipse will run with this, however, when doing this last time, I ran into some problems, like various things kept crashing the JVM. Somewhere on the Eclipse site (unfortunately, I cannot find the link now) they suggested that you use genuine Sun Java. This is accomplished with sudo apt-get install sun-java-jdk, and then you need to modify your eclipse.ini to point to /usr/lib/jvm/java-6-sun/bin/java.
  3. Install SoapUI. I’m still having problems with this one.
  4. Install Groovy. Use the same location as mentioned above, which makes it work right out of the box.
  5. Install SVN. Fix the problems.

Create a free website or blog at WordPress.com.