auto_test 0.0.7.4 → 0.0.7.5

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.
@@ -0,0 +1,58 @@
1
+
2
+ namespace :auto_test do
3
+ desc "starting the test"
4
+ task :test do
5
+ puts "Running auto_test..."
6
+ sh "rails server -e test -p 3002 -d"
7
+ sh "rspec lib/spec/requests/auto_spec.rb -o 'log/rspec.txt'"
8
+ sh "rm 'log/rspec.txt'"
9
+ Rake::Task[:error_calc].invoke
10
+ Rake::Task[:stop].invoke
11
+ end
12
+
13
+ desc "calculating the errors"
14
+ task :error_calc do
15
+ err = File.read("log/errors.log")
16
+ if err.to_i > 0 then
17
+ puts "Do you want to see the error-path in firefox? Type yes/no:"
18
+ a = STDIN.gets.chomp
19
+ if a == "yes" then
20
+ Rake::Task[:reduce_errors].invoke
21
+ end
22
+ end
23
+ end
24
+
25
+ desc "Reducing the error path"
26
+ task :reduce_errors do
27
+ puts "Reducing the error path, this could take a while..."
28
+ sh "rspec lib/spec/requests/error_reduction_spec.rb -o 'log/reduce_err.txt'"
29
+ sh "rm 'log/reduce_err.txt'"
30
+ Rake::Task[:start_action_path].invoke
31
+ end
32
+
33
+ desc "starting the simulation"
34
+ task :start_action_path do
35
+ # paths = File.new("lib/auto_test/log/new_path.log")
36
+ # n = 0
37
+ # while line = paths.gets do
38
+ # n = n + 1
39
+ # end
40
+ # puts "Error path reduced.There are #{n} paths to be visited."
41
+ # puts "Press Enter to see the path in Firefox! "
42
+ # STDIN.gets
43
+ ruby 'lib/simulation.rb'
44
+ end
45
+
46
+
47
+ desc 'stopping rails'
48
+ task :stop do
49
+ begin
50
+ pid_file = 'tmp/pids/server.pid'
51
+ pid = File.read(pid_file).to_i
52
+ Process.kill 9, pid
53
+ File.delete pid_file
54
+ rescue
55
+ end
56
+ end
57
+
58
+ end
data/lib/test.rb ADDED
@@ -0,0 +1,380 @@
1
+ require 'capybara/rspec'
2
+ #require 'watir-webdriver'
3
+ module AutoTest
4
+ module Test
5
+ module_function
6
+
7
+ # initialize all variables
8
+ def initialize_test
9
+ User.init_users
10
+ User.init_login_data
11
+ init_action_path
12
+ init_depth
13
+ init_inputs
14
+ init_always_present
15
+ Error.init_error
16
+ init_object_dependencies
17
+ init_stop_at_first_error
18
+ init_stop
19
+ init_auth
20
+ init_number_of_test_runs
21
+ end
22
+
23
+ def init_number_of_test_runs
24
+ @@runs = 1
25
+ end
26
+
27
+ def init_stop_at_first_error
28
+ @@stop_at_first_error = false
29
+ end
30
+
31
+ def init_stop
32
+ @@stop = false
33
+ end
34
+
35
+ # initialize the depth of link search with 0
36
+ def init_depth
37
+ @@depth = 0
38
+ end
39
+
40
+ def init_auth
41
+ @@no_auth = false
42
+ end
43
+
44
+ def init_action_path
45
+ @@action_paths = []
46
+ end
47
+
48
+ # initialize dependency array
49
+ def init_object_dependencies
50
+ @@dependencies = []
51
+ end
52
+
53
+ def init_always_present
54
+ @@always_present = []
55
+ end
56
+
57
+ def init_inputs
58
+ @@inputs = Hash.new
59
+ end
60
+
61
+
62
+ def check_invariants
63
+ get_always_present.each do |present|
64
+ if present[0].find(:first, :conditions => "#{present[1]} = '#{present[2]}'") == nil then
65
+ if stop_at_first_error? then stop! end
66
+ logger.info "*"*100
67
+ message = "ERROR : Invariant always_present :
68
+ class '#{present[0]}' attribute '#{present[1]}' value '#{present[2]}' was deleted"
69
+ logger.error message
70
+ logger.info "*"*100
71
+ Error.inc_error(message)
72
+ end
73
+ end
74
+ get_object_dependencies.each do |dependency|
75
+ children = dependency[0].find(:all)
76
+ children.each do |child|
77
+ begin
78
+ dependency[1].find(child.send(dependency[2].to_sym))
79
+ rescue
80
+ if stop_at_first_error? then stop! end
81
+ logger.info "*"*100
82
+ message = "ERROR : Invariant object_dependencies :
83
+ no '#{dependency[1]}' for #{dependency[0]} #{child.id}"
84
+ logger.error message
85
+ logger.info "*"*100
86
+ Error.inc_error(message)
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ def set_number_of_test_runs(number)
93
+ @@runs = number
94
+ end
95
+
96
+ def get_number_of_test_runs
97
+ @@runs
98
+ end
99
+
100
+ def stop_at_first_error(stop_at_first_error)
101
+ @@stop_at_first_error = stop_at_first_error
102
+ end
103
+
104
+ def stop_at_first_error?
105
+ @@stop_at_first_error
106
+ end
107
+
108
+ def stop?
109
+ @@stop
110
+ end
111
+
112
+ def stop!
113
+ @@stop = true
114
+ end
115
+
116
+ #incremtent the depth
117
+ def inc_depth
118
+ @@depth = @@depth + 1
119
+ end
120
+
121
+ def get_depth
122
+ @@depth
123
+ end
124
+
125
+ def set_max_depth(value)
126
+ @@max_depth = value
127
+ end
128
+
129
+ def get_max_depth
130
+ @@max_depth
131
+ end
132
+
133
+
134
+ # get object dependencies
135
+ def get_object_dependencies
136
+ @@dependencies
137
+ end
138
+
139
+ # for all objects of class child, there has to be one object of class parent
140
+ # attribute_name is the column that joins the two tables
141
+ def set_object_dependency(child, parent, attribute_name)
142
+ @@dependencies << [child, parent, attribute_name]
143
+ end
144
+
145
+ # for the name or part of the name of an input field, decide, which values to use
146
+ # if not set, random values matching the input type are used
147
+ def set_input(input, *values)
148
+ @@inputs.store(input, values)
149
+ end
150
+
151
+ def get_inputs
152
+ @@inputs
153
+ end
154
+
155
+ def set_number_of_sessions(number)
156
+ @@number_of_sessions = number
157
+ end
158
+
159
+ def get_number_of_sessions
160
+ @@number_of_sessions
161
+ end
162
+
163
+ # add an array of the classname, key and value of the object to be checked every cycle
164
+ def always_present(class_name, key, value)
165
+ @@always_present << [class_name, key, value]
166
+ end
167
+
168
+ def get_always_present
169
+ @@always_present
170
+ end
171
+
172
+ def get_links_to_exclude
173
+ @@links
174
+ end
175
+
176
+ # list of links that will not be clicked during the test
177
+ def links_to_exclude(*links)
178
+ @@links = links
179
+ end
180
+
181
+ def paths=(value)
182
+ @paths = value
183
+ end
184
+
185
+ def paths
186
+ @paths
187
+ end
188
+
189
+ def iterations
190
+ @iterations
191
+ end
192
+
193
+ def iterations=(value)
194
+ @iterations = value
195
+ end
196
+
197
+ def link_counter
198
+ @link_counter
199
+ end
200
+ def link_counter=(value)
201
+ @link_counter = value
202
+ end
203
+
204
+ def run=(value)
205
+ @run = value
206
+ end
207
+
208
+ def run
209
+ @run
210
+ end
211
+
212
+ def action_path
213
+ @@action_paths
214
+ end
215
+
216
+ def no_auth
217
+ @@no_auth
218
+ end
219
+
220
+ def set_no_auth
221
+ @@no_auth = true
222
+ end
223
+
224
+ def add_to_action_path(hash)
225
+ @@action_paths << hash
226
+ end
227
+
228
+ def users_logged_in
229
+ @users_logged_in
230
+ end
231
+
232
+ def init_ready
233
+ @ready = Array.new(Test.get_number_of_sessions)
234
+ end
235
+
236
+ def ready(i)
237
+ @ready[i-1] = true
238
+ end
239
+
240
+ def ready?
241
+ count = 0
242
+ for i in 0..@ready.size-1 do
243
+ if @ready[i] == true then
244
+ count = count + 1
245
+ end
246
+ end
247
+ if count == @ready.size then
248
+ return true
249
+ else
250
+ return false
251
+ end
252
+ end
253
+
254
+ def init_sessions
255
+ @sessions = Hash.new
256
+ end
257
+
258
+ def init_hash_sessions(key, value)
259
+ @sessions["#{key}"] = value
260
+ end
261
+
262
+ def sessions(index, key, value)
263
+ @sessions["#{index}"]["#{key}"] = value
264
+ end
265
+
266
+ def get_sessions(i, key)
267
+ @sessions["#{i}"]["#{key}"]
268
+ end
269
+
270
+ def get_sessions_array
271
+ @sessions_array
272
+ end
273
+
274
+ def init_sessions_array
275
+ @sessions_array = []
276
+ end
277
+
278
+ def add_to_sessions_array(session)
279
+ @sessions_array << session
280
+ end
281
+
282
+ def users_logged_in=(value)
283
+ @users_logged_in = value
284
+ end
285
+
286
+ def set_max_number_of_session_links(no)
287
+ @@no_session_links = no
288
+ end
289
+
290
+ def get_max_number_of_session_links
291
+ @@no_session_links
292
+ end
293
+
294
+
295
+ def write_user_input_to_file
296
+ f = File.new("lib/auto_test/log/user_input.log", "w")
297
+ if Test.no_auth then
298
+ f.puts("get_number_of_test_runs:#{get_number_of_test_runs}
299
+ get_number_of_sessions:#{get_number_of_sessions}")
300
+ else
301
+ f.puts("get_number_of_test_runs:#{get_number_of_test_runs}
302
+ get_login_path:#{User.get_login_path}
303
+ get_logout_path:#{User.get_logout_path}
304
+ get_login_attributes:#{User.get_login_attributes}
305
+ get_number_of_sessions:#{get_number_of_sessions}
306
+ get_login_data:#{User.get_login_data}
307
+ get_login_names:#{User.get_login_names}
308
+ get_unique_login_attribute_name:#{User.get_unique_login_attribute_name}
309
+ use_db_users:#{User.use_db_users}
310
+ user_class:#{User.user_class}")
311
+ end
312
+ f.close
313
+ end
314
+
315
+ end
316
+
317
+ class TestStarter
318
+ # method to start the test and handle errors
319
+ def test
320
+ Test.write_user_input_to_file
321
+ for j in 1..Test.get_number_of_test_runs do
322
+ Test.run = j
323
+ if !Test.stop?
324
+ number_of_sessions = Test.get_number_of_sessions
325
+ Test.init_sessions_array
326
+ Test.init_sessions
327
+ Test.init_ready
328
+ for i in 0..number_of_sessions-1 do
329
+ sess = Capybara::Session.new(:rack_test,Rails.application)
330
+ Test.add_to_sessions_array sess
331
+ Test.init_hash_sessions(i, Hash.new)
332
+ Test.sessions(i,"depth", 0)
333
+ Test.sessions(i,"iterations", 0)
334
+ if !Test.no_auth then
335
+ user = User.get_users[rand(User.get_users.size)]
336
+ Test.sessions(i,"user", user)
337
+ Test.add_to_action_path "#{i}:#{user.id}ID"
338
+ Test.sessions(i,"current_user", 0)
339
+ end
340
+ Test.sessions(i,"ready", false)
341
+ Test.sessions(i,"iteration", 0)
342
+ Test.sessions(i,"current_depth", 0)
343
+ Test.sessions(i,"paths", Hash.new)
344
+ end
345
+ Error.init_error
346
+ puts
347
+ Test.link_counter = 0
348
+ Test.users_logged_in = 1
349
+ if Test.no_auth then
350
+ Test.users_logged_in = 0
351
+ end
352
+ STDOUT.write("\r#{Test.run}. run: \t#{Test.link_counter} links clicked, \t#{Test.users_logged_in} users logged in, \t#{Error.get_error} Errors")
353
+ while !Test.stop? && !Test.ready? do
354
+ session = rand(number_of_sessions)
355
+ for i in 0..rand(Test.get_max_number_of_session_links) do
356
+ Page.run_session(session)
357
+ end
358
+ end
359
+ end
360
+ end
361
+ Test.check_invariants
362
+ if !Test.stop? then Test.users_logged_in = Test.users_logged_in + 1 end
363
+ f = File.new("lib/auto_test/log/paths.log", "w")
364
+ f.puts(Test.action_path)
365
+ f.close
366
+ puts
367
+ Error.print_errors
368
+ if Error.get_error > 0 then
369
+ f = File.new("lib/auto_test/log/errors.log", "w")
370
+ f.puts(Error.get_error)
371
+ f.puts(Error.get_error_messages)
372
+ f.close
373
+ end
374
+ end
375
+ end
376
+
377
+ autoload :User, 'auto_test/lib/auto_test/user'
378
+ autoload :Error, 'auto_test/lib/auto_test/error'
379
+
380
+ end
data/lib/user.rb ADDED
@@ -0,0 +1,179 @@
1
+ #require 'spec_helper'
2
+ require 'capybara/rspec'
3
+
4
+ module AutoTest
5
+ module User
6
+ module_function
7
+
8
+ # initialize the @users array
9
+ def init_users
10
+ @users = []
11
+ @db_users = [false, nil]
12
+ end
13
+
14
+
15
+ def set_logout_path(path)
16
+ @logout = path
17
+ end
18
+
19
+ def get_logout_path
20
+ @logout
21
+ end
22
+
23
+ # set the attribute_names and fields that are needed to login
24
+ # the argument are two arrays, the first are the database names of the attribute
25
+ # the second are the belonging field names
26
+ def set_login_attribute_names(names, fields)
27
+ @login_attributes = names.zip fields
28
+ end
29
+
30
+ # return the login attributes
31
+ def get_login_attributes
32
+ @login_attributes
33
+ end
34
+
35
+ # get the login path
36
+ def get_login_path
37
+ @login
38
+ end
39
+
40
+ # set the login path
41
+ def set_login_path(path)
42
+ @login = path
43
+ end
44
+
45
+
46
+ def init_login_data
47
+ @data = []
48
+ end
49
+
50
+ def get_login_data
51
+ @data
52
+ end
53
+
54
+ def set_login_data(*data)
55
+ data.each do |d|
56
+ @data << d
57
+ end
58
+
59
+ end
60
+
61
+ def get_users_from_db(user_class_name, bool)
62
+ @users = user_class_name.find(:all)
63
+ if bool then
64
+ @db_users = [true, user_class_name]
65
+ else
66
+ @db_users = [false, user_class_name]
67
+ end
68
+ end
69
+
70
+ def set_login_names(names)
71
+ @names = names
72
+ end
73
+
74
+ def get_login_names
75
+ @names
76
+ end
77
+
78
+
79
+ def set_login_fields(fields)
80
+ @fields = fields
81
+ end
82
+
83
+ def get_login_fields
84
+ @fields
85
+ end
86
+
87
+ def set_unique_login_attribute_name(name)
88
+ @unique_login = name
89
+ end
90
+
91
+ def get_unique_login_attribute_name
92
+ @unique_login
93
+ end
94
+
95
+ def use_db_users
96
+ @db_users[0]
97
+ end
98
+
99
+ def user_class
100
+ @db_users[1]
101
+ end
102
+
103
+ # return the users
104
+ def get_users
105
+ if @db_users[0] then
106
+ @users = @db_users[1].find(:all)
107
+ else
108
+ index = get_login_names.index(get_unique_login_attribute_name)
109
+ @users = []
110
+ @db_users[1].find(:all).each do |u|
111
+ get_login_data.each do |d|
112
+ if d[index] == u.send(get_unique_login_attribute_name.to_sym) then
113
+ @users << u
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+ def set_user_data(value)
121
+ @user_data = value
122
+ end
123
+
124
+ def get_user_data
125
+ @user_data
126
+ end
127
+
128
+
129
+ def logout(session)
130
+ begin
131
+ hash = Hash.new
132
+ hash["#{Test.get_sessions_array.index(session)}:#{session.current_path}"] = get_logout_path
133
+ Test.add_to_action_path hash
134
+ session.visit get_logout_path
135
+ Page.check_for_error_code(session)
136
+ rescue => e
137
+ end
138
+ end
139
+
140
+ # login a user
141
+ def login(user, session)
142
+ begin
143
+ session.visit get_login_path
144
+ user = user.class.find(user.id)
145
+ hash = Hash.new
146
+ hash["#{Test.get_sessions_array.index(session)}:#{session.current_path}"] = Page.all_submits(session).first.value
147
+ Test.add_to_action_path hash
148
+ Page.check_for_error_code(session)
149
+ index = get_login_names.index(get_unique_login_attribute_name)
150
+ @db_users[1].find(:all).each do |u|
151
+ get_login_data.each do |d|
152
+ if d[index] == u.send(get_unique_login_attribute_name.to_sym) then
153
+ set_user_data(get_login_data[index])
154
+ end
155
+ end
156
+ end
157
+ if @db_users[0] then
158
+ get_login_attributes.each do |field|
159
+ session.fill_in field[1], :with => user.send(field[0].to_sym)
160
+ end
161
+ else
162
+ get_login_attributes.each_with_index do |field, i|
163
+ session.fill_in field[1], :with => get_user_data[i]
164
+ end
165
+ end
166
+ session.click_button Page.all_submits(session).first.value
167
+
168
+ Page.check_for_error_code(session)
169
+ rescue (ActiveRecord::RecordNotFound)
170
+ Test.sessions(Test.get_sessions_array.index(session), "depth", Test.get_max_depth+1)
171
+ Test.sessions(Test.get_sessions_array.index(session), "current_depth", Test.get_sessions(Test.get_sessions_array.index(session), "depth"))
172
+ end
173
+
174
+ end
175
+ end
176
+ autoload :Test, 'auto_test/lib/auto_test/test'
177
+ autoload :Page, 'auto_test/lib/auto_test/page'
178
+
179
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: auto_test
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.7.4
5
+ version: 0.0.7.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Maike Hargens
@@ -95,7 +95,17 @@ files:
95
95
  - Rakefile
96
96
  - auto_test.gemspec
97
97
  - lib/auto_test.rb
98
+ - lib/auto_test/railtie.rb
98
99
  - lib/auto_test/version.rb
100
+ - lib/dsl.rb
101
+ - lib/error.rb
102
+ - lib/page.rb
103
+ - lib/simulation.rb
104
+ - lib/spec/requests/auto_spec.rb
105
+ - lib/spec/requests/error_reduction_spec.rb
106
+ - lib/tasks/auto_test.task
107
+ - lib/test.rb
108
+ - lib/user.rb
99
109
  homepage:
100
110
  licenses: []
101
111