Class: Masque

Inherits:
Object
  • Object
show all
Defined in:
lib/masque.rb,
lib/masque/dsl.rb,
lib/masque/version.rb

Defined Under Namespace

Modules: DSL

Constant Summary collapse

VERSION =
"0.4.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Masque

Returns a new instance of Masque.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/masque.rb', line 16

def initialize(options = {})
  @options = options
  @driver = options[:driver] || :webkit

  case options[:capybara_compat]
  when "2.1"
    compat_capybara_21!
  when "2.0"
    compat_capybara_20!
  when "1.x"
    compat_capybara_1x!
  else # default compat mode is 1.x (currently. IMO this is most useful for crawler, but any use case can be changed this decision)
    compat_capybara_1x!
  end

  @agent = Class.new do
    include Masque::DSL

    attr_accessor :session

    def initialize(session)
      @session = session
    end

    def method_missing(*args, &block)
      if block_given?
        session.__send__(*args, &block)
      else
        session.__send__(*args)
      end
    end
  end.new(Capybara::Session.new(@driver))

  if @driver == :webkit
    h = Headless.new(options.merge(:destroy_at_exit => false, :reuse => true))
    h.start
    ObjectSpace.define_finalizer(@agent.driver.browser.instance_variable_get(:@connection)) do
      h.destroy
    end
  end
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



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

def agent
  @agent
end

#compat_modeObject (readonly)

Returns the value of attribute compat_mode.



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

def compat_mode
  @compat_mode
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.run(options = {}, &block) ⇒ Object



12
13
14
# File 'lib/masque.rb', line 12

def self.run(options = {}, &block)
  new(options).run(&block)
end

Instance Method Details

#compat_capybara_1x!Object



90
91
92
93
94
95
96
97
# File 'lib/masque.rb', line 90

def compat_capybara_1x!
  # http://www.elabs.se/blog/60-introducing-capybara-2-1
  @compat_mode = "1.x"
  Capybara.configure do |config|
    config.match = :prefer_exact
    config.ignore_hidden_elements = false
  end
end

#compat_capybara_20!Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/masque.rb', line 79

def compat_capybara_20!
  # http://www.elabs.se/blog/60-introducing-capybara-2-1
  @compat_mode = "2.0"
  Capybara.configure do |config|
    config.match = :one
    config.exact_options = true
    config.ignore_hidden_elements = true
    config.visible_text_only = true
  end
end

#compat_capybara_21!Object



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

def compat_capybara_21!
  # https://github.com/jnicklas/capybara/blob/2.1.0/lib/capybara.rb#L323
  @compat_mode = "2.1"
  Capybara.configure do |config|
    config.ignore_hidden_elements = true
    config.match = :smart
    config.exact = false
    config.visible_text_only = false
  end
end

#reset_session!Object



58
59
60
61
62
# File 'lib/masque.rb', line 58

def reset_session!
  run do
    driver.reset!
  end
end

#run(&block) ⇒ Object



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

def run(&block)
  @agent.instance_eval(&block)
end