Class: OmniBrowser

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

Constant Summary collapse

FIREFOX_41 =
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1'
CHROME =
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'

Instance Method Summary collapse

Constructor Details

#initialize(*config) ⇒ OmniBrowser

Returns a new instance of OmniBrowser.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/omni_browser.rb', line 11

def initialize (*config)
  profile = Selenium::WebDriver::Firefox::Profile.new

  profile['javascript.enabled'] = false if config.include?(:no_java)
  profile['general.useragent.override'] =
      case
        when config.include?(:firefox_41)
          FIREFOX_41
        else
          CHROME
      end

  @browser = Watir::Browser.new(:firefox, profile: profile)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



49
50
51
# File 'lib/omni_browser.rb', line 49

def method_missing (method, *args)
  @browser.send(method, *args)
end

Instance Method Details

#domainObject



53
54
55
# File 'lib/omni_browser.rb', line 53

def domain
  @browser.url.scan(/[^\/]+/)[1]
end

#move(x, y) ⇒ Object



31
32
33
# File 'lib/omni_browser.rb', line 31

def move (x, y)
  @browser.window.move_to(x, y)
end

#positionObject



41
42
43
# File 'lib/omni_browser.rb', line 41

def position
  @browser.windows.first.position.to_a
end

#resize(width, height = nil) ⇒ Object



26
27
28
29
# File 'lib/omni_browser.rb', line 26

def resize (width, height = nil)
  height = width if height.nil?
  @browser.window.resize_to(width, height)
end

#save_html(path) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/omni_browser.rb', line 57

def save_html (path)
  path = path
  path << '.html' unless path =~ /.+\.html\z/

  file = File.open(path, 'w')
  file.puts @browser.html
  file.close
end

#sizeObject



45
46
47
# File 'lib/omni_browser.rb', line 45

def size
  @browser.windows.first.size.to_a
end

#zoom_out(times) ⇒ Object



35
36
37
38
39
# File 'lib/omni_browser.rb', line 35

def zoom_out (times)
  times.times do
    @browser.send_keys([:command, :subtract])
  end
end