Class: Lightpanda::Capybara::Driver

Inherits:
Capybara::Driver::Base
  • Object
show all
Defined in:
lib/lightpanda/capybara/driver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Driver



10
11
12
13
14
15
16
# File 'lib/lightpanda/capybara/driver.rb', line 10

def initialize(app, options = {})
  super()

  @app = app
  @options = options
  @browser = nil
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



8
9
10
# File 'lib/lightpanda/capybara/driver.rb', line 8

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/lightpanda/capybara/driver.rb', line 8

def options
  @options
end

Instance Method Details

#browserObject



18
19
20
# File 'lib/lightpanda/capybara/driver.rb', line 18

def browser
  @browser ||= Lightpanda::Browser.new(@options)
end

#current_urlObject



26
27
28
# File 'lib/lightpanda/capybara/driver.rb', line 26

def current_url
  browser.current_url
end

#evaluate_script(script, *_args) ⇒ Object



70
71
72
# File 'lib/lightpanda/capybara/driver.rb', line 70

def evaluate_script(script, *_args)
  browser.evaluate(script)
end

#execute_script(script, *_args) ⇒ Object



74
75
76
77
# File 'lib/lightpanda/capybara/driver.rb', line 74

def execute_script(script, *_args)
  browser.execute(script)
  nil
end

#find_css(selector) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/lightpanda/capybara/driver.rb', line 60

def find_css(selector)
  count = browser.evaluate("document.querySelectorAll(#{selector.inspect}).length")

  return [] if count.nil? || count.zero?

  (0...count).map do |index|
    Node.new(self, { selector: selector, index: index }, index)
  end
end

#find_xpath(selector) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lightpanda/capybara/driver.rb', line 39

def find_xpath(selector)
  nodes = browser.evaluate("    (function() {\n      var result = document.evaluate(\n        \#{selector.inspect},\n        document,\n        null,\n        XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,\n        null\n      );\n      var nodes = [];\n      for (var i = 0; i < result.snapshotLength; i++) {\n        nodes.push(result.snapshotItem(i));\n      }\n      return nodes;\n    })()\n  JS\n\n  wrap_nodes(nodes || [])\nend\n")

#htmlObject Also known as: body



30
31
32
# File 'lib/lightpanda/capybara/driver.rb', line 30

def html
  browser.body
end

#invalid_element_errorsObject



98
99
100
# File 'lib/lightpanda/capybara/driver.rb', line 98

def invalid_element_errors
  [Lightpanda::NodeNotFoundError, Lightpanda::NoExecutionContextError]
end

#needs_server?Boolean



90
91
92
# File 'lib/lightpanda/capybara/driver.rb', line 90

def needs_server?
  true
end

#quitObject



85
86
87
88
# File 'lib/lightpanda/capybara/driver.rb', line 85

def quit
  @browser&.quit
  @browser = nil
end

#reset!Object



79
80
81
82
83
# File 'lib/lightpanda/capybara/driver.rb', line 79

def reset!
  browser.go_to("about:blank")
rescue StandardError
  nil
end

#titleObject



35
36
37
# File 'lib/lightpanda/capybara/driver.rb', line 35

def title
  browser.title
end

#visit(url) ⇒ Object



22
23
24
# File 'lib/lightpanda/capybara/driver.rb', line 22

def visit(url)
  browser.go_to(url)
end

#wait?Boolean



94
95
96
# File 'lib/lightpanda/capybara/driver.rb', line 94

def wait?
  true
end