Module: Utils

Defined in:
lib/cuculungwa/utils.rb

Instance Method Summary collapse

Instance Method Details

#click(element) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/cuculungwa/utils.rb', line 2

def click(element)
  if @app.config['capybara']['browser'] == 'phantomjs'
    element.trigger('click')
  else
    element.click
  end
end

#finished_all_ajax_requests?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/cuculungwa/utils.rb', line 52

def finished_all_ajax_requests?
  page.evaluate_script('jQuery.active').zero?
end

#I18n_first(text) ⇒ Object



38
39
40
41
42
43
# File 'lib/cuculungwa/utils.rb', line 38

def I18n_first(text)
  text = I18n.t(text, raise: I18n::MissingTranslationData)
  return text
rescue I18n::MissingTranslationData
  return text
end

#set_app(app) ⇒ Object



10
11
12
# File 'lib/cuculungwa/utils.rb', line 10

def set_app(app)
  Capybara.app_host = @app.config['project']['default_host'][app]
end

#set_environment_and_app(environment, app) ⇒ Object



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

def set_environment_and_app(environment, app)
  Capybara.app_host = @app.config['project'][environment][app]
end

#wait_for_ajaxObject



45
46
47
48
49
50
# File 'lib/cuculungwa/utils.rb', line 45

def wait_for_ajax
  sleep 1
  Timeout.timeout(Capybara.default_wait_time, Timeout::Error) do
    loop until finished_all_ajax_requests?
  end
end

#wait_for_expected_result(timespan = @app.config['capybara']['default_wait_time'], exception_type = RSpec::Expectations::ExpectationNotMetError) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cuculungwa/utils.rb', line 18

def wait_for_expected_result(timespan = @app.config['capybara']['default_wait_time'], exception_type = RSpec::Expectations::ExpectationNotMetError)
  # Default setting: get default wait time from config.yml
  start_time = Time.now
  loop do
    begin
      yield
      break
    rescue exception_type => e
      # Expectations were not met, check if we timed out, otherwise try one more time after 10ms
      if Time.now > start_time + timespan
        puts 'Timeout.'
        raise e
      end
      sleep 0.01
    end
  end
  # for debug purpose
  # puts "Waiting for expected result took #{Time.now - _then}s."
end