Class: Browsate::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/browsate/browser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBrowser

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

#browserObject (readonly)

Returns the value of attribute browser.



5
6
7
# File 'lib/browsate/browser.rb', line 5

def browser
  @browser
end

#console_logsObject (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_idObject (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_pathObject (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

#closeObject



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

#htmlObject



65
66
67
68
# File 'lib/browsate/browser.rb', line 65

def html
  ensure_browser_ready
  @browser.execute_script("document.documentElement.outerHTML")
end


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