Module: WatirSplash::RSpecPatches

Defined in:
lib/watirsplash/rspec_patches.rb

Class Method Summary collapse

Class Method Details

.bootstrap(const) ⇒ Object



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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/watirsplash/rspec_patches.rb', line 29

def self.bootstrap const
  const.class_eval do

    inst_methods = instance_methods.map &:to_sym

    if !(inst_methods.include?(:__matches?) || inst_methods.include?(:__does_not_match?)) && 
      (inst_methods.include?(:matches?) || inst_methods.include?(:does_not_match?))

      def within(timeout)
        @within_timeout = timeout
        self
      end

      def during(timeout)
        @during_timeout = timeout
        self
      end

      def soon 
        within(30)
      end      

      def seconds
        # for syntactic sugar

        self
      end

      alias_method :second, :seconds

      def minutes
        @within_timeout *= 60 if @within_timeout
        @during_timeout *= 60 if @during_timeout
        self
      end

      alias_method :minute, :minutes
    end

    if inst_methods.include? :matches?
      alias_method :__matches?, :matches? 

      def matches?(actual)
        match_with_wait {__matches?(actual)}
      end
    end

    if inst_methods.include? :does_not_match?
      alias_method :__does_not_match?, :does_not_match?

      def does_not_match?(actual)
        match_with_wait {__does_not_match?(actual)}
      end
    elsif inst_methods.include? :matches?
      def does_not_match?(actual)
        match_with_wait {!__matches?(actual)}
      end
    end

    private

    def match_with_wait
      if @within_timeout
        timeout = @within_timeout; @within_timeout = nil
        Watir::Wait.until(timeout) {yield} rescue false
      elsif @during_timeout
        timeout = @during_timeout; @during_timeout = nil
        Watir::Wait.while(timeout) {yield} rescue true        
      else
        yield
      end
    end
  end
end