Class: PersistentSelenium::Browser

Inherits:
Capybara::Selenium::Driver
  • Object
show all
Defined in:
lib/persistent_selenium/browser.rb

Instance Method Summary collapse

Constructor Details

#initialize(browser_type) ⇒ Browser

Returns a new instance of Browser.



7
8
9
10
11
# File 'lib/persistent_selenium/browser.rb', line 7

def initialize(browser_type)
  @browser_type = browser_type.to_s.to_sym
  @frame_handles = {}
  @__found_elements__ = []
end

Instance Method Details

#browserObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/persistent_selenium/browser.rb', line 13

def browser
  case @browser_type
  when :firefox
    profile = Selenium::WebDriver::Firefox::Profile.new
    profile['browser.cache.disk.enable'] = false
    profile['browser.cache.memory.enable'] = false
    profile['browser.cache.offline.enable'] = false
    profile['network.http.use-cache'] = false

    options = { :profile => profile }
  when :chrome
    switches = %w{--disk-cache-size=1 --media-cache-size=1}

    PersistentSelenium.chrome_extensions.each do |extension|
      switches.push("--load-extension=#{extension}")
    end

    options = { switches: switches }
  end

  @browser ||= Selenium::WebDriver.for(@browser_type, options)
end

#browser_initialized?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/persistent_selenium/browser.rb', line 40

def browser_initialized?
  false
end

#load_splash_pageObject



57
58
59
# File 'lib/persistent_selenium/browser.rb', line 57

def load_splash_page
  browser.navigate.to("data:text/html;base64,#{Base64.encode64(starting_page)}")
end

#optionsObject



36
37
38
# File 'lib/persistent_selenium/browser.rb', line 36

def options
  {}
end

#reset!Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/persistent_selenium/browser.rb', line 61

def reset!
  if @browser
    begin
      @browser.manage.delete_all_cookies
    rescue Selenium::WebDriver::Error::UnhandledError => e
      # delete_all_cookies fails when we've previously gone
      # to about:blank, so we rescue this error and do nothing
      # instead.
    end
  end
end

#set_app_host(host) ⇒ Object



52
53
54
55
# File 'lib/persistent_selenium/browser.rb', line 52

def set_app_host(host)
  @app_host = host.dup
  load_splash_page
end

#starting_pageObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/persistent_selenium/browser.rb', line 73

def starting_page
  <<-HTML
<html>
  <head>
<title>Persistent Selenium -- Starting up...</title>
<style>
  body {
    background: #444;
  }

  * {
    font-family: Verdana, Helvetica, sans-serif;
  }

  td {
    background: #ccc;
    width: 50%;
    text-align: center;

    border: solid #333 1px;
    border-radius: 5px;
    padding: 10px;
  }
</style>
  </head>
  <body>
<table width="100%" height="100%" cellpadding="0" cellspacing="0">
  <tr>
    <td>
      <h1>Persistent Selenium</h1>
      <h2>Starting up...</h2>
    </td>
  </tr>
</table>
  </body>
</html>
  HTML
end

#visit(path) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/persistent_selenium/browser.rb', line 44

def visit(path)
  if !path[/^http/]
    path = @app_host + path
  end

  browser.navigate.to(path)
end