Skip to content
June 21, 2013 / selenium34

How I stopped worrying and learned to love Geb

So as I said in a comment last week I wanted to take a bit of time and give an example of where I thought things were simply easier with Geb. For now I can’t find a live page with this specific element, but I’m sure you can modify this process to something you have seen or are currently using.

So we have a photo gallery we were working on, and it seems to be using some jquery plugin to handle the transitions. It seems to be doing a css transform on the newer browsers, which means that we aren’t changing css classes to indicate which slide is currently active. We were having a bear of a time determining which slide was active. Geb made that quite simple:

    class PhotoGalleryPage extends Page {
      private static final int SLIDE_SIZE = 800

      static content = {
        currentSlideNumber { $("span.current").text() as int }
        previousButton { $("a.prev") }
        nextButton { $("a.next") }
        //The style was simply contained -800 px; or 0 px; or -1600 px; dependent upon which slide was active
        activeSlide { $("div.crsl-slide", style: contains(getOffset() as String)) }
        photo { activeSlide.find("img")
        credit { activeSlide.find("p.credit") }
      }

      def getOffset() {
        (currentSlideNumber - 1) * SLIDE_SIZE
      }
      // Some other helper methods
    }

As you can imagine our specs were quite simple to run after that. Could we have done this in Java? Certainly. Would it have been as small of a footprint? Certainly not.

Some other things which are awesome to have:

  1. Easy access to JavaScript execution
    1. This is fantastic when determining if animations have completed (assuming they are jquery) — this is all it takes: waitFor (3) { js.exec(“return jQuery(“:animated”).length === 0″) }
    2. You could easily do the same for ajax events — and have a different waiting time, or you could configure one for ajax and have waitFor (ajax) { //code }
  2. Directly access properties on the page
    1. You’ve drive to a page, just directly access the activeSlide field or the currentSlideNumber
    2. This makes your specs very expressive
  3. More options to find items (you saw I was able to use style: contains above), there are plenty of other options.
    1. Seriously
    2. This is one of the design decisions on selenium that I don’t agree with. Had the How enumeration been responsible for creating the locators extending selenium to do custom location would have been so much easier.

Overall I’m quite pleased with Geb.

Advertisements

2 Comments

Leave a Comment
  1. kopi luwak / Apr 19 2014 2:18 am

    Great post however , I was wondering if you could write a litte more on this subject?

    I’d be very thankful if you could elaborate a little bit more.
    Thank you!

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

%d bloggers like this: