Module: CapyTime

Defined in:
lib/capy_time.rb,
lib/capy_time/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Instance Method Details

#load_element(statement, *options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
# File 'lib/capy_time.rb', line 4

def load_element(statement,*options)
    #initialize
    loaded=false
    
    #retrieve options (if any)
    if(options!=[] && ((x=options.pop)!=nil))then
        @options = []
        @options.push(x.dup)
        for o in options do
            @options.push(options[o])
        end
    else
        @options=[]
    end
    
    #if option has known key put val for key in var
    @t=nil
    @r=nil
    @te=nil
    for o in @options do
        val = @options.pop.dup
        if(val.has_key?(:time))then
            @t = val[:time]
        elsif(val.has_key?(:refresh))then
            @r = val[:refresh]
        elsif(val.has_key?(:text))then
            @te = val[:text]
        end
    end
    
    #set vars or default if option not provided
    @time=nil
    @time=@t ||=ENV['STIME'].to_i
    @refresh=nil
    @refresh=@r ||=false
    @text=@te ||=nil

    Timeout.timeout(@time)do
      begin
        urlPopulated= false
        while(!urlPopulated)
        urlPopulated= current_url.length>0
        end
        if(@refresh)then
            page.evaluate_script('window.history.go()')
            found=false
            while(!found)
                begin
                    if(@text==nil)then
                        found = (all(statement)!=nil && all(statement).length>0)
                    else
                        found = (all(statement,:text =>@text)!=nil && all(statement,:text =>@text).length>0)
                    end
                rescue
                    retry
                end
            end
            if(@text==nil)then
                return all(statement)
            else
                return all(statement,:text =>@text)
            end
        end
        found=false
            while(!found)
                begin
                    if(@text==nil)then
                        found = (all(statement)!=nil && all(statement).length>0)
                    else
                        found = (all(statement,:text =>@text)!=nil && all(statement,:text =>@text).length>0)
                    end
                rescue
                    retry
                end
            end
        if(@text==nil)then
                return all(statement)
            else
                return all(statement,:text =>@text)
            end
      rescue
        retry
      end
    end
end

#load_true(statement, *options) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/capy_time.rb', line 298

def load_true(statement,*options)
    loaded=false
    
    if(options!=[] && ((x=options.pop)!=nil))then
        @options = []
        @options.push(x.dup)
        for o in options do
            @options.push(options[o])
        end
    else
        @options=[]
    end
    for o in @options do
        val = @options.pop.dup
        if(val.has_key?(:time))then
            @t = val[:time]
        elsif(val.has_key?(:refresh))then
            @r = val[:refresh]
        end
    end
    @time=@t ||=ENV['STIME'].to_i
    @refresh=@r ||=false
    Timeout.timeout(@time)do
        while(!statement)
            if(@refresh && !statement)then
                page.evaluate_script('window.history.go()')
                while(!statement)
                end
            end 
        end
        return statement
    end
end

#should_have_content(content, *options) ⇒ Object



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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/capy_time.rb', line 90

def should_have_content(content,*options)
    #retrieve options (if any)
    if(options!=[] && ((x=options.pop)!=nil))then
        @options = []
        @options.push(x.dup)
        for o in options do
            @options.push(options[o])
        end
    else
        @options=[]
    end
    
    #if option has known key put val for key in var
    @t=nil
    @r=nil
    @p=nil
    for o in @options do
        val = @options.pop.dup
        if(val.has_key?(:time))then
            @t = val[:time]
        elsif(val.has_key?(:refresh))then
            @r = val[:refresh]
        elsif(val.has_key?(:parent))then
            @p = val[:parent]
        end
    end
    
    #set vars or default if option not provided
    @time=nil
    @time=@t ||=ENV['STIME'].to_i
    @refresh=nil
    @refresh=@r ||=false
    @parent=nil
    @parent=@p ||=nil
    
    @content=content
    Timeout.timeout(@time)do
        urlPopulated= false
        while(!urlPopulated)
        urlPopulated= current_url.length>0
        end
        if(@refresh && (@content!=nil) && @parent!=nil)then
            page.evaluate_script('window.history.go()')
            load_element(@parent)
            status=false
            while(!status)
                status=load_element(@parent)[0].has_content?(@content)
            end
        elsif(@refresh && (@content!=nil) && @parent==nil)then
            page.evaluate_script('window.history.go()')
            status=false
            while(!status)
                status=page.has_content?(@content)
            end
        elsif((!@refresh) && (@content!=nil) && @parent!=nil)then
            load_element(@parent)
            status=false
            while(!status)
                status=load_element(@parent)[0].has_content?(@content)
            end
        elsif((!@refresh) && (@content!=nil) && @parent==nil)then
            status=false
            while(!status)
                status=page.has_content?(@content)
            end
        else
            raise 'Error: Invalid options passed to method- should_have_content'
        end 
    end
end

#should_have_css(content, *options) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/capy_time.rb', line 161

def should_have_css(content,*options)
    #retrieve options (if any)
    if(options!=[] && ((x=options.pop)!=nil))then
        @options = []
        @options.push(x.dup)
        for o in options do
            @options.push(options[o])
        end
    else
        @options=[]
    end
    
    #if option has known key put val for key in var
    @t=nil
    @r=nil
    @p=nil
    for o in @options do
        val = @options.pop.dup
        if(val.has_key?(:time))then
            @t = val[:time]
        elsif(val.has_key?(:refresh))then
            @r = val[:refresh]
        elsif(val.has_key?(:parent))then
            @p = val[:parent]
        end
    end
    
    #set vars or default if option not provided
    @time=nil
    @time=@t ||=ENV['STIME'].to_i
    @refresh=nil
    @refresh=@r ||=false
    @parent=nil
    @parent=@p ||=nil
    
    @content=content
    Timeout.timeout(@time)do
        urlPopulated= false
        while(!urlPopulated)
        urlPopulated= current_url.length>0
        end
        if(@refresh && (@content!=nil) && @parent!=nil)then
            page.evaluate_script('window.history.go()')
            load_element(@parent)
            status=false
            while(!status)
                status=load_element(@parent+" "+@content)
            end
        elsif(@refresh && (@content!=nil) && @parent==nil)then
            page.evaluate_script('window.history.go()')
            status=false
            while(!status)
                status=load_element(@content)
            end
        elsif((!@refresh) && (@content!=nil) && @parent!=nil)then
            load_element(@parent)
            status=false
            while(!status)
                status=load_element(@parent+" "+@content)
            end
        elsif((!@refresh) && (@content!=nil) && @parent==nil)then
            status=false
            while(!status)
                status=load_element(@content)
            end
        else
            raise 'Error: Invalid options passed to method- should_have_css'
        end 
    end
end

#should_open_tab(url, numtabs, *options) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/capy_time.rb', line 232

def should_open_tab(url,numtabs,*options)
    
    #retrieve options (if any)
if(options!=[] && ((x=options.pop)!=nil))then
    @options = []
    @options.push(x.dup)
    for o in options do
        @options.push(options[o])
    end
else
    @options=[]
end

#if option has known key put val for key in var
@c=nil
@t=nil
for o in @options do
    val = @options.pop.dup
    if(val.has_key?(:close))then
        @c = val[:close]
    elsif(val.has_key?(:time))then
        @t = val[:time]
    end
end

#set vars or default if option not provided
@close=nil
@close=@c ||=false
@time=nil
@time=@t  ||=ENV['STIME'].to_i
    
    Timeout::timeout(@time) {
        status=false
        while(!status)
            status=page.driver.browser.window_handles.length > numtabs
        end
    }
    
    page.driver.browser.switch_to.window(page.driver.browser.window_handles.last)
    Timeout::timeout(@time) {
        status=false
        while(!status)
            status=current_url.length > 1
        end
    }
        
    begin
        load_true(current_url.include?(url))
    rescue
        raise 'Error: Failed to navigate to '+url
    end
    if(@close==true)then
        current_window.close
  
  
        Timeout::timeout(@time) {
            status=false
            while(!status)
                status=(page.driver.browser.window_handles.length) == numtabs
            end
        }
  
        page.driver.browser.switch_to.window(page.driver.browser.window_handles.first)
    end
end

#wait_for_page_change(prevUrl) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/capy_time.rb', line 332

def wait_for_page_change(prevUrl)
    $url = prevUrl
    Timeout.timeout(ENV['STIME'].to_i)do
        urlChanged=false
        while(!urlChanged)
            begin
                if(current_url != prevUrl)then
                    urlChanged=true
                end
            rescue
                retry
            end
        end
    end
end