Search code examples
page-object-gem

adding type="date" to the text_field elements


I have an input element that has the type="date" as an attribute and when I attempt to define a page-object element as text_field then it is not found.

This is the html

<input id="dteVStartPicker" type="date"style="">

I'm asking as i have a widget created and would like to use the

class SendkeyField < PageObject::Elements::TextField
,,,
PageObject.register_widget :sendkey_field, SendkeyField, :text_field

This is what i would like my page_object to be

 sendkey_field(:start_date, :id="dteVStartPicker")

Solution

  • There is an outstanding feature request for this - https://github.com/cheezy/page-object/issues/307.

    For now, I would suggest monkey-patching the functionality into the Accessors module. This is basically what the feature request will need to add.

    require 'page-object'
    
    module PageObject
      module Accessors
        def date_field(name, identifier={:index => 0}, &block)
          define_method("#{name}_element") do
            return call_block(&block) if block_given?
            platform.date_field(identifier.clone)
          end
    
          define_method("#{name}?") do
            self.send("#{name}_element").exists?
          end    
    
          define_method(name) do
            self.send("#{name}_element").value
          end
    
          define_method("#{name}=") do |value|
            self.send("#{name}_element").value = value
          end
        end
      end
    end
    

    Then your page object would be:

    class MyPage
      include PageObject
    
      date_field(:start_date, id: 'dteVStartPicker')
    end
    

    Which gives a setter and getter for the field:

    page = MyPage.new(browser)
    page.start_date = '2019-11-02'
    p page.start_date
    #=> '2019-11-02'