Class: OmniBrowser
- Inherits:
-
Object
show all
- Defined in:
- lib/omni_browser.rb,
lib/omni_browser/action_logging.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 Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*config) ⇒ OmniBrowser
Returns a new instance of OmniBrowser.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/omni_browser.rb', line 18
def initialize (*config)
h_args = config.last
@config = FlexConfig.new('/Users/YCL/Documents/RubyProjects/gems/omni_browser', {})
@database =
if config.include?(:no_database)
Dummy.new('database')
else
db_name = h_args[:db]
details = @config.to_h[:databases][db_name]
details.store(:database, db_name)
details
FlexPG.new(details)
end
@logging_db = database[@config.to_h[:tbl_names][:logging]]
@domains_db = database[@config.to_h[:tbl_names][:domains]]
@ip_db = database[@config.to_h[:tbl_names][:ip]]
@tunnel = nil
@my_ip = nil
unless config.include?(:no_report)
ClickReport.link_browser(self)
Watir::Element.include(ClickReport)
require 'omni_browser/action_logging/watir_mods'
end
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)
@last_domain = ''
@dom_id = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/omni_browser.rb', line 85
def method_missing (method, *args)
if [:goto, :refresh, :back].include?(method)
save_domain
browsed = true
else
browsed = false
end
begin
result = @browser.send(method, *args)
rescue Timeout::Error => e
puts 'page timed out'
@browser.stop
@browser.refresh
end
sleep(0.88)
if browsed
domain_check
add_request
end
result
end
|
Instance Attribute Details
#allowance ⇒ Object
Returns the value of attribute allowance.
16
17
18
|
# File 'lib/omni_browser.rb', line 16
def allowance
@allowance
end
|
#config ⇒ Object
Returns the value of attribute config.
15
16
17
|
# File 'lib/omni_browser.rb', line 15
def config
@config
end
|
#database ⇒ Object
Returns the value of attribute database.
15
16
17
|
# File 'lib/omni_browser.rb', line 15
def database
@database
end
|
#domains_db ⇒ Object
Returns the value of attribute domains_db.
15
16
17
|
# File 'lib/omni_browser.rb', line 15
def domains_db
@domains_db
end
|
#ip_db ⇒ Object
Returns the value of attribute ip_db.
15
16
17
|
# File 'lib/omni_browser.rb', line 15
def ip_db
@ip_db
end
|
#logging_db ⇒ Object
Returns the value of attribute logging_db.
15
16
17
|
# File 'lib/omni_browser.rb', line 15
def logging_db
@logging_db
end
|
#my_ip ⇒ Object
Returns the value of attribute my_ip.
16
17
18
|
# File 'lib/omni_browser.rb', line 16
def my_ip
@my_ip
end
|
#tunnel ⇒ Object
Returns the value of attribute tunnel.
16
17
18
|
# File 'lib/omni_browser.rb', line 16
def tunnel
@tunnel
end
|
Instance Method Details
#add_request ⇒ Object
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/omni_browser.rb', line 129
def add_request
details = {:ip_address => @my_ip, :domain => @dom_id, :raw_url => @browser.url, :time => Time.now}
logging_db.insert(details)
puts '.'
unless tunnel.nil?
puts @allowance -= 1
if @allowance == 0
@allowance = tunnel.reallocate
@my_ip = get_my_ip
end
end
end
|
#domain ⇒ Object
142
143
144
|
# File 'lib/omni_browser.rb', line 142
def domain
@browser.url.scan(/[^\/]+/)[1].strip
end
|
#domain_check ⇒ Object
120
121
122
123
124
125
126
127
|
# File 'lib/omni_browser.rb', line 120
def domain_check
dom = domain
if @last_domain != dom || @last_domain == ''
@dom_id = domains_db[:domain_url => dom][:id]
else
@dom_id
end
end
|
#get_my_ip ⇒ Object
7
8
9
10
11
|
# File 'lib/omni_browser/action_logging.rb', line 7
def get_my_ip
puts 'Getting IP'
puts ip = open('http://icanhazip.com').read.strip
ip
end
|
#move(x, y) ⇒ Object
67
68
69
|
# File 'lib/omni_browser.rb', line 67
def move (x, y)
@browser.window.move_to(x, y)
end
|
#position ⇒ Object
77
78
79
|
# File 'lib/omni_browser.rb', line 77
def position
@browser.windows.first.position.to_a
end
|
#resize(width, height = nil) ⇒ Object
62
63
64
65
|
# File 'lib/omni_browser.rb', line 62
def resize (width, height = nil)
height = width if height.nil?
@browser.window.resize_to(width, height)
end
|
#save_domain ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/omni_browser.rb', line 108
def save_domain
if @last_domain == ''
@my_ip = get_my_ip
puts 'Browser disconnecting database...'
database.disconnect
puts 'Sleep(3)'
sleep(3)
puts "Database connection ok? #{database.test_connection}"
end
@last_domain = domain
end
|
#save_html(path) ⇒ Object
146
147
148
149
150
151
152
153
|
# File 'lib/omni_browser.rb', line 146
def save_html (path)
path = path
path << '.html' unless path =~ /.+\.html\z/
file = File.open(path, 'w')
file.puts @browser.html
file.close
end
|
#size ⇒ Object
81
82
83
|
# File 'lib/omni_browser.rb', line 81
def size
@browser.windows.first.size.to_a
end
|
#zoom_out(times) ⇒ Object
71
72
73
74
75
|
# File 'lib/omni_browser.rb', line 71
def zoom_out (times)
times.times do
@browser.send_keys([:command, :subtract])
end
end
|