Class: Capy::Evaluator

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/capy/evaluator.rb

Instance Method Summary collapse

Constructor Details

#initializeEvaluator

Returns a new instance of Evaluator.



6
7
8
9
10
11
12
13
# File 'lib/capy/evaluator.rb', line 6

def initialize
  if page.driver.respond_to?(:header)
    page.driver.header(
      'user-agent',
      'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.55.3 (KHTML, like Gecko) Version/5.1.5 Safari/534.55.3'
    )
  end
end

Instance Method Details

#browserObject



68
69
70
# File 'lib/capy/evaluator.rb', line 68

def browser
  driver.browser
end

#capybara(script) ⇒ Object



49
50
51
# File 'lib/capy/evaluator.rb', line 49

def capybara(script)
  instance_eval script
end

#driverObject



64
65
66
# File 'lib/capy/evaluator.rb', line 64

def driver
  page.driver
end

#eval_script(script, mode, file_path = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/capy/evaluator.rb', line 15

def eval_script(script, mode, file_path = nil)
  prev_file, @current_file = @current_file, file_path if file_path
  prev_mode, @current_mode = @current_mode, mode

  case mode
  when :javascript
    javascript script
  else
    capybara script
  end
ensure
  @current_file = prev_file if file_path
  @current_mode = prev_mode
end

#host(app_host) ⇒ Object



72
73
74
# File 'lib/capy/evaluator.rb', line 72

def host(app_host)
  Capybara.app_host = app_host
end

#import(file_name) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/capy/evaluator.rb', line 30

def import(file_name)
  script_file = if @current_file
      File.join(File.dirname(@current_file), file_name)
    else
      file_name
    end

  if File.exists?(script_file)
    eval_script(File.read(script_file), @current_mode, script_file)
  else
    raise "No such file: #{script_file}"
  end
end

#javascript(script) ⇒ Object Also known as: js



44
45
46
# File 'lib/capy/evaluator.rb', line 44

def javascript(script)
  page.evaluate_script script.sub(/\A#!.*\n/, '')
end

#stopObject



76
77
78
# File 'lib/capy/evaluator.rb', line 76

def stop
  Capy.start_shell(self)
end

#take_screenshot(png_path = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/capy/evaluator.rb', line 53

def take_screenshot(png_path = nil)
  png_path = gen_uniq_file_name('Screen Shot', 'png') unless png_path
  case Capybara.current_driver
  when :webkit
    driver.render(png_path)
  else
    browser.save_screenshot(png_path)
  end
  png_path
end