1
votes

I am new to Geb and Groovy but I have pretty good experience with Selenium 2.0 Automation framework.

Due to some requirement I need to move to Geb. Framework should have Gradle as build tool, Spock to run tests, Groovy as scripting language and Geb.

I went to Geb's official website and saw this piece of code

import geb.Browser

Browser.drive {
    go "http://myapp.com/login"

    assert $("h1").text() == "Please Login"

    $("form.login").with {
        username = "admin"
        password = "password"
        login().click()
    }

    assert $("h1").text() == "Admin Section"
}

but running it doesn't open a browser. Where we are defining browser instantiation like we do in Java: driver = new FirefoxDriver();

My questions are:

  1. What is the structure of project?
  2. How to write setup() i.e. browser instantiation and teardown() i.e. browser instantiation and quitting methods in Groovy?
  3. How to run tests using Spock?

Edit: This is the structure of one example project I downloaded from https://github.com/geb/geb-example-gradle

Where in here we have the setup() and teardown() method, which initializes the driver and quits the browser? Basically a BaseClass that has all initialization and other classes extending it?

enter image description here

1

1 Answers

3
votes

When I was learning Geb, I found this example code very helpful. It's put out by Geb, and integrates with Gradle, and also uses the Chrome and Firefox webdrivers, and PhantomJS for headless operation:

https://github.com/geb/geb-example-gradle

I'm going to answer your questions in slightly different order, which will hopefully help.

Question 1. The project structure is fairly open, as it is built with Gradle, which allows for many options. In the example in the link, there is a src/test structure, which is where you can use your normal package structure.

Question 3. You don't have to do anything to run Geb with Spock, but rather, you write your tests using some of the Spock mechanisms, such as setup:, when:, etc. blocks. You'll actually run the tests with gradle commands, as the example above will show.

Question 2. Because these tests are used with Spock style tests, you can use the typical setup and teardown methods, which are: setup() and cleanup() which are run once for each test, and setupSpec() and cleanupSpec() which are run once for each class/spec.

Hope this helps.