Module: FerrumCommon::Common

Defined in:
lib/ferrum_common.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.mhtml(browser, timeout, mtd, msg = nil) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/ferrum_common.rb', line 6

def self.mhtml browser, timeout, mtd, msg = nil
  Timeout.timeout(timeout){ yield }
rescue Timeout::Error
  browser.mhtml path: "temp.mhtml"
  $!.backtrace.reject!{ |_| _[/\/gems\/concurrent-ruby-/] }
  $!.backtrace.reject!{ |_| _[/\/gems\/ferrum-/] }
  raise Timeout::Error, "#{$!.to_s} after #{timeout} sec in #{mtd}#{" (#{msg.respond_to?(:call) ? msg.call : msg})" if msg}"
end

Instance Method Details

#abort(msg_or_cause) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/ferrum_common.rb', line 42

def abort msg_or_cause
  # puts (msg_or_cause.respond_to?(:backtrace) ? msg_or_cause : Thread.current).backtrace
  puts (msg_or_cause.respond_to?(:full_message) ? msg_or_cause.full_message : Thread.current.backtrace)
  mhtml path: "temp.mhtml"
  puts "dumped to ./temp.mhtml"
  Kernel.abort msg_or_cause.to_s
end

#until_one(type, selector, timeout) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ferrum_common.rb', line 25

def until_one type, selector, timeout
  t = nil
  Module.nesting.first.mhtml self, timeout, __method__, ->{ "expected exactly one node for #{type} #{selector.inspect}, got #{t ? t.size : "none"}" } do
    t = begin
      public_method(type).call selector
    rescue Ferrum::NodeNotFoundError
      sleep timeout * 0.1
      redo
    end
    unless 1 == t.size
      sleep timeout * 0.1
      redo
    end
  end
  t.first
end

#until_true(timeout, msg = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/ferrum_common.rb', line 15

def until_true timeout, msg = nil
  Module.nesting.first.mhtml self, timeout, __method__, msg do
    begin
      yield
    rescue Ferrum::NodeNotFoundError
      redo
    end or (sleep timeout*0.1; redo)
  end
end