Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow all form field types to be called using 1 element type: browser.form_field #664

Closed
sjieg opened this issue Oct 4, 2017 · 10 comments
Closed
Labels

Comments

@sjieg
Copy link
Contributor

sjieg commented Oct 4, 2017

As requested by Titus, putting a ticket in for a possible new feature.

In the category "thinking out loud": I've been working the last few weeks on quite some form validation scripts, creating my own custom form_field_to_value functions and it made me wonder. Could we make (or is there already) a selector that groups input, select & textfield into 1 selector: form_field. In practise making it look something as following.

Expected Behavior -

browser.form_field(:name => 'country').set('netherlands') # Selects the option from a <select> field with the value 'netherlands'

browser.form_field(:id => 'acceptTerms').set(true) # Checks the <input type="checkbox">

browser.form_field(:class => 'descriptionField').set('My life story...') # writes the string in the <textarea> field

This would make it much easier to convert configuration values to actual form input values, without having to worry about which element type you're trying to set.

@titusfortner
Copy link
Member

This might dovetail with #405. What if we define set method on input class, lookup the type and take the action on an object of that subtype.

@jkotests
Copy link
Member

jkotests commented Oct 4, 2017

@titusfortner, are you suggesting that someone would use browser.input.set? That'd mean that select lists and textarea are excluded.

@titusfortner
Copy link
Member

Oh right... I guess #to_subtype handles all of it, though... FormField class with set method that returns correct element object? Then alias #set for each of those Element objects? Page object gems won't need the extra logic.

@sjieg
Copy link
Contributor Author

sjieg commented Oct 5, 2017

I was thinking in the line of:

def form_field(args*)
  case args
    when b.input(args).exists?
       return b.input(args)
    when b.select(args).exists?
       return b.select(args)
    when b.text_area(args).exists?
       return b.text_area(args)
     else
       raise Watir::ElementNotFoundError
  end
end

Or maybe, to reduce redundancy, make an array with form_field_types ['input', 'select', 'text_area'] and loop trough them checking for it existing.

@titusfortner
Copy link
Member

The intention is to make it so you don't have to specify the element type or figure out the appropriate input method?

What if instead of adding another method, we just duck type it? Everything that responds to #set works. So you don't need #form_field and can just use #element.

def set(*args)
  return to_subtype.set(*args) if self.instance_of?(Watir::Element)
  raise "#{self.class} does not support the #set method"
end

This would have the advantage of making Page Object implementations easier. This could also make it easier to implement select list type behavior for weird divs that some devs like to do by simply adding a #set method to it that class.

@titusfortner
Copy link
Member

Hmm, though this wouldn't work for the RadioSet class we made since #to_subtype will return a Radio which already has #set defined...

@sjieg
Copy link
Contributor Author

sjieg commented Oct 5, 2017

To solve the radio subtype, how about something like this:

# element(selectors).set(value)
if element.type == 'radio' and value.is_a? String
  browser.input(selectors, :value => value).set
end

Maybe this could actually be implemented in the RadioSet class. Keep the current behaviour if no value is given, else look for the same selectors with a :value added to it.

@titusfortner titusfortner added the Capabilities An issue with Capabilities Class label Nov 20, 2017
@titusfortner titusfortner removed the Capabilities An issue with Capabilities Class label Mar 4, 2021
titusfortner added a commit to titusfortner/watir that referenced this issue Mar 19, 2021
titusfortner added a commit to titusfortner/watir that referenced this issue Mar 19, 2021
titusfortner added a commit to titusfortner/watir that referenced this issue Mar 19, 2021
@titusfortner
Copy link
Member

This was implemented and will be available in 7.0.0.beta2

@sjieg
Copy link
Contributor Author

sjieg commented Mar 24, 2021

Cool stuff, thanks @titusfortner !

@titusfortner
Copy link
Member

Only took, what, 3 1/2 years? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants