Class: Browsate::Browser
- Inherits:
-
Object
- Object
- Browsate::Browser
- Defined in:
- lib/browsate/browser.rb
Instance Attribute Summary collapse
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
-
#console_logs ⇒ Object
readonly
Returns the value of attribute console_logs.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
-
#session_path ⇒ Object
readonly
Returns the value of attribute session_path.
Instance Method Summary collapse
- #close ⇒ Object
- #execute_javascript(script) ⇒ Object
- #html ⇒ Object
-
#initialize ⇒ Browser
constructor
A new instance of Browser.
- #navigate(url, session_id = nil) ⇒ Object
- #screenshot(path = nil) ⇒ Object
- #wait_for_selector(selector, timeout = Browsate.configuration.timeout) ⇒ Object
Constructor Details
#initialize ⇒ Browser
Returns a new instance of Browser.
7 8 9 10 11 12 |
# File 'lib/browsate/browser.rb', line 7 def initialize @session_id = nil @session_path = nil @browser = nil @console_logs = [] end |
Instance Attribute Details
#browser ⇒ Object (readonly)
Returns the value of attribute browser.
5 6 7 |
# File 'lib/browsate/browser.rb', line 5 def browser @browser end |
#console_logs ⇒ Object (readonly)
Returns the value of attribute console_logs.
5 6 7 |
# File 'lib/browsate/browser.rb', line 5 def console_logs @console_logs end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
5 6 7 |
# File 'lib/browsate/browser.rb', line 5 def session_id @session_id end |
#session_path ⇒ Object (readonly)
Returns the value of attribute session_path.
5 6 7 |
# File 'lib/browsate/browser.rb', line 5 def session_path @session_path end |
Instance Method Details
#close ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/browsate/browser.rb', line 34 def close return unless @browser Browsate.logger.info("Closing browser session") @browser.stop @browser = nil end |
#execute_javascript(script) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/browsate/browser.rb', line 26 def execute_javascript(script) ensure_browser_ready Browsate.logger.info("Executing JavaScript") result = @browser.execute_script(script) capture_state("after_script") result end |
#html ⇒ Object
65 66 67 68 |
# File 'lib/browsate/browser.rb', line 65 def html ensure_browser_ready @browser.execute_script("document.documentElement.outerHTML") end |
#navigate(url, session_id = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/browsate/browser.rb', line 14 def navigate(url, session_id = nil) setup_session(session_id) start_browser Browsate.logger.info("Navigating to #{url}") @browser.navigate_to(url) wait_for_page_load capture_state("initial") self end |
#screenshot(path = nil) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/browsate/browser.rb', line 57 def screenshot(path = nil) ensure_browser_ready path ||= File.join(@session_path, "screenshot_#{Time.now.to_i}.png") Browsate.logger.info("Taking screenshot: #{path}") @browser.screenshot(path) path end |
#wait_for_selector(selector, timeout = Browsate.configuration.timeout) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/browsate/browser.rb', line 42 def wait_for_selector(selector, timeout = Browsate.configuration.timeout) ensure_browser_ready Browsate.logger.info("Waiting for selector: #{selector}") start_time = Time.now while Time.now - start_time < timeout element = @browser.find_element(selector) return element if element sleep 0.1 end raise "Timeout waiting for selector: #{selector}" end |