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
# File 'lib/persistent_selenium/browser.rb', line 7

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

Instance Method Details

#browserObject



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

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
    profile = Selenium::WebDriver::Chrome::Profile.new

    PersistentSelenium.chrome_extensions.each do |extension|
      profile.add_extension extension
    end

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

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

#load_splash_pageObject



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

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

#optionsObject



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

def options
  {}
end

#reset!Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/persistent_selenium/browser.rb', line 56

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



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

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

#starting_pageObject



68
69
70
71
72
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
# File 'lib/persistent_selenium/browser.rb', line 68

def starting_page
  "<html>\n  <head>\n<title>Persistent Selenium -- Starting up...</title>\n<style>\n  body {\n    background: #444;\n  }\n\n  * {\n    font-family: Verdana, Helvetica, sans-serif;\n  }\n\n  td {\n    background: #ccc;\n    width: 50%;\n    text-align: center;\n\n    border: solid #333 1px;\n    border-radius: 5px;\n    padding: 10px;\n  }\n</style>\n  </head>\n  <body>\n<table width=\"100%\" height=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n  <tr>\n    <td>\n      <h1>Persistent Selenium</h1>\n      <h2>Starting up...</h2>\n    </td>\n  </tr>\n</table>\n  </body>\n</html>\n  HTML\nend\n"

#visit(path) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/persistent_selenium/browser.rb', line 39

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

  browser.navigate.to(path)
end