ae_page_objects 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,10 +2,12 @@ require 'capybara'
2
2
  require 'capybara/dsl'
3
3
 
4
4
  require 'ae_page_objects/version'
5
+ require 'ae_page_objects/exceptions'
5
6
 
6
7
  module AePageObjects
7
8
  autoload :Universe, 'ae_page_objects/core/universe'
8
- autoload :Application, 'ae_page_objects/core/application'
9
+ autoload :Site, 'ae_page_objects/core/site'
10
+ autoload :BasicRouter, 'ae_page_objects/core/basic_router'
9
11
  autoload :ApplicationRouter, 'ae_page_objects/core/application_router'
10
12
  autoload :RakeRouter, 'ae_page_objects/core/rake_router'
11
13
  autoload :Dsl, 'ae_page_objects/core/dsl'
@@ -1,7 +1,4 @@
1
1
  module AePageObjects
2
- class LoadingFailed < StandardError
3
- end
4
-
5
2
  module Concerns
6
3
  module LoadEnsuring
7
4
 
@@ -1,12 +1,9 @@
1
1
  module AePageObjects
2
- class StalePageObject < StandardError
3
- end
4
-
5
- module Concerns
2
+ module Concerns
6
3
  module Staleable
7
4
 
8
5
  def stale?
9
- @stale
6
+ !!@stale
10
7
  end
11
8
 
12
9
  def node
@@ -1,7 +1,4 @@
1
1
  module AePageObjects
2
- class PathNotResolvable < StandardError
3
- end
4
-
5
2
  module Concerns
6
3
  module Visitable
7
4
 
@@ -23,7 +20,7 @@ module AePageObjects
23
20
  def visit(*args)
24
21
  raise ArgumentError, "Cannot pass block to visit()" if block_given?
25
22
 
26
- full_path = application.generate_path(paths.first, *args)
23
+ full_path = site.generate_path(paths.first, *args)
27
24
  raise PathNotResolvable, "#{self.name} not visitable via #{paths.first}(#{args.inspect})" unless full_path
28
25
 
29
26
  Capybara.current_session.visit(full_path)
@@ -39,7 +36,7 @@ module AePageObjects
39
36
  url = current_url_without_params
40
37
 
41
38
  paths.any? do |path|
42
- application.path_recognizes_url?(path, url)
39
+ site.path_recognizes_url?(path, url)
43
40
  end
44
41
  end
45
42
 
@@ -59,4 +56,4 @@ module AePageObjects
59
56
  end
60
57
  end
61
58
  end
62
- end
59
+ end
@@ -1,60 +1,130 @@
1
1
  module AePageObjects
2
- class ApplicationRouter
3
- def path_recognizes_url?(path, url)
4
- if path.is_a?(String)
5
- path.sub(/\/$/, '') == url.sub(/\/$/, '')
6
- elsif path.is_a?(Symbol)
7
- url, router = url_and_router(url)
8
-
9
- ["GET", "PUT", "POST", "DELETE", "PATCH"].each do |method|
10
- router.recognize(request_for(url, method)) do |route, matches, params|
11
- return true if route.name.to_s == path.to_s
2
+ class ApplicationRouter < BasicRouter
3
+
4
+ # This whole file is a kludge and probably belongs in an ae_page_objects-rails extension
5
+
6
+ module Recognizer
7
+
8
+ class Base
9
+ def generate_path(named_route, *args)
10
+ if routes.respond_to?("#{named_route}_path")
11
+ routes.send("#{named_route}_path", *args)
12
12
  end
13
13
  end
14
- false
15
14
  end
16
- end
17
15
 
18
- def generate_path(named_route, *args)
19
- return named_route if named_route.is_a?(String)
16
+ class Rails23 < Base
17
+ def recognizes?(path, url)
18
+ ["GET", "PUT", "POST", "DELETE", "PATCH"].map(&:downcase).map(&:to_sym).each do |method|
19
+ route = ActionController::Routing::Routes.named_routes[path]
20
+ route.recognize(url, {:method => method})
20
21
 
21
- if routes.respond_to?("#{named_route}_path")
22
- routes.send("#{named_route}_path", *args)
22
+ return true if route && route.recognize(url, {:method => method})
23
+ end
24
+
25
+ false
26
+ end
27
+
28
+ private
29
+
30
+ def routes
31
+ @routes ||= begin
32
+ routes_class = Class.new do
33
+ include ActionController::UrlWriter
34
+ end
35
+ ActionController::Routing::Routes.install_helpers(routes_class)
36
+ routes_class.new
37
+ end
38
+ end
23
39
  end
24
- end
25
40
 
26
- private
27
-
28
- def request_for(url, method)
29
- Rails.application.routes.request_class.new(env_for(url, method))
41
+ class Rails3 < Base
42
+ def recognizes?(path, url)
43
+ url, router = url_and_router(url)
44
+
45
+ ["GET", "PUT", "POST", "DELETE", "PATCH"].each do |method|
46
+ router.recognize(request_for(url, method)) do |route, matches, params|
47
+ return true if route.name.to_s == path.to_s
48
+ end
49
+ end
50
+
51
+ false
52
+ end
53
+
54
+
55
+ private
56
+
57
+ def request_for(url, method)
58
+ Rails.application.routes.request_class.new(env_for(url, method))
59
+ end
60
+
61
+ def env_for(url, method)
62
+ begin
63
+ Rack::MockRequest.env_for(url, {:method => method})
64
+ rescue URI::InvalidURIError => e
65
+ raise ActionController::RoutingError, e.message
66
+ end
67
+ end
68
+
69
+ def url_and_router(url)
70
+ url = Rack::Mount::Utils.normalize_path(url) unless url =~ %r{://}
71
+ router = Rails.application.routes.set
72
+
73
+ [url, router]
74
+ end
75
+
76
+ def routes
77
+ @routes ||= begin
78
+ routes_class = Class.new do
79
+ include Rails.application.routes.url_helpers
80
+ end
81
+ routes_class.new
82
+ end
83
+ end
84
+ end
85
+
86
+ class Rails32 < Rails3
87
+
88
+ private
89
+ def url_and_router(url)
90
+ url = Journey::Router::Utils.normalize_path(url) unless url =~ %r{://}
91
+ router = Rails.application.routes.router
92
+
93
+ [url, router]
94
+ end
95
+ end
30
96
  end
31
-
32
- def env_for(url, method)
33
- begin
34
- Rack::MockRequest.env_for(url, {:method => method})
35
- rescue URI::InvalidURIError => e
36
- raise ActionController::RoutingError, e.message
97
+
98
+ def path_recognizes_url?(path, url)
99
+ if path.is_a?(Symbol)
100
+ recognizer.recognizes?(path, url)
101
+ else
102
+ super
37
103
  end
38
104
  end
39
-
40
- def url_and_router(url)
41
- if Rails.version =~ /^3.[01]/
42
- url = Rack::Mount::Utils.normalize_path(url) unless url =~ %r{://}
43
- router = Rails.application.routes.set
105
+
106
+ def generate_path(named_route, *args)
107
+ if named_route.is_a?(Symbol)
108
+ recognizer.generate_path(named_route, *args)
44
109
  else
45
- url = Journey::Router::Utils.normalize_path(url) unless url =~ %r{://}
46
- router = Rails.application.routes.router
110
+ super
47
111
  end
48
-
49
- [url, router]
50
112
  end
51
113
 
52
- def routes
53
- @routes ||= begin
54
- routes_class = Class.new do
55
- include Rails.application.routes.url_helpers
114
+ private
115
+
116
+ def recognizer
117
+ @recognizer ||= begin
118
+ if Rails.version =~ /\A2\.3/
119
+ Recognizer::Rails23.new
120
+ elsif Rails.version =~ /\A3\.[01]/
121
+ Recognizer::Rails3.new
122
+ elsif Rails.version =~ /\A3\.2/
123
+ Recognizer::Rails32.new
124
+ else
125
+ warn "[WARNING]: AePageObjects is not tested against Rails #{Rails.version} and may behave in an undefined manner."
126
+ Recognizer::Rails32.new
56
127
  end
57
- routes_class.new
58
128
  end
59
129
  end
60
130
  end
@@ -0,0 +1,11 @@
1
+ module AePageObjects
2
+ class BasicRouter
3
+ def path_recognizes_url?(path, url)
4
+ path.sub(/\/$/, '') == url.sub(/\/$/, '')
5
+ end
6
+
7
+ def generate_path(named_route, *args)
8
+ named_route
9
+ end
10
+ end
11
+ end
@@ -1,5 +1,5 @@
1
1
  module AePageObjects
2
- class RakeRouter
2
+ class RakeRouter < BasicRouter
3
3
 
4
4
  attr_reader :routes
5
5
 
@@ -18,11 +18,11 @@ module AePageObjects
18
18
  end
19
19
 
20
20
  def path_recognizes_url?(path, url)
21
- if path.is_a?(String)
22
- path.sub(/\/$/, '') == url.sub(/\/$/, '')
23
- elsif path.is_a?(Symbol)
21
+ if path.is_a?(Symbol)
24
22
  route = @routes[path]
25
23
  route && route.matches?(url)
24
+ else
25
+ super
26
26
  end
27
27
  end
28
28
 
@@ -1,7 +1,7 @@
1
1
  module AePageObjects
2
- class Application
2
+ class Site
3
3
  extend AePageObjects::Singleton
4
-
4
+
5
5
  class << self
6
6
  private :new
7
7
 
@@ -13,11 +13,11 @@ module AePageObjects
13
13
  instance.router = router
14
14
  end
15
15
 
16
- def inherited(application_class)
16
+ def inherited(site_class)
17
17
  super
18
18
 
19
- application_class.universe.send(:include, Universe)
20
- application_class.universe.page_objects_application_class = application_class
19
+ site_class.universe.send(:include, Universe)
20
+ site_class.universe.page_objects_site_class = site_class
21
21
  end
22
22
 
23
23
  def universe
@@ -27,7 +27,7 @@ module AePageObjects
27
27
  def from(from_mod)
28
28
  until from_mod == Object
29
29
  if from_mod < AePageObjects::Universe
30
- return from_mod.page_objects_application_class.instance
30
+ return from_mod.page_objects_site_class.instance
31
31
  end
32
32
 
33
33
  from_mod = from_mod.parent
@@ -3,7 +3,7 @@ module AePageObjects
3
3
  def self.included(target)
4
4
  target.class_eval do
5
5
  class << self
6
- attr_accessor :page_objects_application_class
6
+ attr_accessor :page_objects_site_class
7
7
  end
8
8
  end
9
9
  end
@@ -2,14 +2,18 @@ module AePageObjects
2
2
  class Document < Node
3
3
  include Concerns::Visitable
4
4
 
5
+ def initialize
6
+ super(Capybara.current_session)
7
+ end
8
+
5
9
  def document
6
10
  self
7
11
  end
8
12
 
9
13
  class << self
10
14
  private
11
- def application
12
- @application ||= AePageObjects::Application.from(self)
15
+ def site
16
+ @site ||= AePageObjects::Site.from(self)
13
17
  end
14
18
  end
15
19
  end
@@ -0,0 +1,10 @@
1
+ module AePageObjects
2
+ class StalePageObject < StandardError
3
+ end
4
+
5
+ class LoadingFailed < StandardError
6
+ end
7
+
8
+ class PathNotResolvable < StandardError
9
+ end
10
+ end
@@ -1,7 +1,7 @@
1
1
  module AePageObjects
2
2
  class Node
3
3
  module Methods
4
- def initialize(capybara_node = Capybara.current_session)
4
+ def initialize(capybara_node)
5
5
  @node = capybara_node
6
6
  end
7
7
 
@@ -1,3 +1,3 @@
1
1
  module AePageObjects
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ae_page_objects
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
8
+ - 4
9
9
  - 0
10
- version: 0.3.0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Donnie Tognazzini
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-07-08 00:00:00 Z
18
+ date: 2013-07-19 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  type: :runtime
@@ -32,26 +32,10 @@ dependencies:
32
32
  version: "1.1"
33
33
  requirement: *id001
34
34
  prerelease: false
35
- - !ruby/object:Gem::Dependency
36
- type: :runtime
37
- name: nokogiri
38
- version_requirements: &id002 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
41
- - - ~>
42
- - !ruby/object:Gem::Version
43
- hash: 17
44
- segments:
45
- - 1
46
- - 5
47
- - 9
48
- version: 1.5.9
49
- requirement: *id002
50
- prerelease: false
51
35
  - !ruby/object:Gem::Dependency
52
36
  type: :development
53
37
  name: appraisal
54
- version_requirements: &id003 !ruby/object:Gem::Requirement
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
55
39
  none: false
56
40
  requirements:
57
41
  - - ~>
@@ -62,12 +46,12 @@ dependencies:
62
46
  - 5
63
47
  - 1
64
48
  version: 0.5.1
65
- requirement: *id003
49
+ requirement: *id002
66
50
  prerelease: false
67
51
  - !ruby/object:Gem::Dependency
68
52
  type: :development
69
53
  name: mocha
70
- version_requirements: &id004 !ruby/object:Gem::Requirement
54
+ version_requirements: &id003 !ruby/object:Gem::Requirement
71
55
  none: false
72
56
  requirements:
73
57
  - - "="
@@ -78,12 +62,12 @@ dependencies:
78
62
  - 13
79
63
  - 3
80
64
  version: 0.13.3
81
- requirement: *id004
65
+ requirement: *id003
82
66
  prerelease: false
83
67
  - !ruby/object:Gem::Dependency
84
68
  type: :development
85
69
  name: selenium-webdriver
86
- version_requirements: &id005 !ruby/object:Gem::Requirement
70
+ version_requirements: &id004 !ruby/object:Gem::Requirement
87
71
  none: false
88
72
  requirements:
89
73
  - - ">="
@@ -92,7 +76,7 @@ dependencies:
92
76
  segments:
93
77
  - 0
94
78
  version: "0"
95
- requirement: *id005
79
+ requirement: *id004
96
80
  prerelease: false
97
81
  description: Capybara Page Objects pattern
98
82
  email:
@@ -108,10 +92,11 @@ files:
108
92
  - lib/ae_page_objects/concerns/load_ensuring.rb
109
93
  - lib/ae_page_objects/concerns/staleable.rb
110
94
  - lib/ae_page_objects/concerns/visitable.rb
111
- - lib/ae_page_objects/core/application.rb
112
95
  - lib/ae_page_objects/core/application_router.rb
96
+ - lib/ae_page_objects/core/basic_router.rb
113
97
  - lib/ae_page_objects/core/dsl.rb
114
98
  - lib/ae_page_objects/core/rake_router.rb
99
+ - lib/ae_page_objects/core/site.rb
115
100
  - lib/ae_page_objects/core/universe.rb
116
101
  - lib/ae_page_objects/core_ext/module.rb
117
102
  - lib/ae_page_objects/document.rb
@@ -121,6 +106,7 @@ files:
121
106
  - lib/ae_page_objects/elements/collection.rb
122
107
  - lib/ae_page_objects/elements/form.rb
123
108
  - lib/ae_page_objects/elements/select.rb
109
+ - lib/ae_page_objects/exceptions.rb
124
110
  - lib/ae_page_objects/node.rb
125
111
  - lib/ae_page_objects/util/hash_symbolizer.rb
126
112
  - lib/ae_page_objects/util/inflector.rb