Class: WatirDrops::PageObject

Inherits:
Object
  • Object
show all
Defined in:
lib/watir_drops/page_object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePageObject

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

#browserObject (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

.elementsObject



13
14
15
# File 'lib/watir_drops/page_object.rb', line 13

def elements
  @elements ||= []
end

.page_urlObject



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

.useObject



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

#titleObject



79
80
81
# File 'lib/watir_drops/page_object.rb', line 79

def title
  browser.title
end

#urlObject



83
84
85
# File 'lib/watir_drops/page_object.rb', line 83

def url
  browser.url
end