ule_page 0.0.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
- require "ule_page/version"
1
+ require 'ule_page/version'
2
2
  require 'rails'
3
3
  require 'active_record'
4
+ require 'ule_page/page_map'
4
5
  require 'site_prism'
5
6
 
6
7
  module UlePage
@@ -13,7 +14,6 @@ module UlePage
13
14
  autoload :Detail, 'ule_page/models/detail'
14
15
  autoload :Index, 'ule_page/models/index'
15
16
 
16
-
17
17
  mattr_accessor :resource_models
18
18
  @@resource_models = []
19
19
 
@@ -26,22 +26,23 @@ module UlePage
26
26
  mattr_accessor :map_initialized
27
27
  @@map_initialized = false
28
28
 
29
+ mattr_accessor :module_name
30
+ @@module_name = 'Page'
31
+
29
32
  def self.setup
30
33
  UlePage::Page.send(:include, Rails.application.routes.url_helpers) if defined? Rails
31
34
  UlePage::Page.send(:include, ActionView::Helpers::NumberHelper) if defined? ActionView
32
35
 
33
- self.add_models
36
+ add_models
34
37
 
35
38
  yield self
36
39
  end
37
40
 
38
41
  def self.add_models
39
- if(defined? ActiveRecord::Base && defined? Rails)
42
+ if defined? ActiveRecord::Base && defined? Rails
40
43
  @@resource_models = Dir["#{Rails.root}/app/models/**/*.rb"].map do |m|
41
- m.chomp('.rb').camelize.split("::").last
44
+ m.chomp('.rb').camelize.split('::').last
42
45
  end
43
46
  end
44
47
  end
45
48
  end
46
-
47
-
@@ -2,12 +2,12 @@ require 'capybara'
2
2
 
3
3
  module UlePage
4
4
  module Helper
5
+ def pg
6
+ UlePage::ModelMatch.get_current_page_with_wait {}
7
+ end
5
8
 
6
9
  def wait_for_ajax
7
10
  page.has_css?('.pace-small .pace-inactive')
8
- # Timeout.timeout(Capybara.default_wait_time) do
9
- # loop until finished_all_ajax_requests?
10
- # end
11
11
  end
12
12
 
13
13
  def finished_all_ajax_requests?
@@ -18,24 +18,6 @@ module UlePage
18
18
  visit '/admin'
19
19
  end
20
20
 
21
- # def signin(user)
22
- # token = User.new_remember_token
23
-
24
- # if need_run_javascript
25
- # if Capybara.current_driver == :selenium
26
- # visit_admin_pages
27
- # page.driver.browser.manage.delete_all_cookies
28
- # Capybara.current_session.driver.browser.manage.add_cookie :name => 'remember_token', :value => token
29
- # else
30
- # page.driver.set_cookie("remember_token", token)
31
- # end
32
- # else
33
- # Capybara.current_session.driver.browser.set_cookie("remember_token=#{token}")
34
- # end
35
-
36
- # user.update_attribute(:remember_token, User.encrypt(token))
37
- # end
38
-
39
21
  def signout
40
22
  browser = Capybara.current_session.driver.browser
41
23
  if need_run_javascript
@@ -43,20 +25,20 @@ module UlePage
43
25
  visit_admin_pages
44
26
  browser.manage.delete_all_cookies
45
27
  else
46
- page.driver.set_cookie("remember_token", '')
28
+ page.driver.set_cookie('remember_token', '')
47
29
  end
48
30
  else
49
31
  if browser.respond_to?(:clear_cookies)
50
32
  # Rack::MockSession
51
33
  browser.clear_cookies
52
34
  else
53
- Capybara.current_session.driver.browser.set_cookie("remember_token=")
35
+ Capybara.current_session.driver.browser.set_cookie('remember_token=')
54
36
  end
55
37
  end
56
38
  end
57
39
 
58
40
  def need_run_javascript
59
- Capybara.current_driver == :selenium or Capybara.current_driver == Capybara.javascript_driver
41
+ (Capybara.current_driver == :selenium) || (Capybara.current_driver == Capybara.javascript_driver)
60
42
  end
61
43
 
62
44
  def confirm_alert
@@ -66,7 +48,7 @@ module UlePage
66
48
  sleep 1 # prevent test from failing by waiting for popup
67
49
  page.driver.browser.accept_js_confirms
68
50
  else
69
- p "pressed ok"
51
+ p 'pressed ok'
70
52
  end
71
53
  end
72
54
 
@@ -3,7 +3,6 @@ require 'site_prism'
3
3
 
4
4
  module UlePage
5
5
  module ModelMatch
6
-
7
6
  class << self
8
7
  include Capybara::DSL
9
8
  def get_current_page_with_wait(special_maps = {})
@@ -12,7 +11,7 @@ module UlePage
12
11
  current = get_current_page special_maps
13
12
 
14
13
  if !current_path.nil? && current.nil?
15
- p "current path is #{current_path}, we can not get the page model, please add it to the get_current_page map. (helper.rb)"
14
+ p "current path is #{current_path}, we can not get the page model, please define the set_urls for the page model."
16
15
 
17
16
  raise
18
17
  end
@@ -21,44 +20,33 @@ module UlePage
21
20
  end
22
21
 
23
22
  def get_current_page(special_maps = {})
24
- resources = UlePage.resource_models || []
25
-
26
- if !UlePage.map_initialized || UlePage.special_maps.empty?
27
- UlePage.special_maps = special_maps
28
- UlePage.map = UlePage.special_maps
23
+ map = prepare_maps special_maps
29
24
 
30
- # => to generate the following
31
- # '/customers' => Page::Customers::Index.new,
32
- # '/customers/new' => Page::Customers::Create.new,
33
- # '/customers/:id' => Page::Customers::Details.new,
34
- # '/customers/:id/edit' => Page::Customers::Edit.new,
35
- resources.each do |model|
25
+ return nil unless current_path
36
26
 
37
- pluralized = model.to_s.underscore.pluralize
38
- page_module_name = model.to_s.pluralize.camelize
27
+ current = get_model map, current_path
39
28
 
40
- next unless Object.const_defined?("Page::#{page_module_name}")
29
+ if current.nil? && current_path.include?('/admin/')
30
+ current = get_model map, current_path[6, current_path.length]
31
+ end
41
32
 
42
- page_module = Object.const_get("Page").const_get(page_module_name)
43
- UlePage.map["/#{pluralized}"] = page_module.const_get("Index").try(:new) rescue false
44
- UlePage.map["/#{pluralized}/new"] = page_module.const_get("Create").new rescue false
45
- UlePage.map["/#{pluralized}/:id"] = page_module.const_get("Details").new rescue false
46
- UlePage.map["/#{pluralized}/:id/edit"] = page_module.const_get("Edit").new rescue false
47
- end
33
+ current
34
+ end
48
35
 
49
- UlePage.map_initialized = true
50
- end
36
+ def prepare_maps(special_maps)
37
+ if !UlePage.map_initialized || UlePage.special_maps.empty?
38
+ UlePage.special_maps = special_maps
39
+ UlePage.map = {}
51
40
 
52
- map = UlePage.map
53
- return nil unless current_path
41
+ generate_map_by_convention UlePage.map, UlePage.resource_models
54
42
 
55
- current = get_model map, current_path
43
+ UlePage.map.merge! UlePage.special_maps if UlePage.special_maps.any?
44
+ UlePage.map.merge! UlePage::PageMap.instance.pages
56
45
 
57
- if current.nil? && current_path.include?('/admin/')
58
- current = get_model map, current_path[6, current_path.length]
46
+ UlePage.map_initialized = true
59
47
  end
60
48
 
61
- current
49
+ UlePage.map
62
50
  end
63
51
 
64
52
  def get_model(map, path)
@@ -71,13 +59,50 @@ module UlePage
71
59
  pattern = k.gsub(/(:\w+)/, '\w+') if k.include?(':')
72
60
 
73
61
  regex = Regexp.new "^#{pattern}$"
74
- if regex.match(path)
75
- return v
76
- end
62
+ return v if regex.match(path)
77
63
  end
78
64
 
79
65
  nil
80
66
  end
67
+
68
+ private
69
+
70
+ def generate_map_by_convention(map, resources)
71
+ resources ||= []
72
+ # => to generate the following
73
+ # '/customers' => Page::Customers::Index.new,
74
+ # '/customers/new' => Page::Customers::Create.new,
75
+ # '/customers/:id' => Page::Customers::Details.new,
76
+ # '/customers/:id/edit' => Page::Customers::Edit.new,
77
+ resources.each do |model|
78
+ pluralized = model.to_s.underscore.pluralize
79
+ page_module_name = model.to_s.pluralize.camelize
80
+
81
+ next unless Object.const_defined?("#{UlePage.module_name}::#{page_module_name}")
82
+
83
+ page_module = Object.const_get(UlePage.module_name).const_get(page_module_name)
84
+ map["/#{pluralized}"] = begin
85
+ page_module.const_get('Index').try(:new)
86
+ rescue StandardError
87
+ false
88
+ end
89
+ map["/#{pluralized}/new"] = begin
90
+ page_module.const_get('Create').new
91
+ rescue StandardError
92
+ false
93
+ end
94
+ map["/#{pluralized}/:id"] = begin
95
+ page_module.const_get('Details').new
96
+ rescue StandardError
97
+ false
98
+ end
99
+ map["/#{pluralized}/:id/edit"] = begin
100
+ page_module.const_get('Edit').new
101
+ rescue StandardError
102
+ false
103
+ end
104
+ end
105
+ end
81
106
  end
82
107
  end
83
- end
108
+ end
@@ -5,7 +5,7 @@ module UlePage
5
5
  element :create_button, 'input[type="submit"]'
6
6
 
7
7
  def submit
8
- self.create_button.click
8
+ create_button.click
9
9
  end
10
10
  end
11
- end
11
+ end
@@ -3,28 +3,31 @@ require 'ule_page/page'
3
3
  module UlePage
4
4
  class Index < Page
5
5
  element_collection :rows, 'tbody tr'
6
+ element :table, 'table'
6
7
 
7
8
  def goto_edit
8
9
  click_link_or_button '编辑'
9
10
  end
10
11
 
11
12
  protected
13
+
12
14
  def key_column
13
- ""
15
+ ''
14
16
  end
15
17
 
16
- def get_row key
17
- find_row key, self.rows
18
+ def get_row(key)
19
+ find_row key, rows
18
20
  end
19
21
 
20
- def get_model_row model
21
- find_row(model[key_column], self.rows)
22
+ def get_model_row(model)
23
+ find_row(model[key_column], rows)
22
24
  end
23
25
 
24
- def get_model_row_anchor model, link_text
26
+ def get_model_row_anchor(model, link_text)
25
27
  row = get_model_row(model)
26
28
  raise 'can not find the row' if row.nil?
29
+
27
30
  row.find(:link_or_button, link_text)
28
31
  end
29
32
  end
30
- end
33
+ end
@@ -4,7 +4,9 @@ require 'rspec/expectations'
4
4
  require 'capybara'
5
5
  require 'ule_page/site_prism_extender'
6
6
  require 'ule_page/helper'
7
+ require 'ule_page/page_map'
7
8
  require 'active_support/inflector'
9
+ require 'active_support/hash_with_indifferent_access'
8
10
 
9
11
  module UlePage
10
12
  class Page < SitePrism::Page
@@ -13,16 +15,27 @@ module UlePage
13
15
  extend UlePage::SitePrismExtender
14
16
  include RSpec::Matchers
15
17
 
18
+ @urls = []
19
+ def self.set_urls(*urls)
20
+ @urls = urls
21
+ add_to_page_map @urls
22
+ set_first_url
23
+ end
24
+
25
+ class << self
26
+ attr_reader :urls
27
+ end
28
+
16
29
  # e.g. is_order_detail?
17
30
  def self.inherited(subclass)
18
- method_name = "is_#{subclass.parent.name.demodulize.singularize.underscore}_#{subclass.name.demodulize.singularize.underscore}?"
31
+ method_name = "is_#{subclass.module_parent_name.demodulize.singularize.underscore}_#{subclass.name.demodulize.singularize.underscore}?"
19
32
  subclass.send(:define_method, method_name) do
20
33
  true
21
34
  end
22
35
  end
23
36
 
24
37
  def open(expansion = {})
25
- self.load expansion
38
+ load expansion
26
39
  self
27
40
  end
28
41
 
@@ -32,15 +45,27 @@ module UlePage
32
45
  # fill_form hashtable
33
46
  # if the fields is empty, it will use all the keys of the hashtable
34
47
  def fill_form(hashtable, fields = [], map = {})
35
- fields = hashtable.keys.map { |k| k.to_sym } if fields.empty?
48
+ hashtable = wrapper_hash(hashtable)
49
+ map = wrapper_hash(map)
50
+
51
+ fields = hashtable.keys.map(&:to_sym) if fields.empty?
36
52
 
37
53
  fields.each do |f|
38
54
  key = f
39
- key = map[f] if map.has_key?(f)
40
-
41
- # p "setting #{f} with #{hashtable[key.to_s]}"
42
-
43
- send(f).send(:set, hashtable[key]) or send(f).send(:select, hashtable[key]) if self.respond_to? f.to_sym
55
+ key = map[f] if map.key?(f)
56
+
57
+ el = send(f)
58
+ val = hashtable[key]
59
+ tag = el.send(:tag_name)
60
+ it = el.send(:[], :type)
61
+
62
+ if tag == 'input' && it == 'checkbox' # checkbox
63
+ el.send(:set, val == 'true')
64
+ elsif tag == 'select' # select
65
+ el.send(:select, val)
66
+ else
67
+ el.send(:set, val)
68
+ end
44
69
  end
45
70
  end
46
71
 
@@ -53,44 +78,64 @@ module UlePage
53
78
  # precondition:
54
79
  # there are the elements mapped to the hashtable keys.
55
80
  def check_form(hashtable, fields = [], map = {})
56
- fields = hashtable.keys.map { |k| k.to_sym } if fields.empty?
81
+ hashtable = wrapper_hash(hashtable)
82
+ map = wrapper_hash(map)
83
+
84
+ fields = hashtable.keys.map(&:to_sym) if fields.empty?
57
85
 
58
86
  fields.each do |f|
59
87
  key = f
60
- key = map[f] if map.has_key?(f)
88
+ key = map[f] if map.key?(f)
61
89
 
62
- if self.respond_to? f.to_sym
63
- el_content = send(f).send(:value)
90
+ next unless respond_to? f.to_sym
64
91
 
65
- expect(el_content).to eq hashtable[key.to_s]
66
- end
92
+ el_content = send(f).send(:value)
67
93
 
94
+ expect(el_content).to eq hashtable[key.to_s]
68
95
  end
69
96
  end
70
97
 
71
- def check_have_content(content)
72
- page.should have_content content
98
+ def check_have_content(content, container = nil)
99
+ container ||= page
100
+ if container.respond_to?(:has_text?)
101
+ expect(container.has_text?(content)).to be_truthy
102
+ else
103
+ expect(container).to have_content(content)
104
+ end
73
105
  end
74
106
 
75
- def check_have_not_content(content)
76
- expect(page).to have_no_content(content)
107
+ def check_have_not_content(content, container = nil)
108
+ container ||= page
109
+ if container.respond_to?(:has_no_text?)
110
+ expect(container.has_no_text?(content)).to be_truthy
111
+ else
112
+ expect(container).not_to have_content(content)
113
+ end
77
114
  end
78
115
 
79
116
  # usage: check_have_hashtable_content hashtable
80
117
  # usage: check_have_hashtable_content hashtable, [:id, :name]
81
- def check_have_hashtable_content(hashtable, keys = [])
118
+ def check_have_hashtable_content(hashtable, keys = [], container = nil)
119
+ hashtable = wrapper_hash(hashtable)
82
120
  keys = hashtable.keys if keys.empty?
83
121
 
84
122
  keys.each do |k|
85
- check_have_content hashtable[k.to_s]
123
+ begin
124
+ check_have_content hashtable[k.to_s], container
125
+ rescue RSpec::Expectations::ExpectationNotMetError => e
126
+ puts "The key: '#{k}'' has no matched content!!"
127
+ raise
128
+ end
86
129
  end
87
130
  end
88
131
 
89
- def check_have_no_hashtable_content(hashtable, keys = [])
132
+ def check_have_no_hashtable_content(hashtable, keys = [], container = nil)
133
+ hashtable = wrapper_hash(hashtable)
134
+
90
135
  keys = hashtable.keys if keys.empty?
91
136
 
92
137
  keys.each do |k|
93
- check_have_not_content hashtable[k.to_s]
138
+ check_have_not_content hashtable[k.to_s], container
94
139
  end
95
140
  end
96
141
 
@@ -114,7 +159,25 @@ module UlePage
114
159
  def go_back
115
160
  click_link_or_button '返回'
116
161
  end
117
- end
118
162
 
163
+ private
164
+
165
+ def self.add_to_page_map(urls = [])
166
+ urls.each { |x| PageMap.instance.pages[x] = new } unless urls.nil?
167
+ end
168
+
169
+ def wrapper_hash(hash)
170
+ return hash unless hash.is_a?(Hash)
119
171
 
172
+ ActiveSupport::HashWithIndifferentAccess.new hash
173
+ end
174
+
175
+ def self.set_first_url
176
+ return unless @urls.any?
177
+
178
+ first_url = @urls.first
179
+ first_url = first_url.gsub(/:(\w+)/, '{\1}')
180
+ set_url first_url
181
+ end
182
+ end
120
183
  end