Geb and code reuse

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Geb and code reuse

rsom@certifydatasystems.com
Hi,

I'm using Geb without page objects with JUnit4. I want to achieve something like this:

class AbstractGebTest {
  def login(username, password) {
    ...
  }
}

class MyGebTest {
  def testMyScenario() {
    Browser.drive {
      login('admin', 'password')
      $('a#somelink').click()
      ...
    }
  }
}

Given that Browser.drive expects a closure, and $ is a method that works inside that closure, what am I supposed to do pass to the login method for login to get access to all the methods?

R,
rahul
Reply | Threaded
Open this post in threaded view
|

Re: Geb and code reuse

rsom@certifydatasystems.com
I found out the answer:

The call to login needs to move out of the Browser.drive closure and call it's code inside a similar Browser.drive closure.

R,
rahul

On Jan 6, 2012, at 9:16 AM, Rahul Somasunderam wrote:

Hi,

I'm using Geb without page objects with JUnit4. I want to achieve something like this:

class AbstractGebTest {
  def login(username, password) {
    ...
  }
}

class MyGebTest {
  def testMyScenario() {
    Browser.drive {
      login('admin', 'password')
      $('a#somelink').click()
      ...
    }
  }
}

Given that Browser.drive expects a closure, and $ is a method that works inside that closure, what am I supposed to do pass to the login method for login to get access to all the methods?

R,
rahul

Reply | Threaded
Open this post in threaded view
|

Re: Geb and code reuse

ld@ldaley.com
On 6 Jan 2012, at 17:34, Rahul Somasunderam wrote:

> I found out the answer:
> The call to login needs to move out of the Browser.drive closure and
> call it's code inside a similar Browser.drive closure.

You would be better creating a browser instance and passing that around
manually, instead of using a drive() block.

class Utils {
       
        def login(Browser browser, String username, String password) {
                browser.username = username
                browser.password = password
                browser.login()
        }

}

A drive() block uses an individual instance of Browser each time, and is
intended to encompass an entire “process”.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Geb and code reuse

geramassen
Hi, I’m working with:
Grails 2.1
Geb 0.9.0-RC-1
selenium2.25.0
spock 0.7

I have tried what you have suggested:

class Utils  {
    def login(Browser browser, String username, String password) {
        browser.username = username
        browser.password = password
        browser.login()
    }                
}

class UserPageSpec extends GebSpec {
   
    def "User list"() {
        given:
        new Utils().login(getBrowser(), "admin", "admin")
 
        when:
        to UserPage
       
        then:
        at UserPage
    }
}

But I get the following error:

  geb.error.UnresolvablePropertyException: Unable to resolve username as a property to set on Page's Navigator context
       at geb.content.NavigableSupport.propertyMissing(NavigableSupport.groovy:146)
       at geb.Browser.propertyMissing(Browser.groovy:209)
       at ar.gob.gna.liquidacion_haberes.specs.Utils.login(Utils.groovy:8)
       at ar.gob.gna.liquidacion_haberes.specs.UserPageSpec.User list (UserPageSpec.groovy:16)

Maybe I have a different version of Browser class. Anyway, instead of that, I would like to reuse other spec methods, like:

    def "Correct Login"() {
        given:
        to HomePage
 
        expect:
        at LoginPage
 
        when:
        usuario = "admin"
        password = "admin"
        logIn.click()
       
        then:
        at HomePage
    }

So my UserPageSpec would be:

class UserPageSpec extends GebSpec {
   
    def "User list"() {
        given:
        LoginPageSpec loginSpec = new LoginPageSpec()
        loginSpec."Login correcto"()

       ...
    }
}

Here, I get this error:

  groovy.lang.MissingFieldException: No such field: $spock_sharedField__browser for class: org.codehaus.groovy.runtime.NullObject
       at geb.spock.GebSpec.getBrowser(GebSpec.groovy:37)
       at geb.spock.GebSpec.methodMissing(GebSpec.groovy:51)
       at ar.gob.gna.liquidacion_haberes.specs.UserPageSpec.User list(UserPageSpec.groovy:17)

My idea is to reuse methods, each of them corresponding to an action in a page. So a use case would be a set of these actions, from different pages, and the spec would belong to the last page I am interested on. How should I do it? I don't know if I was clear enough.
Thanks in advance.
Gerardo
Reply | Threaded
Open this post in threaded view
|

Re: Geb and code reuse

mordka
Has anyone found a way to reuse geb & spock feature methods' logic?
I was trying to use test actors (the concept described here: http://assertagile.de/archives/26_test-actors/)
Although I'm getting the same error (No such field: $spock_sharedField__browser for class)

It looks like the access to control geb driver has only the spock class being executed and the only way to modularize tests is to extend from some test base class with some helper methods.