Class: Raakt::Test

Inherits:
Object
  • Object
show all
Defined in:
lib/raakt.rb,
lib/iso_language_codes.rb

Constant Summary collapse

ISO_CODES =
[
"aa",
"ab",
"ae",
"af",
"ak",
"am",
"an",
"ar",
"as",
"av",
"ay",
"az",
"ba",
"be",
"bg",
"bh",
"bi",
"bm",
"bn",
"bo",
"br",
"bs",
"ca",
"ce",
"ch",
"co",
"cr",
"cs",
"cv",
"cy",
"da",
"de",
"dv",
"dz",
"ee",
"el",
"en",
"eo",
"es",
"et",
"eu",
"fa",
"ff",
"fi",
"fj",
"fo",
"fr",
"fy",
"ga",
"gd",
"gl",
"gn",
"gu",
"gv",
"ha",
"he",
"hi",
"ho",
"hr",
"ht",
"hu",
"hy",
"hz",
"ia",
"id",
"ie",
"ig",
"ii",
"ik",
"io",
"is",
"it",
"iu",
"ja",
"jv",
"ka",
"kg",
"ki",
"kj",
"kk",
"kl",
"km",
"kn",
"ko",
"kr",
"ks",
"ku",
"kv",
"kw",
"ky",
"la",
"lb",
"lg",
"li",
"ln",
"lo",
"lt",
"lv",
"mg",
"mh",
"mi",
"mk",
"ml",
"mn",
"mo",
"mr",
"ms",
"mt",
"my",
"na",
"nb",
"nd",
"ne",
"ng",
"nl",
"nn",
"no",
"nr",
"nv",
"ny",
"oc",
"oj",
"om",
"or",
"os",
"pa",
"pi",
"pl",
"ps",
"pt",
"qu",
"rm",
"rn",
"ro",
"ru",
"rw",
"sa",
"sc",
"sd",
"se",
"sg",
"sh",
"si",
"sk",
"sl",
"sm",
"sn",
"so",
"sq",
"sr",
"ss",
"st",
"su",
"sv",
"sw",
"ta",
"te",
"tg",
"th",
"ti",
"tk",
"tl",
"tn",
"to",
"tr",
"ts",
"tt",
"tw",
"ty",
"ug",
"uk",
"ur",
"uz",
"ve",
"vi",
"vo",
"wa",
"wo",
"xh",
"yi",
"yo",
"za",
"zh",
"zu"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html = nil, headers = nil) ⇒ Test

Returns a new instance of Test.



83
84
85
86
87
88
89
# File 'lib/raakt.rb', line 83

def initialize(html=nil, headers=nil)
  @html = html
	  @headers = headers
  self.doc = @html if html
	  self.headers = @headers if headers
	  @ignore_bi = false 
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



81
82
83
# File 'lib/raakt.rb', line 81

def headers
  @headers
end

#htmlObject

Returns the value of attribute html.



81
82
83
# File 'lib/raakt.rb', line 81

def html
  @html
end

#ignore_biObject

Returns the value of attribute ignore_bi.



81
82
83
# File 'lib/raakt.rb', line 81

def ignore_bi
  @ignore_bi
end

#user_agentObject

Returns the value of attribute user_agent.



81
82
83
# File 'lib/raakt.rb', line 81

def user_agent
  @user_agent
end

Instance Method Details

#allObject

Call all check methods.



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/raakt.rb', line 108

def all
  messages = []
  
  self.methods.each do |method|
    if method[0..5] == "check_"
      messages += self.send(method)
    end
  end
  
  return messages
end

#alt_to_text(element) ⇒ Object



481
482
483
484
485
486
487
# File 'lib/raakt.rb', line 481

def alt_to_text(element)
		if element.kind_of?(Hpricot::Elem) then
  		element.has_attribute?("alt") ? element['alt'] : ""
		else
			""
		end
end

#check_areasObject

Verify that all area elements have a non-empty alt attribute. See UWEM 1.0 Test 1.1_HTML_01 (together with check_images)



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/raakt.rb', line 200

def check_areas
		messages = []
		area_elements = (@doc/"area")
		area_elements.map { |element| 
			unless element['alt']
messages << ErrorMessage.new(:missing_area_alt, element['name'] || element['id'] || "unknown") 
			else
if element['alt'].length == 0
	messages << ErrorMessage.new(:missing_area_alt_text, element['name'] || element['id'] || "unknown")
end
			end
		}

		messages
end

#check_character_setObject

Verify that the charater set specified in HTTP headers match that specidied in the HTML meta element.



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/raakt.rb', line 144

def check_character_set
	messages = []
	header_charset = meta_charset = ""
	if @headers and @headers.length > 0 then
		if @headers.has_key?("content-type")
			header_charset = parse_charset(@headers["content-type"].to_s)
		end

		#get meta element charset
		meta_elements = @doc.search("//meta[@http-equiv]")
		for element in meta_elements do
			if element["http-equiv"].downcase == "content-type" then
				meta_charset = parse_charset(element["content"])
			end
		end

		if header_charset.length > 0 and meta_charset.length > 0
			unless meta_charset == header_charset
				messages << ErrorMessage.new(:charset_mismatch) 
			end
		end
	end

	return messages

end

#check_document_structureObject

Verify that heading elements (h1-h6) appear in the correct order (no levels skipped). See UWEM 1.0 Test 3.5_HTML_03.



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/raakt.rb', line 235

def check_document_structure
  messages = []
  currentitem = 0
  docheadings = headings
  
  for heading in docheadings
    if currentitem == 0
      if level(heading.name) != 1
        messages << ErrorMessage.new(:first_h_not_h1, "h" + heading.name[1,1])
      end
    else
      if level(heading.name) - level(docheadings[currentitem - 1].name) > 1
        messages << ErrorMessage.new(:wrong_h_structure)
        break
      end  
    end
    
    currentitem += 1
    
  end
  
  messages
end

#check_embedObject

Verify that the embed element isn’t used. See UWEM 1.0 Test 1.1_HTML_06.



137
138
139
140
# File 'lib/raakt.rb', line 137

def check_embed
	return [ErrorMessage.new(:embed_used)] unless (@doc/'embed').empty?
	[]
end

#check_fieldset_legendObject

Verify that all fieldset elements have a legend child element. See UWEM 1.0 Test 12.3_HTML_01.



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/raakt.rb', line 122

def check_fieldset_legend
	messages = []
	fieldsets = (@doc/"fieldset")
	fieldset_instance = 1
	for fieldset in fieldsets 
		if (fieldset/"legend").empty?
			messages << ErrorMessage.new(:fieldset_missing_legend, fieldset_instance.to_s)				
		end
		fieldset_instance += 1
	end
	messages
end

#check_for_formatting_elementsObject

Verify that no formatting elements have been used. See UWEM 1.0 Test 7.2_HTML_01 and Test 7.3_HTML_01.



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

def check_for_formatting_elements
  
  	messages = []

formatting_elements = %w(font b i u tt small big strike s)
		formatting_elements = %w(font u tt small big strike s) if @ignore_bi
	  
 formatting_items = (@doc/formatting_elements.join('|'))
  
  	unless formatting_items.empty?
			found_elements = []
			for element in formatting_items
found_elements << element.name
			end
    	messages << ErrorMessage.new(:missing_semantics, "#{found_elements.join(', ')}")  
 end
	  
 flicker_elements = %w(blink marquee)
 flicker_items = (@doc/flicker_elements.join('|'))
 
  	unless flicker_items.empty?
    	messages << ErrorMessage.new(:has_flicker)  
  	end

  	messages   
end

#check_for_language_infoObject

Verify that the root documet html element as a lang attribute.



327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/raakt.rb', line 327

def check_for_language_info
  messages = []  
	  unless (@doc/'html[@lang]').empty?
lang_code = (@doc/"html").first["lang"].to_s
if lang_code.length < 2
  		messages << ErrorMessage.new(:missing_lang_info) 
end
	  else
  	messages << ErrorMessage.new(:missing_lang_info) 
	  end
	  messages
end

#check_for_nested_tablesObject

Verify that the document does not have any nested tabled. This is indicative of a table-based layout.



261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/raakt.rb', line 261

def check_for_nested_tables
  
  messages = []  
  tables = (@doc/"table")
  
  for table in tables
    unless (table/"table").empty?
      return messages << ErrorMessage.new(:has_nested_tables)
    end
  end
  
  messages
end

#check_formObject

Verify that all form fields have a corresponding label element. See UWEM 1.0 Test 12.4_HTML_02.



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

def check_form
  messages = []
  labels = get_labels
  fields = get_editable_fields
  
  #make sure all fields have associated labels
  label_for_ids = []
  for label in labels
    if label["for"]
      label_for_ids << label["for"]
    end
  end
  
  field_id = nil
  
  for field in fields
    field_id = (field["id"] || "")
    field_identifier = (field["id"] || field["name"] || "unknown")
    if not label_for_ids.include?(field_id)
      messages << ErrorMessage.new(:field_missing_label, field_identifier)
    end
  end   
  
  messages
end

#check_framesObject

Verify that all frame elements have a title atribute.



401
402
403
404
405
406
407
408
409
# File 'lib/raakt.rb', line 401

def check_frames
	  # Covers UWEM Test 12.1_HTML_01
  return [] unless is_frameset
  
  (@doc/"frame").find_all do |frame|
    frame_title = frame['title'] || ''
    normalize_text(frame_title).empty?
  end.map { |frame| ErrorMessage.new(:missing_frame_title, frame['src']) }            
end

#check_has_headingObject

Verify that the document has at least one h1 element.



228
229
230
231
# File 'lib/raakt.rb', line 228

def check_has_heading
  return [ErrorMessage.new(:missing_heading)] if (@doc/'h1').empty?
  []
end

#check_imagesObject

Verify that all img elements have an alt attribute.



193
194
195
196
# File 'lib/raakt.rb', line 193

def check_images
  no_alt_images = (@doc/"img:not([@alt])")
  no_alt_images.map { |img| ErrorMessage.new(:missing_alt, img['src']) }
end

#check_input_type_imgObject

Verify that all input type=image elements have an alt attribute.



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/raakt.rb', line 173

def check_input_type_img
	#Covers UWEM 1.0 Test 1.1_HTML_01

	messages = []
	image_input_buttons = @doc.search("input").select { |element| element['type'] =~ /image/i }
	image_input_buttons.map { |element| 
		unless element['alt']
			messages << ErrorMessage.new(:missing_input_alt, element['name'] || element['id'] || "") 
		else
			if element['alt'].length == 0
				messages << ErrorMessage.new(:missing_input_alt_text, element['name'] || element['id'] || "")
			end
		end
	}

	messages
end

Verify that no link texts are ambiguous. A typical example is the presence of multiple “Read more” links.



360
361
362
363
364
365
366
367
368
369
# File 'lib/raakt.rb', line 360

def check_link_text
  links = get_links
  
  link = links.find do |link|
    links.find { |cmp_link| is_ambiguous_link(link, cmp_link) }
  end
  
  return [] unless link
  [ErrorMessage.new(:ambiguous_link_text, get_link_text(link))]
end

#check_refreshObject

Verify that the document does not use meta-refresh to redirect the user away after a period of time.



413
414
415
416
417
418
419
# File 'lib/raakt.rb', line 413

def check_refresh
  meta_elements = (@doc/'meta')
  
  meta_elements.find_all do |element|
    element["http-equiv"] == "refresh"
  end.map { ErrorMessage.new(:has_meta_refresh) }
end

#check_tablesObject

Verify that all tables have at least on table header (th) element.



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/raakt.rb', line 277

def check_tables
  messages = []  
  tables = (@doc/"table")   
  currenttable = 1
  
  for table in tables     
  	hasth = false
    hasth = true unless (table/">tr>th").empty?
    hasth = true unless (table/">thead>tr>th").empty?
    
    messages << ErrorMessage.new(:missing_th, currenttable.to_s) unless hasth
            
    currenttable += 1
  end
  
  messages
end

#check_titleObject

Verify that the document has a non-empty title element.



219
220
221
222
223
224
# File 'lib/raakt.rb', line 219

def check_title
  title = @doc.at('title')
  return [ErrorMessage.new(:missing_title)] unless title
  return [ErrorMessage.new(:empty_title)] if normalize_text(title.inner_html).empty?
  []			
end

#check_valid_language_codeObject

Verify that the html element has a valid lang code.



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/raakt.rb', line 342

def check_valid_language_code
  messages = []
  unless (@doc/"html[@lang]").empty?
	#load list of valid language codes
	#iso_lang_codes = []
	#IO.foreach(File.dirname(__FILE__) + "/iso_language_codes.txt") { |code| iso_lang_codes << code.chomp }

	doc_main_lang_code = (@doc/"html").first["lang"].to_s.downcase
	unless ISO_CODES.include?(doc_main_lang_code[0..1])
		messages << ErrorMessage.new(:wrong_lang_code, doc_main_lang_code)
	end
  end

  messages
end

#doc=(html) ⇒ Object

Set the HTML used in the test.



92
93
94
95
# File 'lib/raakt.rb', line 92

def doc=(html)
	  Hpricot.buffer_size = 262144 #Allow for asp.net bastard-sized viewstate attributes...
  @doc = Hpricot(html)
end

#downcase_hash_keys(a_hash) ⇒ Object



438
439
440
441
442
# File 'lib/raakt.rb', line 438

def downcase_hash_keys(a_hash)
	downcased_hash = {}
	a_hash.collect {|key,value| downcased_hash[key.downcase] = value}
	return downcased_hash
end

#elements_to_text(element) ⇒ Object



489
490
491
492
493
494
495
496
# File 'lib/raakt.rb', line 489

def elements_to_text(element)
  str = ''
  element.traverse_all_element do |elem|
    elem.kind_of?(Hpricot::Text) ? str += "#{elem}" : str += alt_to_text(elem)
  end
  
  str
end

#get_editable_fieldsObject



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/raakt.rb', line 521

def get_editable_fields
  allfields = (@doc/"textarea|select|input")
  fields = []
  field_type = ""
  
  for field in allfields do
    field_type = field["type"] || ""
    unless ["button", "submit", "hidden", "image"].include?(field_type)
      fields << field
    end
    
  end
  
  return fields
end

#get_labelsObject



516
517
518
# File 'lib/raakt.rb', line 516

def get_labels
  @doc/'label'
end


556
557
558
559
# File 'lib/raakt.rb', line 556

def get_link_text(link)
  text = (elements_to_text(link) || '').strip
  normalize_text(text)
end


565
566
567
568
# File 'lib/raakt.rb', line 565

def get_link_title(link)
  text = (link['title'] || '').strip
  normalize_text(text)
end


561
562
563
# File 'lib/raakt.rb', line 561

def get_link_url(link)
  link['href']
end


462
463
464
# File 'lib/raakt.rb', line 462

def get_links      
  (@doc/'a')
end

#headingsObject

Utility methods



424
425
426
427
428
429
430
# File 'lib/raakt.rb', line 424

def headings
  headings = []
  1.upto(6) do |i| 
    headings.push((@doc/"h#{i}")) if (@doc/"h#{i}").length > 0
  end
  headings.flatten
end


454
455
456
457
458
459
460
# File 'lib/raakt.rb', line 454

def is_ambiguous_link(link_a, link_b)
  return false if links_point_to_same_resource?(link_a, link_b)
  return true if link_text_identical?(link_a, link_b) &&
                 link_title_identical?(link_a, link_b)
  
  false
end

#is_framesetObject



538
539
540
# File 'lib/raakt.rb', line 538

def is_frameset
  (@doc/"frameset").length > 0
end

#langinfo(element) ⇒ Object



466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/raakt.rb', line 466

def langinfo(element)
  langval = ""
  
  if element.class.to_s == 'Tag'      
    if element['lang']
      langval = element['lang']
    end      
  else
    return nil
  end
  
  return langval
end

#level(heading) ⇒ Object



433
434
435
# File 'lib/raakt.rb', line 433

def level(heading)
  Integer(heading[1].chr)
end

Returns:

  • (Boolean)


543
544
545
# File 'lib/raakt.rb', line 543

def link_text_identical?(link_a, link_b)
  get_link_text(link_a) == get_link_text(link_b)
end

Returns:

  • (Boolean)


547
548
549
# File 'lib/raakt.rb', line 547

def link_title_identical?(link_a, link_b)
  get_link_title(link_a) == get_link_title(link_b)
end

Returns:

  • (Boolean)


551
552
553
554
# File 'lib/raakt.rb', line 551

def links_point_to_same_resource?(link_a, link_b)
  (link_a == link_b) ||
  (get_link_url(link_a) == get_link_url(link_b))
end

#normalize_text(text) ⇒ Object



499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/raakt.rb', line 499

def normalize_text(text)
  text ||= ''
  retval = text.gsub(/&nbsp;/, ' ')
  retval = retval.gsub(/&#160;/, ' ')
  retval = retval.gsub(/\n/, '')
  retval = retval.gsub(/\r/, '')
  retval = retval.gsub(/\t/, '')
  while /  /.match(retval) do
    retval = retval.gsub(/  /, ' ')
  end
  
  retval = retval.strip
  
  return retval
end

#parse_charset(contenttype) ⇒ Object



444
445
446
447
448
449
450
451
# File 'lib/raakt.rb', line 444

def parse_charset(contenttype)
	# get charset identifier from content type string
	if contenttype=~/charset=(.*)\w?/ then
		return $1.downcase.strip
	end

	return ""
end