Module: TestHarness::UIComponentHelper

Defined in:
lib/ui_component_helper.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

If the UIComponent is sent a message it does not understand, it will forward that message on to its #browser but wrap the call in a block provided to the the browser’s ‘#within` method. This provides convenient access to the browser driver’s DSL, automatically scoped to this component.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ui_component_helper.rb', line 34

def method_missing(name, *args, &block)
  if respond_to?(name)
    if component.within
      browser.within(component.within) do
        browser.send(name, *args, &block)
      end
    else
      browser.send(name, *args, &block)
    end
  else
    super
  end
end

Instance Method Details

#browserObject



22
23
24
25
26
27
# File 'lib/ui_component_helper.rb', line 22

def browser
  @browser ||= begin
    raise MissingConfiguration.new('TestHarness.browser must be defined') if configuration.browser.nil?
    configuration.browser
   end
end

#componentObject



9
10
11
12
# File 'lib/ui_component_helper.rb', line 9

def component
  parent_class = TestHarness::Utilities.get_parent_class(self.class)
  parent_class ? parent_class.component : nil
end

#configurationObject



18
19
20
# File 'lib/ui_component_helper.rb', line 18

def configuration
  TestHarness.configuration
end

#formObject

Used to set form elements for submittal by the method submit! In the UIDriver, you can use:

form.username = '[email protected]'
form.password = 'password'
submit!


112
113
114
# File 'lib/ui_component_helper.rb', line 112

def form
  @form ||= TestHarness::FormStruct.new(browser)
end

#mmObject



5
6
7
# File 'lib/ui_component_helper.rb', line 5

def mm
  TestHarness.mm
end

#resetObject



14
15
16
# File 'lib/ui_component_helper.rb', line 14

def reset
  @form = nil
end

#select(*args, &block) ⇒ Object

Since Kernel#select is defined, we have to override it specifically here.



68
69
70
71
72
73
74
75
76
# File 'lib/ui_component_helper.rb', line 68

def select(*args, &block)
  if component.within
    browser.within(component.within) do
      browser.select(*args, &block)
    end
  else
    browser.select(*args, &block)
  end
end

#show!Object



86
87
88
# File 'lib/ui_component_helper.rb', line 86

def show!
  visit component_path
end

#submit!Object

Used to submit elements set by the form method In the UIDriver, you can use:

form.username = '[email protected]'
form.password = 'password'
form.yes = true    # for checkbox
form.radio1 = true # for radio button

submit!


99
100
101
102
103
104
105
# File 'lib/ui_component_helper.rb', line 99

def submit!
  if has_css?(locator = component.submit)
    find(:css, component.submit).click
  else
    click_on component.submit
  end
end

#visit(path) ⇒ Object

We don’t want to go through the method_missing above for visit, but go directly to the browser object



80
81
82
83
84
# File 'lib/ui_component_helper.rb', line 80

def visit(path)
  path = "%s:%s%s" % [server_host, Capybara.server_port, path] if path !~ /^http/

  browser.visit(path)
end

#wait_for_ajaxObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ui_component_helper.rb', line 48

def wait_for_ajax
  start_time = Time.now
  until page.evaluate_script('jQuery.isReady&&jQuery.active==0') or (start_time + ajax_timeout) < Time.now do
    sleep 1
  end

  return unless block_given?

  # Now try to get the list of data few times until we either fail or timeout (2 seconds)
  20.times.each do |index|
    begin
      return true if yield
    rescue Exception => e
      sleep 0.5
    end
  end
  false
end