Class: Lightpanda::Capybara::Driver
- Inherits:
-
Capybara::Driver::Base
- Object
- Capybara::Driver::Base
- Lightpanda::Capybara::Driver
- Defined in:
- lib/lightpanda/capybara/driver.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #browser ⇒ Object
- #browser_alive? ⇒ Boolean
- #current_url ⇒ Object
- #evaluate_script(script, *_args) ⇒ Object
- #execute_script(script, *_args) ⇒ Object
- #find_css(selector) ⇒ Object
- #find_xpath(selector) ⇒ Object
- #html ⇒ Object (also: #body)
-
#initialize(app, options = {}) ⇒ Driver
constructor
A new instance of Driver.
- #invalid_element_errors ⇒ Object
- #needs_server? ⇒ Boolean
- #quit ⇒ Object
- #reset! ⇒ Object
- #title ⇒ Object
- #visit(url) ⇒ Object
- #wait? ⇒ Boolean
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, = {}) super() @app = app = @browser = nil end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
8 9 10 |
# File 'lib/lightpanda/capybara/driver.rb', line 8 def app @app end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/lightpanda/capybara/driver.rb', line 8 def end |
Instance Method Details
#browser ⇒ Object
18 19 20 21 |
# File 'lib/lightpanda/capybara/driver.rb', line 18 def browser @browser = nil if @browser && !browser_alive? @browser ||= Lightpanda::Browser.new() end |
#browser_alive? ⇒ 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_url ⇒ Object
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") |
#html ⇒ Object Also known as: body
37 38 39 |
# File 'lib/lightpanda/capybara/driver.rb', line 37 def html browser.body end |
#invalid_element_errors ⇒ Object
105 106 107 |
# File 'lib/lightpanda/capybara/driver.rb', line 105 def invalid_element_errors [Lightpanda::NodeNotFoundError, Lightpanda::NoExecutionContextError] end |
#needs_server? ⇒ Boolean
97 98 99 |
# File 'lib/lightpanda/capybara/driver.rb', line 97 def needs_server? true end |
#quit ⇒ Object
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 |
#title ⇒ Object
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
101 102 103 |
# File 'lib/lightpanda/capybara/driver.rb', line 101 def wait? true end |