module ProxyTester::SpecHelper::Capybara

Public Class Methods

runtime() click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 10
def runtime
  @__runtime
end

Public Instance Methods

cleanup_reports() click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 39
def cleanup_reports
  old_report_directories.each { |d| FileUtils.rm_rf d }
end
keep_report_directories() click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 35
def keep_report_directories
  @__keep_report_directories ||= 5
end
offline() click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 15
def offline
  @__offline ||= false
end
offline=(status) click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 19
def offline=(status)
  if status == true
    @__offline = true 
  else
    @__offline = false
  end
end
offline?() click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 138
def offline?
  return true if  offline == true
  
  false
end
proxy() click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 27
def proxy
  @__proxy ||= ProxyTester::CapybaraProxy.new
end
proxy_pac() click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 31
def proxy_pac
  @__proxy_pac ||= ProxyTester::CapybaraProxyPac.new
end
set_offline(status) click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 144
def set_offline(status)
  self.offline = status
end
show_screenshot()
Alias for: view_screenshot
take_screenshot() click to toggle source
# 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) click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 104
def use_client_ip(ip)
  proxy_pac.client_ip = ip
end
use_proxy(*args) click to toggle source
# 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) click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 108
def use_time(time)
  proxy_pac.time = time
end
use_timeout(threshold, &block) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 54
def use_user_agent(agent)
  page.driver.headers = { "User-Agent" => agent } 
end
view_screenshot() click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 49
def view_screenshot
  show_image(take_screenshot)
end
Also aliased as: show_screenshot
visit(url) click to toggle source
Calls superclass method
# 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

Private Instance Methods

old_report_directories() click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 192
def old_report_directories
  Dir.glob(File.join(ProxyTester.config.reports_directory, '*')).keep_if { |d| FileTest.directory? d }.sort.reverse.drop(keep_report_directories)
end
operating_system() click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 196
def operating_system
  case RUBY_PLATFORM.downcase
  when /win32/
    :win32
  when /linux/
    :linux
  when /darwin/
    :darwin
  else
    :unknown
  end
end
register_driver(local_proxy) click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 154
def register_driver(local_proxy)
  if local_proxy.blank?
    options = {
      js_errors: false,
      phantomjs_logger: $stderr,
    }
  else 
    options = {
      phantomjs_options: local_proxy.as_phantomjs_arguments,
      js_errors: false,
      phantomjs_logger: $stderr,
    }
  end

  ProxyTester.ui_logger.debug('options: ' + options.to_s)

  ::Capybara.register_driver local_proxy.to_sym do |app|
    ::Capybara::Poltergeist::Driver.new(app, options)
  end

  ::Capybara.run_server        = false
end
screenshot_directory() click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 177
def screenshot_directory
  if @__screenshot_dir.blank?
    @__screenshot_dir = File.join(ProxyTester.config.reports_directory, ProxyTester::SpecHelper::Capybara.runtime)
    FileUtils.mkdir_p @__screenshot_dir
  end

  @__screenshot_dir
end
screenshot_path() click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 186
def screenshot_path
  return @__screenshot_path if @__screenshot_path

  @__screenshot_path = File.join(screenshot_directory, "#{example.full_description.parameterize}.png")
end
show_image(image) click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 209
def show_image(image)
  viewer = case operating_system
           when :linux
             'xdg-open'
           when :win32
             'start'
           when :darwin
             'open'
           else
             'xdg-open'
           end

  system("#{viewer} #{image.shellescape}")
end
use_driver(proxy) click to toggle source
# File lib/proxy_tester/rspec/helper.rb, line 150
def use_driver(proxy)
  ::Capybara.current_driver = proxy.to_sym
end