follow me icons

Thursday, May 24, 2012

Capybara using contains to select an element

Suppose you have a select element that has a random number as the id like the one below:
< select class="select_xpath_class" name="myMethods[select_method_id_23749837]" id="select_method_id_23749837">
Please select... Select1 Select2 Select3

Capybara method of select won't work:"Select1", :from => "select_method_id")
This is because it will only find the exact match of the id.

To solve this we can use the find method directly with xpath:
find(:xpath, "//*[contains(@id, 'select_method_id')]").select("Select1")

GEB - selecting an element with partial name

To select an element on a webpage with a partial name in Geb and Groovy:
   static content = {
        elementField {elementName -> $('select', name: contains(/button[$elementName/))}

   def selectContent(String selectOptionName, String value) {

Check element exists in GEB

To check whether an element exists in a page using GEB:
   static content = {
        searchVisibleField(required: false) {$("#search-id")}

    def isSearchVisible()
        if (browser.searchVisibleField() == geb.navigator.EmptyNavigator.instance())  {
        else {
searchVisibleField returns the instance of geb.navigator.EmptyNavigator class so we can use that to check whether an element exists or not in a page. Reference from The Book of GEB

Wednesday, May 23, 2012

Selecting radio button value in GEB

To select a radio button value in GEB using Groovy: in the page model define:
    static content = {
        selectRadioButtonOptionField {option -> $("input[type='radio']", name: "radioButtonName", value: option)}

    def selectRadioButtonOption(String radioButtonValue) {
I could not use the .value or = to select the radio button so I need to use the click method to select.