Class: RubySeleniumGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/RubySeleniumGenerator.rb

Instance Method Summary collapse

Constructor Details

#initialize(working_directory) ⇒ RubySeleniumGenerator

Returns a new instance of RubySeleniumGenerator.



3
4
5
# File 'lib/RubySeleniumGenerator.rb', line 3

def initialize(working_directory)
	@working_directory = working_directory
end

Instance Method Details

#make_datahandlerObject



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
# File 'lib/RubySeleniumGenerator.rb', line 171

def make_datahandler
data = <<-EOF
class DataHandler

# This allows you to put data in a csv file and access it in your scripts for
# a data driven approach.
#
# e.g.
# require 'datahandler'
# d = DataHandler.new("data.csv")
# loginId = d.data[0].LoginId		--> line 1 of the csv file and column LoginId
# pin = d.data[0].Pin				--> line 1 of the cvs file and column Pin
#
def initialize(datafile)
	@datafile = datafile
end

def data
	csv_data = File.readlines(@datafile)
	header = csv_data[0].split(",")
	header.each{|z| z.chomp!}
	data = Struct.new("Data", *header)
	csv_data.shift
	data_array = []
	csv_data.each do |line|
		line.each{|z| line.chomp!}
		data_array << data.new(*line.split(","))
	end
return data_array
end

end
EOF
f = File.open("methods/datahandler.rb","w")
f.puts data
f.close
end

#make_execute_all_testsObject



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/RubySeleniumGenerator.rb', line 209

def make_execute_all_tests
data = <<-EOF
require 'RubySelenium'

# FireFox or IE
Batch_Generator.new.go
EOF
f = File.open("execute_all_tests.rb","w")
f.puts data
f.close
end

#make_generate_all_testsObject



221
222
223
224
225
226
227
228
229
# File 'lib/RubySeleniumGenerator.rb', line 221

def make_generate_all_tests
data = <<-EOF
require 'RubySelenium'
Batch_Generator.new.create_all_test_files
EOF
f = File.open("generate_all_tests.rb","w")
f.puts data
f.close
end

#make_methods_directoryObject



15
16
17
# File 'lib/RubySeleniumGenerator.rb', line 15

def make_methods_directory
	Dir.mkdir(@working_directory + "/methods")
end

#make_results_cssObject



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/RubySeleniumGenerator.rb', line 451

def make_results_css
data = <<-EOF
#banner
{
font: bold 14px arial;
text-align: center;
background-color: gray;	
}

#nav
{
font: bold 12px arial;
text-align: center;
background-color: #D8BFD8;
margin-top: 10px;
margin-bottom: 10px;
}

#author
{
font: bold 12px arial;
text-align: center;
background-color: lightgrey;
margin-bottom: 10px;
}

td.dataName
{
font: bold 12px arial;
text-align: center;
background-color: lightgrey;
border-color: black;
}

td.dataValue
{
font: 12px arial;
background-color: beige;
border-color: black;
}

p.text
{
font: 13px arial;
}

p.number_of_messages
{
font: 12px arial;
}

#res_suite_title
{
font: bold 14px arial;
text-align: centre;
background-color: lightblue;
padding: 10px 10px 10px 10px;
}

#res_suite_value
{
font: bold 12px arial;
text-align: centre;
background-color: lightblue;
color: black;
padding: 10px 10px 10px 10px;
}

#res_title
{
font: bold 12px arial;
text-align: center;
background-color: lightgrey;
}

#res_value
{
font: bold 12px arial;
text-align: center;
background-color: beige;
color: black;
}

#res_value_passed
{
font: bold 12px arial;
text-align: center;
background-color: beige;
color: green;
}

#res_value_failed
{
font: bold 12px arial;
text-align: center;
background-color: beige;
color: red;
}

#res_passed
{
font: bold 12px arial;
text-align: center;
background-color: #cfffcf;
color: black;
}

#res_failed
{
font: bold 12px arial;
text-align: center;
background-color: red;
color: black;
}
EOF
f = File.open("resultServer/css/seleniumResults.css","w")
f.puts data
f.close
end

#make_resultServer_css_directoryObject



23
24
25
# File 'lib/RubySeleniumGenerator.rb', line 23

def make_resultServer_css_directory
	Dir.mkdir(@working_directory + "/resultServer/css")
end

#make_resultServer_directoryObject



19
20
21
# File 'lib/RubySeleniumGenerator.rb', line 19

def make_resultServer_directory
	Dir.mkdir(@working_directory + "/resultServer")
end

#make_rubySeleniumConfigObject



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
# File 'lib/RubySeleniumGenerator.rb', line 27

def make_rubySeleniumConfig
	data = <<-EOF
# These configurations are only used when running locally on your machine
# and don't apply for running on a remote server in automated test mode.

# GENERATE_LOCATION
# This sets the location that rubySelenium will generate the html.
# set it with the full path to the folder you want the tests to be generated into.
# e.g. C:/IBM/workspaces/SecureMessaging1/FunctionalTests/WebContent/tests/Ruby

GENERATE_LOCATION = ./tests

# TEST_LOCATION
# This sets the location that rubySelenium should look for when running
# the tests locally. It should point to the url of where the tests live on
# your local server.
#
# e.g. if you navigate to
# http://localhost:9081/Selenium/tests/Ruby/tests/seleniumTestRunner.html
# to find your tests then you should set this property to 
# http://localhost:9081/Selenium/tests/Ruby

TEST_LOCATION = http://localhost:9081/Selenium/tests/Ruby/tests

# LOGGING
# This turns the log output on or off when using the generate command.
# When you run the generate command all the html is generated - this is
# for use when hooked up to the remote cruisecontrol driven automation.
# The logs show all the ruby code that is executed in order to create the html.
# 
# set it to true or false to turn it on or off

LOGGING = false

# IE_LOCATION
# This is the location of your Internet Explorer executable (iexplore.exe)

IE_LOCATION = C:/Program Files/Internet Explorer/IEXPLORE.EXE

# FF_LOCATION
# This is the location of your Firefox executable (firefox.exe)

FF_LOCATION = C:/Program Files/Mozilla Firefox/firefox.exe

# LOCAL_TEST_RESULTS
# After a test has run locally it will dump out the results to the testResults
# directory. You can turn this on or off here by setting it to either true or false.

LOCAL_TEST_RESULTS = false
	EOF
	f = File.open("rubySeleniumConfig.txt", "w")
	f.puts data
	f.close
end

#make_sample_user_dataObject



231
232
233
234
235
236
237
238
239
# File 'lib/RubySeleniumGenerator.rb', line 231

def make_sample_user_data
data = <<-EOF
clientLogin,webInvestorId,queue,emailAddress,clientType,crnNumber,firstName,secondName,postCode,linked 
DirectInvestor,9999999,Retail,[email protected],Direct,8888888,David,Hopes,SW18 5LY,Yes
EOF
f = File.open("methods/user_data.csv","w")
f.puts data
f.close
end

#make_seleniumMethodsObject



82
83
84
85
86
87
88
89
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
160
161
162
163
164
165
166
167
168
169
# File 'lib/RubySeleniumGenerator.rb', line 82

def make_seleniumMethods
data = <<-EOF
# Selenium helper methods - Kingsley Hendrickse @ ThoughtWorks 2005
#
# This is a collection if useful user-defined functions that can be used when writing rubySelenium tests.

require 'methods/datahandler'

class SeleniumMethods

# Supply a selenium object when you instantiate this class
# example:	
#
# require 'methods/seleniumMethods'
#
# Create new SeleniumMethods and RubySelenium instances
# t = SeleniumMethods.new(s = RubySelenium.new)
#
def initialize(selenium)
	@s = selenium
	# Set up Data that can be used in the user-defined functions
	d = DataHandler.new("methods/user_data.csv")
	@DirectInvestor = d.data[0]
end

# The login function takes a type of client - there are currently 5 types of client defined
#
# 1. DirectInvestor
# 2. DirectEnquirer
# 3. BrokerLinkedEnquirer
# 4. BrokerLinkedInvestor
# 5. ShareNetwork
# 
# The login function will then generate login commands using the appropriate data and it will also verify 
# that the client has logged in correctly
#
# example:	 t.login("DirectInvestor")
#
def login(id)

	if id == "DirectInvestor"
		id = @DirectInvestor.crnNumber
	end
		
	@s.open("/someurl")
	@s.open("javascript:var link=document.getElementById('logoutUser');if(link!=null){location.href=link.href;}else{location.hash='dummy';}")
	@s.open("/wps/myportal")
	@s.type("userID","#\{id\}")
	@s.type("password","#\{id\}")
	@s.clickAndWait("login")
	@s.open("/someurl")
	@s.verifyTextPresent("Log out")
	@s.verifyTextPresent("Some Text")
end

# This method is for creating strings of the supplied length. It creates the specified number of characters as the letter x
#
# example:	t.length(10)		-> makes a string of 10 x's e.g.   xxxxxxxxxx
#
def length(num, char="x")
	arr = [] ; (1..num).to_a.each{|x| arr << char}
	return arr
end

# This methods returns todays date in the format required by the secure messaging
#
# example:	t.timeNow		-> returns e.g. 07-Jun-2005
#
def timeNow
	Time.now.strftime("%d %b %Y")
end

# This methods returns todays date in the format required by account management header
# It only checks the date as the server time is different from BST as 'they' don't use nntp for some bizarre reason.
# Before you ask yes yes I did ask why not but there was not really much of an answer and I'm busy....
#
# example:	t.todaysDate		-> returns e.g. July 11, 2005 12:52
#
def todaysDate
	Time.now.strftime("%B %d, %Y :")
end

end
EOF
f = File.open("methods/seleniumMethods.rb","w")
f.puts data
f.close
end

#make_testResults_directoryObject



11
12
13
# File 'lib/RubySeleniumGenerator.rb', line 11

def make_testResults_directory
	Dir.mkdir(@working_directory + "/testResults")
end

#make_tests_directoryObject



7
8
9
# File 'lib/RubySeleniumGenerator.rb', line 7

def make_tests_directory
	Dir.mkdir(@working_directory + "/tests")
end

#make_webserverObject



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
297
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/RubySeleniumGenerator.rb', line 241

def make_webserver
data = <<-ENDOFFILE
#!/usr/local/bin/ruby
require 'webrick'
require 'uri'
require 'net/smtp'
include WEBrick

# set up variables
TO_ADDRESS = "[email protected]"

s = HTTPServer.new( :Port => 2001 )

class ApplicationStatus < HTTPServlet::AbstractServlet

def do_GET(req,res)
res.body = "<html><body><p>Ruby Results WebServer is UP</p></body></html>"
end
end

class SeleniumResults < HTTPServlet::AbstractServlet

def template_header(css_file)
	html =<<-EOF
<html>
<head>
<title>Ruby Selenium Results</title>
<link rel="stylesheet" type="text/css" href="#\{css_file\}" />
</head>
<body>
<div id="banner">Selenium Results</div>
<div id="author">Kingsley Hendrickse (ThoughtWorks)</div>
EOF
html
end

def tableOne(body)
	decoded_body = URI.unescape(body.to_s).gsub("+","\\s")
	resultsArray = decoded_body.split("\\n")
	resultStats = resultsArray[0].split("&")
	
	result = {		
	"result" => resultStats[0].split("=")[1],
	"totalTime" => resultStats[1].split("=")[1],
	"numberTestsPassed" => resultStats[2].split("=")[1],
	"numberTestsFailed" => resultStats[3].split("=")[1],
	"numberCommandsPassed" => resultStats[4].split("=")[1],
	"numberCommandsFailed" => resultStats[5].split("=")[1],
	"numberCommandsError" => resultStats[6].split("=")[1],
	"testSuite" => resultStats[7].split("=")[1]
	}
	
	if result['testSuite'] == nil then result['testSuite'] = "Local Run #\{Time.now\}" ; @testSuite = "Local_Run_#\{Time.now.to_s.gsub(' ','_').gsub(':','_')\}" else @testSuite = result['testSuite'] end
	if result['result'] == "passed" then css_result = "res_passed" ; @status = "Passed" else css_result = "res_failed" ; @status = "Failed" end
		
	table_1 = <<-EOF
<table>
<tr><td><div id="res_suite_title">Results for Test Suite:</div></td><td><div id="res_suite_value">#\{result['testSuite']\}</div></td></tr>
<tr><td><div id="res_title">Run Date</div></td><td><div id="res_value">#\{Time.now.to_s\}</div></td></tr>
<tr><td><div id="res_title">Result Status</div></td><td><div id="#\{css_result\}">#\{result['result']\}</div></td></tr>
<tr><td><div id="res_title">Total Time</div></td><td><div id="res_value">#\{result['totalTime']\}</div></td></tr>
<tr><td><div id="res_title">Number of Tests Passed</div></td><td><div id="res_value_passed">#\{result['numberTestsPassed']\}</div></td></tr>
<tr><td><div id="res_title">Number of Tests Failed</div></td><td><div id="res_value_failed">#\{result['numberTestsFailed']\}</div></td></tr>
<tr><td><div id="res_title">Number of Commands Passed</div></td><td><div id="res_value_passed">#\{result['numberCommandsPassed']\}</div></td></tr>
<tr><td><div id="res_title">Number of Commands Failed</div></td><td><div id="res_value_failed">#\{result['numberCommandsFailed']\}</div></td></tr>
<tr><td><div id="res_title">Number of Command Errors</div></td><td><div id="res_value_failed">#\{result['numberCommandsError']\}</div></td></tr>
</table>
EOF
end

def tableTwo(body)
	decoded_body = URI.unescape(body.to_s).gsub("+","\\s")
	resultsArray = decoded_body.split("\\n")
	resultsArray.shift
	return resultsArray.to_s.gsub(/&testTable\.\d+=/, '<br><hr color="lightblue" size="5"><br>').gsub(/&suite=/, '<br><hr color="lightblue" size="5"><br>')
end

def sendMail(to_address, subject, body)
	msgstr = <<-EOF
From: Selenium Automated Tests
To: #\{TO_ADDRESS.split("@")[0].gsub("."," ")\} <#\{TO_ADDRESS\}>
Subject: #\{subject\}
Date: #\{Time.now\}
Content-Type: text/html
#\{body\}
	EOF
	
	Net::SMTP.start('mail21.hld.uk.fid-intl.com', 25) do |smtp|
		smtp.send_message msgstr, 'from@address', TO_ADDRESS
	end
end

def mail_header
html =<<-EOF
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Selenium Automated Tests</TITLE>
<style>
#res_suite_title
{
font: bold 14px arial;
text-align: centre;
background-color: lightblue;
padding: 10px 10px 10px 10px;
}

#res_suite_value
{
font: bold 12px arial;
text-align: centre;
background-color: lightblue;
color: black;
padding: 10px 10px 10px 10px;
}

#res_title
{
font: bold 12px arial;
text-align: center;
background-color: lightgrey;
}

#res_value
{
font: bold 12px arial;
text-align: center;
background-color: beige;
color: black;
}

#res_value_passed
{
font: bold 12px arial;
text-align: center;
background-color: beige;
color: green;
}

#res_value_failed
{
font: bold 12px arial;
text-align: center;
background-color: beige;
color: red;
}

#res_passed
{
font: bold 12px arial;
text-align: center;
background-color: #cfffcf;
color: black;
}

#res_failed
{
font: bold 12px arial;
text-align: center;
background-color: red;
color: black;
}
</style>
<META content="MSHTML 6.00.2800.1498" name=GENERATOR></HEAD>
EOF
end

def do_GET(req,res)
	res.body = template_header("seleniumResults.css")
	res.body << tableOne(req.body)
	res.body << "<br>"
	res.body << tableTwo(req.body)
	res.body << "</body></html>"
		
	f = File.new("../testResults/#\{@testSuite\}.html", "w")
	f.puts template_header("../RubyWebService/css/seleniumResults.css")
	f.puts tableOne(req.body)
	f.puts "<br>"
	f.puts tableTwo(req.body)
	f.puts "</body></html>"
	f.close
	
	if @testSuite =~ /Local_Run/ then puts "No Mail sent as local run" else 
		subject = "Selenium Automated Test (#\{@testSuite\}) - #\{@status\}"
		sendMail(TO_ADDRESS, subject, mail_header + tableOne(req.body).to_s + "<BR>" + tableTwo(req.body).to_s + "</body></html>")
	end
end



alias do_POST do_GET
end


# mount css
s.mount_proc("/seleniumResults.css") do |req, res|
 res.body = File.read("css/seleniumResults.css")
 res['content-type'] = 'text/css'
end

s.mount("/status", ApplicationStatus)
s.mount("/seleniumResults", SeleniumResults)


trap("INT"){ s.shutdown }
s.start
ENDOFFILE
f = File.open("resultServer/webServer.rb","w")
f.puts data
f.close
end