Class: WatirCrawler::Base

Inherits:
Abstract show all
Defined in:
lib/watir_crawler/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

extended, included

Methods included from Loggable::Logger

#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

#timeoutsObject (readonly)

Returns the value of attribute timeouts.



5
6
7
# File 'lib/watir_crawler/base.rb', line 5

def timeouts
  @timeouts
end

Instance Method Details

#browserObject



19
20
21
# File 'lib/watir_crawler/base.rb', line 19

def browser
  @browser.browser
end

#browser_profileObject



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_sessionObject



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_startObject



23
24
25
# File 'lib/watir_crawler/base.rb', line 23

def browser_start
  @browser.start
end

#browser_stopObject



27
28
29
# File 'lib/watir_crawler/base.rb', line 27

def browser_stop
  @browser.stop
end

#catch_errorObject



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, # http connection with driver
       Selenium::WebDriver::Error::TimeOutError, # browser.driver.manage.timeouts.page_load
       Selenium::WebDriver::Error::ScriptTimeOutError # browser.driver.manage.timeouts.script_timeout

  log.error "Site is too slow at page: '#{browser.url}'"
  raise SiteTooSlow

rescue SystemCallError, # 'Unknown error - Connection reset by peer'
       Errno::ECONNREFUSED, # 'Connection refused - Connection refused'
       Selenium::WebDriver::Error::WebDriverError => e # 'unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055)'
                                                       # 'unable to bind to locking port 7054 within 45 seconds'
  messages = [
      /Connection reset by peer/, # SystemCallError
      /Connection refused/, # Errno::ECONNREFUSED
      /unable to obtain stable firefox connection/, # Selenium::WebDriver::Error::WebDriverError
      /unable to bind to locking port/ # Selenium::WebDriver::Error::WebDriverError
  ]

  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
    #todo 1 raise_if_firefox_error if respond_to?(:raise_if_firefox_error)
    #todo 2 raise_if_service_unavailable if respond_to?(:raise_if_service_unavailable) # see class method :raise_service_unavailable_if

    if args.any? || block
      pull(args) || (block && instance_eval(&block))
    else
      return nil # running raise_if 1 times and exit if no args & block
    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


Returns:

  • (Boolean)


157
158
159
# File 'lib/watir_crawler/base.rb', line 157

def exist? xpath
  !!pull(xpath)
end

#goto(url) ⇒ Object

— commands



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? # default is :present?
  opt_first = !!opts.delete(:first)  || !opts.delete(:all) # default is true, return 1th element
  raise "Unknown options: '#{opts.inspect}'" if opts.any?

  elements = xpaths.select do |xpath|
    node_for(xpath).send(opt_mode) # detect element on the page by opt_mode
  end.map do |xpath|
    nodes_for(xpath) # get all elements
  end.flatten.select do |node|
    node.send(opt_mode) # select elements by mode
  end

  # flash result nodes
  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

#timerObject



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
  #todo 3 raise_if_site_too_slow if respond_to?(:raise_if_site_too_slow)
  common_wait *xpaths, &block
end