watir-wait_with_refresh 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Justin Ko
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ Watir::WaitWithRefresh
2
+ ==============
3
+
4
+ Watir::WaitWithRefresh extends Watir to include methods that will refresh the page until or while an element is present or a block is true.
5
+
6
+ Installation
7
+ -----------
8
+
9
+ ```ruby
10
+ gem install watir-wait_with_refresh
11
+ ```
12
+
13
+ Usage
14
+ -----------
15
+
16
+ The wait methods can be added after requiring Watir:
17
+
18
+ ```ruby
19
+ require 'watir' #or 'watir-classic' or 'watir-webdriver'
20
+ require 'watir/wait_with_refresh'
21
+ ```
22
+
23
+ Refresh the page until an element is present:
24
+
25
+ ```ruby
26
+ element.refresh_until_present
27
+ ```
28
+
29
+ Refresh the page while an element is present:
30
+
31
+ ```ruby
32
+ element.refresh_while_present
33
+ ```
34
+
35
+ Do something after refreshing the page makes the element present:
36
+
37
+ ```ruby
38
+ element.when_present_after_refresh.text
39
+ ```
40
+
41
+ Refresh the page until a block evaluates as true:
42
+
43
+ ```ruby
44
+ browser.refresh_until{ browser.div.present? }
45
+ ```
46
+
47
+ Refresh the page while a block evaluates as true:
48
+
49
+ ```ruby
50
+ browser.refresh_while{ browser.div.present? }
51
+ ```
@@ -0,0 +1,43 @@
1
+ module Watir
2
+ module WaitWithRefresh
3
+
4
+ #
5
+ # Due to the difference in implementation between watir-classic
6
+ # and watir-webdriver, the browser class is different - "Browser"
7
+ # vs "IE". Monkey patch based on the gem loaded.
8
+ #
9
+
10
+ def self.load()
11
+ if Gem.loaded_specs.keys.include?('watir-classic')
12
+ require_relative 'wait_with_refresh/ie'
13
+ elsif Gem.loaded_specs.keys.include?('watir-webdriver')
14
+ require_relative 'wait_with_refresh/browser'
15
+ else
16
+ raise LoadError, "Watir must be required before Watir::WaitWithRefresh"
17
+ end
18
+ require_relative 'wait_with_refresh/wait'
19
+ require_relative 'wait_with_refresh/element'
20
+ end
21
+ end
22
+ end
23
+
24
+
25
+ if Gem.loaded_specs.keys.include?('watir')
26
+ # Watir was loaded. Override Watir.load_driver to include
27
+ # WaitWithRefresh based on the watir driver loaded.
28
+ module Watir
29
+ class << self
30
+ def load_driver
31
+ require "watir-#{driver}"
32
+ Watir::WaitWithRefresh.load
33
+ end
34
+ end # self
35
+ end # Watir
36
+
37
+ else
38
+ # Watir-classic or watir-webdriver was directly loaded.
39
+ # Therefore, we can already determine the class to monkey patch.
40
+ Watir::WaitWithRefresh.load
41
+
42
+ end
43
+
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ module Watir
3
+ class Browser
4
+
5
+ def refresh_until(*args, &blk)
6
+ Watir::WaitWithRefresh.refresh_until(browser, *args, &blk)
7
+ end
8
+
9
+ def refresh_while(*args, &blk)
10
+ Watir::WaitWithRefresh.refresh_while(browser, *args, &blk)
11
+ end
12
+
13
+ end # Browser
14
+ end # Watir
@@ -0,0 +1,92 @@
1
+ # encoding: utf-8
2
+ module Watir
3
+ class Element
4
+
5
+ #
6
+ # Refresh the page until the element is present.
7
+ #
8
+ # @example
9
+ # browser.button(:id => 'foo').refresh_until_present
10
+ #
11
+ # @param [Fixnum] timeout seconds to wait before timing out
12
+ #
13
+ # @see Watir::WaitWithRefresh
14
+ # @see Watir::Element#present?
15
+ #
16
+
17
+ def refresh_until_present(timeout = 30)
18
+ message = "waiting for #{@selector.inspect} to become present"
19
+ Watir::WaitWithRefresh.refresh_until(browser, timeout, message) { present? }
20
+ end
21
+
22
+ #
23
+ # Refresh the page while the element is present.
24
+ #
25
+ # @example
26
+ # browser.button(:id => 'foo').refresh_while_present
27
+ #
28
+ # @param [Integer] timeout seconds to wait before timing out
29
+ #
30
+ # @see Watir::WaitWithRefresh
31
+ # @see Watir::Element#present?
32
+ #
33
+
34
+ def refresh_while_present(timeout = 30)
35
+ message = "waiting for #{@selector.inspect} to disappear"
36
+ Watir::WaitWithRefresh.refresh_while(browser, timeout, message) { present? }
37
+ end
38
+
39
+ #
40
+ # Refreshes the page until the element is present.
41
+ #
42
+ # @example
43
+ # browser.button(:id => 'foo').when_present_after_refresh.click
44
+ # browser.div(:id => 'bar').when_present_after_refresh { |div| ... }
45
+ # browser.p(:id => 'baz').when_present_after_refresh(60).text
46
+ #
47
+ # @param [Fixnum] timeout seconds to wait before timing out
48
+ #
49
+ # @see Watir::WaitWithRefresh
50
+ # @see Watir::Element#present?
51
+ #
52
+
53
+ def when_present_after_refresh(timeout = 30)
54
+ message = "waiting for #{@selector.inspect} to become present"
55
+
56
+ if block_given?
57
+ Watir::WaitWithRefresh.refresh_until(browser, timeout, message) { present? }
58
+ yield self
59
+ else
60
+ WhenPresentAfterRefreshDecorator.new(self, timeout, message)
61
+ end
62
+ end
63
+
64
+ end # Element
65
+
66
+ #
67
+ # Wraps an Element so that any subsequent method calls are
68
+ # put on hold until the element is present (exists and is visible) on the page.
69
+ #
70
+
71
+ class WhenPresentAfterRefreshDecorator
72
+ def initialize(element, timeout, message = nil)
73
+ @element = element
74
+ @timeout = timeout
75
+ @message = message
76
+ end
77
+
78
+ def respond_to?(*args)
79
+ @element.respond_to?(*args)
80
+ end
81
+
82
+ def method_missing(m, *args, &block)
83
+ unless @element.respond_to?(m)
84
+ raise NoMethodError, "undefined method `#{m}' for #{@element.inspect}:#{@element.class}"
85
+ end
86
+
87
+ Watir::WaitWithRefresh.refresh_until(@element.browser, @timeout, @message) { @element.present? }
88
+
89
+ @element.__send__(m, *args, &block)
90
+ end
91
+ end # WhenPresentAfterRefreshDecorator
92
+ end # Watir
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ module Watir
3
+ class IE
4
+
5
+ def refresh_until(*args, &blk)
6
+ Watir::WaitWithRefresh.refresh_until(browser, *args, &blk)
7
+ end
8
+
9
+ def refresh_while(*args, &blk)
10
+ Watir::WaitWithRefresh.refresh_while(browser, *args, &blk)
11
+ end
12
+
13
+ end # IE
14
+ end # Watir
@@ -0,0 +1,5 @@
1
+ module Watir
2
+ module WaitWithRefresh
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,69 @@
1
+ # encoding: utf-8
2
+ module Watir
3
+ module WaitWithRefresh
4
+
5
+ class TimeoutError < StandardError ; end
6
+
7
+ INTERVAL = 0.1
8
+
9
+ class << self
10
+
11
+ #
12
+ # Refresh the browser until the block evaluates to true or times out.
13
+ #
14
+ # @example
15
+ # Watir::WaitWithRefresh.refresh_until(browser){ browser.a(:id => "ajaxed").visible? }
16
+ #
17
+ # @param [Watir::Browser] browser
18
+ # @param [Fixnum] timeout How long to wait in seconds
19
+ # @param [String] message Message to raise if timeout is exceeded
20
+ # @raise [TimeoutError] if timeout is exceeded
21
+ #
22
+
23
+ def refresh_until(browser, timeout = 30, message = nil, &block)
24
+ end_time = ::Time.now + timeout
25
+
26
+ until ::Time.now > end_time
27
+ result = yield(self)
28
+ return result if result
29
+ sleep INTERVAL
30
+ browser.refresh
31
+ end
32
+
33
+ raise TimeoutError, message_for(timeout, message)
34
+ end
35
+
36
+ #
37
+ # Refresh the browser while the block evaluates to true or times out.
38
+ #
39
+ # @example
40
+ # Watir::WaitWithRefresh.refresh(browser){ browser.a(:id => "ajaxed").visible? }
41
+ #
42
+ # @param [Watir::Browser] browser
43
+ # @param [Fixnum] timeout How long to wait in seconds
44
+ # @param [String] message Message to raise if timeout is exceeded
45
+ # @raise [TimeoutError] if timeout is exceeded
46
+ #
47
+
48
+ def refresh_while(browser, timeout = 30, message = nil, &block)
49
+ end_time = ::Time.now + timeout
50
+
51
+ until ::Time.now > end_time
52
+ return unless yield(self)
53
+ sleep INTERVAL
54
+ browser.refresh
55
+ end
56
+
57
+ raise TimeoutError, message_for(timeout, message)
58
+ end
59
+
60
+ def message_for(timeout, message)
61
+ err = "timed out after #{timeout} seconds"
62
+ err << ", #{message}" if message
63
+
64
+ err
65
+ end
66
+
67
+ end #self
68
+ end # WaitWithRefresh
69
+ end # Watir
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: watir-wait_with_refresh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Justin Ko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: watir
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Watir::WaitWithRefresh extends Watir to include methods that will refresh
31
+ the page until or while an element is present or a block is true. Adds Element#refresh_until_present,
32
+ Element#refresh_while_present, Element#when_present_after_refresh, Browser#refresh_until
33
+ and Browser#refresh_while.
34
+ email: jkotests@gmail.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - lib/watir/wait_with_refresh/browser.rb
40
+ - lib/watir/wait_with_refresh/element.rb
41
+ - lib/watir/wait_with_refresh/ie.rb
42
+ - lib/watir/wait_with_refresh/version.rb
43
+ - lib/watir/wait_with_refresh/wait.rb
44
+ - lib/watir/wait_with_refresh.rb
45
+ - LICENSE
46
+ - README.md
47
+ homepage: https://github.com/jkotests/watir-wait_with_refresh
48
+ licenses: []
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.24
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Extend Watir to wait with page refreshes.
71
+ test_files: []