Class: WatirDrops::PageObject
- Inherits:
-
Object
- Object
- WatirDrops::PageObject
- Defined in:
- lib/watir_drops/page_object.rb
Instance Attribute Summary collapse
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
Class Method Summary collapse
- .element(name, &block) ⇒ Object
- .elements ⇒ Object
- .page_url ⇒ Object
- .use ⇒ Object
- .visit(*args) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ PageObject
constructor
A new instance of PageObject.
- #submit_form(model) ⇒ Object
- #title ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize ⇒ PageObject
Returns a new instance of PageObject.
68 69 70 |
# File 'lib/watir_drops/page_object.rb', line 68 def initialize @browser = WatirSession.browser end |
Instance Attribute Details
#browser ⇒ Object (readonly)
Returns the value of attribute browser.
66 67 68 |
# File 'lib/watir_drops/page_object.rb', line 66 def browser @browser end |
Class Method Details
.element(name, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/watir_drops/page_object.rb', line 17 def element(name, &block) define_method(name) do |*args| self.instance_exec(*args, &block) end define_method(name.to_s.pluralize) do |*args| selector = self.instance_exec(*args, &block).instance_variable_get('@selector') element_type = selector.delete(:tag_name) || 'element' browser.send(element_type.pluralize, selector) end define_method("#{name}=") do |val| watir_element = self.instance_exec &block watir_element.wait_until_present case watir_element when Watir::Radio watir_element.set if val when Watir::CheckBox val ? watir_element.set : watir_element.clear when Watir::Select watir_element.select val when Watir::Button watir_element.click # TODO - Email & Password types are not set to UserEditable in Watir when Watir::Input watir_element.wd.clear watir_element.send_keys val else watir_element.click if val end end elements << {name.to_sym => block} end |
.elements ⇒ Object
13 14 15 |
# File 'lib/watir_drops/page_object.rb', line 13 def elements @elements ||= [] end |
.page_url ⇒ Object
7 8 9 10 11 |
# File 'lib/watir_drops/page_object.rb', line 7 def page_url define_method("goto") do |*args| browser.goto yield(*args) end end |
.use ⇒ Object
60 61 62 |
# File 'lib/watir_drops/page_object.rb', line 60 def use new.tap { yield if block_given? } end |
.visit(*args) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/watir_drops/page_object.rb', line 53 def visit(*args) new.tap do |page| page.goto(*args) yield if block_given? end end |
Instance Method Details
#submit_form(model) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/watir_drops/page_object.rb', line 72 def submit_form(model) intersect = self.class.elements.map(&:keys).flatten & model.keys intersect.each do |val| self.send("#{val}=", model.send(val)) end end |
#title ⇒ Object
79 80 81 |
# File 'lib/watir_drops/page_object.rb', line 79 def title browser.title end |
#url ⇒ Object
83 84 85 |
# File 'lib/watir_drops/page_object.rb', line 83 def url browser.url end |