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.3"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Masque

Returns a new instance of Masque.



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
57
58
59
60
# File 'lib/masque.rb', line 20

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

  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.



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

def agent
  @agent
end

#compat_modeObject (readonly)

Returns the value of attribute compat_mode.



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

def compat_mode
  @compat_mode
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

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



16
17
18
# File 'lib/masque.rb', line 16

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

Instance Method Details

#compat_capybara_1x!Object



94
95
96
97
98
99
100
101
# File 'lib/masque.rb', line 94

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



83
84
85
86
87
88
89
90
91
92
# File 'lib/masque.rb', line 83

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



72
73
74
75
76
77
78
79
80
81
# File 'lib/masque.rb', line 72

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



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

def reset_session!
  run do
    driver.reset!
  end
end

#run(&block) ⇒ Object



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

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