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

Returns a new instance of 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
21
# File 'lib/lightpanda/capybara/driver.rb', line 18

def browser
  @browser = nil if @browser && !browser_alive?
  @browser ||= Lightpanda::Browser.new(@options)
end

#browser_alive?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/lightpanda/capybara/driver.rb', line 23

def browser_alive?
  @browser.client && !@browser.client.closed?
rescue StandardError
  false
end

#current_urlObject



33
34
35
# File 'lib/lightpanda/capybara/driver.rb', line 33

def current_url
  browser.current_url
end

#evaluate_script(script, *_args) ⇒ Object



77
78
79
# File 'lib/lightpanda/capybara/driver.rb', line 77

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

#execute_script(script, *_args) ⇒ Object



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

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

#find_css(selector) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/lightpanda/capybara/driver.rb', line 67

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lightpanda/capybara/driver.rb', line 46

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



37
38
39
# File 'lib/lightpanda/capybara/driver.rb', line 37

def html
  browser.body
end

#invalid_element_errorsObject



105
106
107
# File 'lib/lightpanda/capybara/driver.rb', line 105

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

#needs_server?Boolean

Returns:

  • (Boolean)


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

def needs_server?
  true
end

#quitObject



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

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

#reset!Object



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

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

#titleObject



42
43
44
# File 'lib/lightpanda/capybara/driver.rb', line 42

def title
  browser.title
end

#visit(url) ⇒ Object



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

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

#wait?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/lightpanda/capybara/driver.rb', line 101

def wait?
  true
end