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.
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
61
|
# File 'lib/omni_browser.rb', line 19
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/omni_browser.rb', line 86
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.
17
18
19
|
# File 'lib/omni_browser.rb', line 17
def allowance
@allowance
end
|
#config ⇒ Object
Returns the value of attribute config.
16
17
18
|
# File 'lib/omni_browser.rb', line 16
def config
@config
end
|
#database ⇒ Object
Returns the value of attribute database.
16
17
18
|
# File 'lib/omni_browser.rb', line 16
def database
@database
end
|
#domains_db ⇒ Object
Returns the value of attribute domains_db.
16
17
18
|
# File 'lib/omni_browser.rb', line 16
def domains_db
@domains_db
end
|
#ip_db ⇒ Object
Returns the value of attribute ip_db.
16
17
18
|
# File 'lib/omni_browser.rb', line 16
def ip_db
@ip_db
end
|
#logging_db ⇒ Object
Returns the value of attribute logging_db.
16
17
18
|
# File 'lib/omni_browser.rb', line 16
def logging_db
@logging_db
end
|
#my_ip ⇒ Object
Returns the value of attribute my_ip.
17
18
19
|
# File 'lib/omni_browser.rb', line 17
def my_ip
@my_ip
end
|
#tunnel ⇒ Object
Returns the value of attribute tunnel.
17
18
19
|
# File 'lib/omni_browser.rb', line 17
def tunnel
@tunnel
end
|
Instance Method Details
#add_request ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/omni_browser.rb', line 130
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
143
144
145
|
# File 'lib/omni_browser.rb', line 143
def domain
@browser.url.scan(/[^\/]+/)[1].strip
end
|
#domain_check ⇒ Object
121
122
123
124
125
126
127
128
|
# File 'lib/omni_browser.rb', line 121
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
68
69
70
|
# File 'lib/omni_browser.rb', line 68
def move (x, y)
@browser.window.move_to(x, y)
end
|
#position ⇒ Object
78
79
80
|
# File 'lib/omni_browser.rb', line 78
def position
@browser.windows.first.position.to_a
end
|
#resize(width, height = nil) ⇒ Object
63
64
65
66
|
# File 'lib/omni_browser.rb', line 63
def resize (width, height = nil)
height = width if height.nil?
@browser.window.resize_to(width, height)
end
|
#save_domain ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/omni_browser.rb', line 109
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
147
148
149
150
151
152
153
154
|
# File 'lib/omni_browser.rb', line 147
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
82
83
84
|
# File 'lib/omni_browser.rb', line 82
def size
@browser.windows.first.size.to_a
end
|
#zoom_out(times) ⇒ Object
72
73
74
75
76
|
# File 'lib/omni_browser.rb', line 72
def zoom_out (times)
times.times do
@browser.send_keys([:command, :subtract])
end
end
|