Talking testing, agility and automation... and anything else.


Geb: Get Selected Text From A Select Dropdown

gebMy current client has decided to migrate their automated ui tests over to Geb, thus, I’ve been busily ramping up on it for the last couple weeks. Geb, written in Groovy, sits atop Selenium Webdriver and provides some great conveniences, including elegant css selectors and page object support. Additionally, using Spock as your test runner allows you to write your tests in a given/when/then dsl.

I’m finding Geb to be a great tool but like all tools, it has its quirks. Take getting the currently selected text from a select box… Geb does not currently (0.9.2) have an api for this. It has support for getting the currently selected value but you’ll have to do a little work to get the selected text. With a little trial and error (heavy on error), I was able to craft a reasonable solution to this problem in a page object selector. Here’s my example code…

Page Object Code (TestyPage.groovy):

Test Code (testy.groovy):

The trick here is in the page object. In the selector dropdownSelectedText, we use .find to get the first option tag, down the dom from dropdown, that has a value of dropdown.value() (in this case ‘ggg’) and then get the text from that option tag.

Reasonably clever…

Plusone Linkedin Facebook Twitter Digg Email

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.