Complex UI elements in Geb browser functional tests

Examples of interacting with complex UI elements in Geb browser functional tests..

Object Partners

Browser functional testing is a great way to verify your web application fully works end-to-end. Interacting with standard web page elements like links and forms is often straightforward, but driving more complex UI elements such as sliders and date pickers can get tricky. In this post we’ll go through some examples of working with these types of elements using the Geb functional testing framework.

Using mouse

In this example we’ll move a slider element using the mouse. Geb provides an interact block that we can use to drive the mouse using Selenium Actions. Here we’ll click the slider element, move it horizontally be a given number of pixels, then release the slider.

 static content = {
    sliderHandle { $(".ui-slider-handle") }
  }

  void moveSlider(Integer numPixelsX) {
    interact {
      clickAndHold(sliderHandle)
      moveByOffset(numPixelsX, 0)
      release()
    }
  }

Using keyboard

Another way to interact with these types of elements is using the keyboard. With Geb we can use the left-shift operator to send keystrokes to any UI element.

For example, if you wanted to send the individual keys for each letter in the word “value” to an input field, you can use:

$("#myInputField") << "value"

Or if you want to send a non-character keystroke, such as using the right arrow to move a slider, you can use:

$(".ui-slider-handle") << Keys.ARROW_RIGHT

Using raw Javascript

And if all else fails, you can use Geb’s support for running raw Javascript inside your test to interact with an element. One common use of this technique is to click an element that is difficult to make visible using a test. Maybe something like a mouse-over menu that is tricky to hover over in a test. Selenium (which Geb is based upon) doesn’t allow interacting with hidden elements, but we can use some jQuery in our test to get around this restriction.

static content = {
  hiddenLink { $("#hiddenLink") }
}

void clickHiddenLink() {
  hiddenLink.jquery.show()

  hiddenLink.click()
}

Example code

A full Geb example including some the code samples above is on my Geb example Github repo.

Happy testing!

Share this Post

Related Blog Posts

Unknown

Gr8Conf US 2015 Conference Recap

August 3rd, 2015

Recap of the workshops and talks at the Gr8Conf US 2015 conference.

Object Partners
Unknown

A Unix Guy looks at Powershell

July 28th, 2015

A Unix Guy looks at Powershell

Mike Hostetler
Unknown

Getting smarter with git

June 23rd, 2015

Review some command-line hits for more fine-grained git control

Mike Hostetler

About the author