p0deje/watirsome

Unable to use input or element; is this by design?

jeffnyman opened this issue · 1 comments

Looking at the source, I'm not sure if this is by design or not. Consider the following:

class LoginPage
  include Watirsome

  element :name, id: 'name'
  # input :name, id: 'name'
  # text_field :name, id: 'name'
end

Watirsome.settable << :input
Watirsome.settable << :element

browser = Watir::Browser.new
browser.goto('http://localhost:9292/practice')
page = LoginPage.new(browser)
page.name_element.set 'admin'

This does not work. I get the error:

in `extract_selector': expected Hash or (:how, 'what'), got ["admin"]

The exact same thing happens with the input line if you comment it out and change the last line above to page.name_input.set 'admin'. Only the third commented line in the class works.

I then tried these variations (using = rather than set):

page.name_input = 'admin'
page.name_element = 'admin'

In both cases, I get the error:

undefined method `name_input=' for #<LoginPage:0x007f977538fd60> (NoMethodError)

I realize it works with the text_field setting and I can just do that. But I'm not sure if the other approaches should work. In particular, I thought element in Watir-WebDriver could stand in for any element at all.

If you plan to just call #set on element, you don't need to modify Watirsome.settable at all. The reason why it fails for you is probably because watir-webdriver doesn't have #set method on all elements.

You could try smth like this:

class LoginPage
  include Watirsome

  element :name, id: 'name'
  input :name, id: 'name'
end

browser = Watir::Browser.new
browser.goto('http://localhost:9292/practice')
page = LoginPage.new(browser)
page.name_element.send_keys 'admin'
page.name_input.send_keys 'admin'

Just call #send_keys on elements.

If you still want to use settable accessor, it should work with something like this (though I advise to use the first option):

# be sure to call it before login page is required
Watirsome.settable << :input
Watirsome.settable << :element

class LoginPage
  include Watirsome

  element :name1, id: 'name'
  input :name2, id: 'name'
end

browser = Watir::Browser.new
browser.goto('http://localhost:9292/practice')
page = LoginPage.new(browser)
page.name1 = 'admin'
page.name2 = 'admin'