[Celerity-users] Suggestion: generic element finder

Kamal Fariz Mahyuddin kamal.fariz at gmail.com
Tue Mar 24 22:26:51 EDT 2009


I use Webrat's semantics of always searching by multiple ways in order
of priority.

Form elements: search by id, then by name attribute, then by label
Buttons: search by value, then by id, then by inner text, then by alt attribute
Link: search by inner text, then by title attribute, then by id

For form elements, I created a custom Element called GenericField
which is a union of TextField, Hidden, Radio, Checkbox, and
SelectList. This is more for ease of locating. Setting though is
different for each actual field, which is a separate post.

I've implemented what I describe above in my webrat fork
http://github.com/kamal/webrat/tree/1543bc64ecf85054f9a033e1fc7670806886b41e/lib/webrat/celerity

On Wed, Mar 25, 2009 at 10:02 AM, Ashley Moran
<ashley.moran at patchspace.co.uk> wrote:
> Hi
>
> As far is I can tell, all input elements respond to #set in a sensible way.
>  So you can use Celerity::SelectList#set the same as Celerity::TextField.
>  The problem is, you can't *find* an element of any type with one method.
>  This causes problems if, as I've had to do recently, you want to sumbit a
> form without caring what type of elements you are filling out.
>
> Currently, I'm doing this (selected parts of my the Cucumber steps code):
>
>  def set_form_attribute_to_value(label_text, value)
>    label_element = label_by_text(label_text)
>    form_element = element_by_id(label_element.for)
>    form_element.set(format_value_for_form_element(value, form_element))
>  end
>
>  def element_by_id(id)
>    element_by_attribute(:id, id)
>  end
>
>  def element_by_attribute(html_attribute, value)
>    element_by_xpath("//*[@#{html_attribute.to_s}='#{value}']")
>  end
>
>  def element_by_xpath(xpath)
>    element = @browser.element_by_xpath(xpath)
>    raise RuntimeError.new("XPath found no elements: #{xpath}") unless
> element.exists?
>    element
>  end
>
> (I think I copied everything you need to understand what I mean.)
>
> This problem could be simplified if you could instead write:
>
>  @browser.element(:id => "or :name => whatever").set("blah")
>
> WDYT?
>
> Ashley
>
> --
> http://www.patchspace.co.uk/
> http://www.linkedin.com/in/ashleymoran
> http://aviewfromafar.net/
> http://twitter.com/ashleymoran
>
>
>
>
>
>
> _______________________________________________
> Celerity-users mailing list
> Celerity-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/celerity-users
>


More information about the Celerity-users mailing list