Module: ProxyTester::SpecHelper::Capybara

Includes:
Capybara::DSL
Defined in:
lib/proxy_tester/rspec/helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.runtimeObject



10
11
12
# File 'lib/proxy_tester/rspec/helper.rb', line 10

def runtime
  @__runtime
end

Instance Method Details

#cleanup_reportsObject



39
40
41
# File 'lib/proxy_tester/rspec/helper.rb', line 39

def cleanup_reports
  old_report_directories.each { |d| FileUtils.rm_rf d }
end

#keep_report_directoriesObject



35
36
37
# File 'lib/proxy_tester/rspec/helper.rb', line 35

def keep_report_directories
  @__keep_report_directories ||= 5
end

#offlineObject



15
16
17
# File 'lib/proxy_tester/rspec/helper.rb', line 15

def offline
  @__offline ||= false
end

#offline=(status) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/proxy_tester/rspec/helper.rb', line 19

def offline=(status)
  if status == true
    @__offline = true 
  else
    @__offline = false
  end
end

#offline?Boolean

Returns:

  • (Boolean)


138
139
140
141
142
# File 'lib/proxy_tester/rspec/helper.rb', line 138

def offline?
  return true if  offline == true
  
  false
end

#proxyObject



27
28
29
# File 'lib/proxy_tester/rspec/helper.rb', line 27

def proxy
  @__proxy ||= ProxyTester::CapybaraProxy.new
end

#proxy_pacObject



31
32
33
# File 'lib/proxy_tester/rspec/helper.rb', line 31

def proxy_pac
  @__proxy_pac ||= ProxyTester::CapybaraProxyPac.new
end

#set_offline(status) ⇒ Object



144
145
146
# File 'lib/proxy_tester/rspec/helper.rb', line 144

def set_offline(status)
  self.offline = status
end

#take_screenshotObject



43
44
45
46
47
# File 'lib/proxy_tester/rspec/helper.rb', line 43

def take_screenshot
  page.save_screenshot(screenshot_path, full: true)

  screenshot_path
end

#use_client_ip(ip) ⇒ Object



104
105
106
# File 'lib/proxy_tester/rspec/helper.rb', line 104

def use_client_ip(ip)
  proxy_pac.client_ip = ip
end

#use_proxy(*args) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/proxy_tester/rspec/helper.rb', line 112

def use_proxy(*args)
  if args.first.kind_of? Symbol
    case args.first
    when :host
      proxy.host, proxy.port = args.second.split(/:/)
    when :pac
      proxy_pac.pac_file = args.second
    else
      fail ProxyTester::Exceptions::SyntaxInvalid, message: "Unknown symbol: #{args.first}"
    end
  else
    proxy.host, proxy.port = args.first.split(/:/)
  end

  proxy.type = args.last[:type] if args.last.kind_of? Hash
end

#use_time(time) ⇒ Object



108
109
110
# File 'lib/proxy_tester/rspec/helper.rb', line 108

def use_time(time)
  proxy_pac.time = time
end

#use_timeout(threshold, &block) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/proxy_tester/rspec/helper.rb', line 129

def use_timeout(threshold, &block)
  old_timeout = ::Capybara.default_wait_time
  ::Capybara.default_wait_time = threshold

  block.call
ensure
  ::Capybara.default_wait_time = old_timeout
end

#use_user(name, options = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/proxy_tester/rspec/helper.rb', line 80

def use_user(name, options = nil)
  if name == :ask
    user_name = HighLine.new.ask('Please enter username: ')
    user_password = HighLine.new.ask('Please enter user password: ') { |q| q.echo = '*' }

    proxy.user = ProxyTester::User.new(name: user_name, password: user_password) 

  elsif options == :ask_password
    user_password = HighLine.new.ask('Please enter user password: ') { |q| q.echo = '*' }

    proxy.user = ProxyTester::User.new(name: name, password: user_password) 
  elsif options == :credential_merging
    user_password = HighLine.new.ask('Please enter user password: ') { |q| q.echo = '*' }
    proxy.user = ProxyTester::User.new(name: "#{ENV['USER']}-#{name}", password: user_password) 
  else
    begin
      proxy.user = ProxyTester::User.find_by!(name: name)
    rescue ActiveRecord::RecordNotFound
      ProxyTester.ui_logger.fatal "User \"#{name}\" could not be found. Please make use he's in the user database. Exiting."
      raise ProxyTester::Exceptions::ProxyUserInvalid, JSON.dump(user: name)
    end
  end
end

#use_user_agent(agent) ⇒ Object



54
55
56
# File 'lib/proxy_tester/rspec/helper.rb', line 54

def use_user_agent(agent)
  page.driver.headers = { "User-Agent" => agent } 
end

#view_screenshotObject Also known as: show_screenshot



49
50
51
# File 'lib/proxy_tester/rspec/helper.rb', line 49

def view_screenshot
  show_image(take_screenshot)
end

#visit(url) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/proxy_tester/rspec/helper.rb', line 58

def visit(url)
  proxy_pac.url = url

  if !proxy_pac.blank? and !proxy_pac.direct?
    proxy.host = proxy_pac.host
    proxy.port = proxy_pac.port
  end

  register_driver proxy
  use_driver proxy
  use_user_agent 'Mozilla/5.0 (X11; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0'

  return if offline?

  begin
    super(url)
  rescue ::Capybara::Poltergeist::TimeoutError
    raise ProxyTester::Exceptions::FetchUrlTimeout, JSON.dump(url: url)
  end

end