Class: WatirCrawler::Base
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Loggable
extended, included
#debug, #debug=, #logger, #logger=
Constructor Details
#initialize(timeouts = {}) ⇒ Base
Returns a new instance of Base.
7
8
9
10
11
|
# File 'lib/watir_crawler/base.rb', line 7
def initialize(timeouts = {})
@elements_path = []
@timeouts = { :wait_timeout => 150 }.merge(timeouts)
@browser = WatirCrawler::Browser.new(@timeouts)
end
|
Instance Attribute Details
#timeouts ⇒ Object
Returns the value of attribute timeouts.
5
6
7
|
# File 'lib/watir_crawler/base.rb', line 5
def timeouts
@timeouts
end
|
Instance Method Details
#browser ⇒ Object
19
20
21
|
# File 'lib/watir_crawler/base.rb', line 19
def browser
@browser.browser
end
|
#browser_profile ⇒ Object
13
14
15
16
17
|
# File 'lib/watir_crawler/base.rb', line 13
def browser_profile
@browser.profile do |profile|
yield profile if block_given?
end
end
|
#browser_session ⇒ Object
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/watir_crawler/base.rb', line 31
def browser_session
timer do
catch_error do
browser_start
yield
end
end
ensure
browser_stop
end
|
#browser_start ⇒ Object
23
24
25
|
# File 'lib/watir_crawler/base.rb', line 23
def browser_start
@browser.start
end
|
#browser_stop ⇒ Object
27
28
29
|
# File 'lib/watir_crawler/base.rb', line 27
def browser_stop
@browser.stop
end
|
#catch_error ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/watir_crawler/base.rb', line 50
def catch_error
yield
rescue Timeout::Error,
Selenium::WebDriver::Error::TimeOutError,
Selenium::WebDriver::Error::ScriptTimeOutError
log.error "Site is too slow at page: '#{browser.url}'"
raise SiteTooSlow
rescue SystemCallError,
Errno::ECONNREFUSED,
Selenium::WebDriver::Error::WebDriverError => e
messages = [
/Connection reset by peer/,
/Connection refused/,
/unable to obtain stable firefox connection/,
/unable to bind to locking port/
]
log "#{e.class}: #{e.message} \n#{e.backtrace.join("\n")}"
klass = messages.select{|msg| msg =~ e.message }.any? ? WebdriverError : SiteChanged
raise klass
end
|
#common_wait(*args, &block) ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/watir_crawler/base.rb', line 137
def common_wait *args, &block
browser.wait_until(@timeouts[:wait_timeout]) do
if args.any? || block
pull(args) || (block && instance_eval(&block))
else
return nil
end
end
rescue Selenium::WebDriver::Error::StaleElementReferenceError,
Selenium::WebDriver::Error::ObsoleteElementError
sleep 1
retry
rescue Watir::Wait::TimeoutError
raise SiteChanged
end
|
#exec(script) ⇒ Object
82
83
84
|
# File 'lib/watir_crawler/base.rb', line 82
def exec script
browser.execute_script(script)
end
|
#exist?(xpath) ⇒ Boolean
157
158
159
|
# File 'lib/watir_crawler/base.rb', line 157
def exist? xpath
!!pull(xpath)
end
|
#goto(url) ⇒ Object
78
79
80
|
# File 'lib/watir_crawler/base.rb', line 78
def goto url
browser.goto url if url != browser.url
end
|
#pull(*args, &block) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/watir_crawler/base.rb', line 89
def pull *args, &block
opts, xpaths = args.flatten.partition{|a| a.is_a?(Symbol) }
opt_mode = opts.delete(:exist?) || opts.delete(:present?) || :present?
opt_first = !!opts.delete(:first) || !opts.delete(:all)
raise "Unknown options: '#{opts.inspect}'" if opts.any?
elements = xpaths.select do |xpath|
node_for(xpath).send(opt_mode)
end.map do |xpath|
nodes_for(xpath)
end.flatten.select do |node|
node.send(opt_mode)
end
elements = elements.take(1) if opt_first
elements.each{|node| node.flash unless node.is_a?(Watir::Frame) }
first_element = elements.first
if block
raise SiteChanged, "Not found elements for xpath: #{xpaths.inspect}" if first_element.nil?
nodes_path << first_element.node_xpath
yield
else
if opt_first
first_element && first_element.to_subtype
else
elements.map{|element| element.to_subtype }
end
end
rescue Selenium::WebDriver::Error::StaleElementReferenceError,
Selenium::WebDriver::Error::ObsoleteElementError
sleep 1
retry
ensure
nodes_path.pop if block
end
|
#timer ⇒ Object
42
43
44
45
46
47
48
|
# File 'lib/watir_crawler/base.rb', line 42
def timer
log.info "Session start"
start_time = Time.now
yield
ensure
log.info "Session end, elapsed time: #{Time.now - start_time}"
end
|
#wait(*xpaths, &block) ⇒ Object
:first - get FIRST element of FIRST founded xpath, DEFAULT OPTION :all - get ALL elements of FIRST founded xpath
131
132
133
134
|
# File 'lib/watir_crawler/base.rb', line 131
def wait *xpaths, &block
common_wait *xpaths, &block
end
|