Class: BasicBrowser

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

Direct Known Subclasses

OmniBrowser

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(*configs) ⇒ BasicBrowser

Returns a new instance of BasicBrowser.



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

def initialize (*configs)

  @profile = Selenium::WebDriver::Firefox::Profile.new
  @profile['javascript.enabled'] = false if configs.include?(:no_java)
  @profile['general.useragent.override'] =
      case
        when configs.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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/basic_browser.rb', line 48

def method_missing (method, *args)

  nav_methods = [:goto, :refresh, :back]

  begin
    result = @browser.send(method, *args)
  rescue Timeout::Error => _
    sputs 'page timed out'
    @browser.stop
    @browser.refresh
  end

  sleep(0.88)
  lprint '.' if method.is_in?(nav_methods)

  result
end

Instance Method Details

#move(x, y) ⇒ Object



30
31
32
# File 'lib/basic_browser.rb', line 30

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

#positionObject



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

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

#resize(width, height = nil) ⇒ Object



25
26
27
28
# File 'lib/basic_browser.rb', line 25

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

#save_html(path) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/basic_browser.rb', line 66

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



44
45
46
# File 'lib/basic_browser.rb', line 44

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

#zoom_out(times) ⇒ Object



34
35
36
37
38
# File 'lib/basic_browser.rb', line 34

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