Class: Raakt::Test
- Inherits:
-
Object
- Object
- Raakt::Test
- 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
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#html ⇒ Object
Returns the value of attribute html.
-
#ignore_bi ⇒ Object
Returns the value of attribute ignore_bi.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Instance Method Summary collapse
-
#all ⇒ Object
Call all check methods.
- #alt_to_text(element) ⇒ Object
-
#check_areas ⇒ Object
Verify that all area elements have a non-empty alt attribute.
-
#check_character_set ⇒ Object
Verify that the charater set specified in HTTP headers match that specidied in the HTML meta element.
-
#check_document_structure ⇒ Object
Verify that heading elements (h1-h6) appear in the correct order (no levels skipped).
-
#check_embed ⇒ Object
Verify that the embed element isn’t used.
-
#check_fieldset_legend ⇒ Object
Verify that all fieldset elements have a legend child element.
-
#check_for_formatting_elements ⇒ Object
Verify that no formatting elements have been used.
-
#check_for_language_info ⇒ Object
Verify that the root documet html element as a lang attribute.
-
#check_for_nested_tables ⇒ Object
Verify that the document does not have any nested tabled.
-
#check_form ⇒ Object
Verify that all form fields have a corresponding label element.
-
#check_frames ⇒ Object
Verify that all frame elements have a title atribute.
-
#check_has_heading ⇒ Object
Verify that the document has at least one h1 element.
-
#check_images ⇒ Object
Verify that all img elements have an alt attribute.
-
#check_input_type_img ⇒ Object
Verify that all input type=image elements have an alt attribute.
-
#check_link_text ⇒ Object
Verify that no link texts are ambiguous.
-
#check_refresh ⇒ Object
Verify that the document does not use meta-refresh to redirect the user away after a period of time.
-
#check_tables ⇒ Object
Verify that all tables have at least on table header (th) element.
-
#check_title ⇒ Object
Verify that the document has a non-empty title element.
-
#check_valid_language_code ⇒ Object
Verify that the html element has a valid lang code.
-
#doc=(html) ⇒ Object
Set the HTML used in the test.
- #downcase_hash_keys(a_hash) ⇒ Object
- #elements_to_text(element) ⇒ Object
- #get_editable_fields ⇒ Object
- #get_labels ⇒ Object
- #get_link_text(link) ⇒ Object
- #get_link_title(link) ⇒ Object
- #get_link_url(link) ⇒ Object
- #get_links ⇒ Object
-
#headings ⇒ Object
Utility methods.
-
#initialize(html = nil, headers = nil) ⇒ Test
constructor
A new instance of Test.
- #is_ambiguous_link(link_a, link_b) ⇒ Object
- #is_frameset ⇒ Object
- #langinfo(element) ⇒ Object
- #level(heading) ⇒ Object
- #link_text_identical?(link_a, link_b) ⇒ Boolean
- #link_title_identical?(link_a, link_b) ⇒ Boolean
- #links_point_to_same_resource?(link_a, link_b) ⇒ Boolean
- #normalize_text(text) ⇒ Object
- #parse_charset(contenttype) ⇒ Object
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
#headers ⇒ Object
Returns the value of attribute headers.
81 82 83 |
# File 'lib/raakt.rb', line 81 def headers @headers end |
#html ⇒ Object
Returns the value of attribute html.
81 82 83 |
# File 'lib/raakt.rb', line 81 def html @html end |
#ignore_bi ⇒ Object
Returns the value of attribute ignore_bi.
81 82 83 |
# File 'lib/raakt.rb', line 81 def ignore_bi @ignore_bi end |
#user_agent ⇒ Object
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
#all ⇒ Object
Call all check methods.
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/raakt.rb', line 108 def all = [] self.methods.each do |method| if method[0..5] == "check_" += self.send(method) end end return end |
#alt_to_text(element) ⇒ Object
477 478 479 480 481 482 483 |
# File 'lib/raakt.rb', line 477 def alt_to_text(element) if element.kind_of?(Hpricot::Elem) then element.has_attribute?("alt") ? element['alt'] : "" else "" end end |
#check_areas ⇒ Object
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 = [] area_elements = (@doc/"area") area_elements.map { |element| unless element['alt'] << ErrorMessage.new(:missing_area_alt, element['name'] || element['id'] || "unknown") else if element['alt'].length == 0 << ErrorMessage.new(:missing_area_alt_text, element['name'] || element['id'] || "unknown") end end } end |
#check_character_set ⇒ Object
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 = [] header_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 = @doc.search("//meta[@http-equiv]") for element in do if element["http-equiv"].downcase == "content-type" then = parse_charset(element["content"]) end end if header_charset.length > 0 and .length > 0 unless == header_charset << ErrorMessage.new(:charset_mismatch) end end end return end |
#check_document_structure ⇒ Object
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 = [] currentitem = 0 docheadings = headings for heading in docheadings if currentitem == 0 if level(heading.name) != 1 << ErrorMessage.new(:first_h_not_h1, "h" + heading.name[1,1]) end else if level(heading.name) - level(docheadings[currentitem - 1].name) > 1 << ErrorMessage.new(:wrong_h_structure) break end end currentitem += 1 end end |
#check_embed ⇒ Object
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 return [ErrorMessage.new(:embed_used)] unless (@doc/'embed').empty? [] end |
#check_fieldset_legend ⇒ Object
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 = [] fieldsets = (@doc/"fieldset") fieldset_instance = 1 for fieldset in fieldsets if (fieldset/"legend").empty? << ErrorMessage.new(:fieldset_missing_legend, fieldset_instance.to_s) end fieldset_instance += 1 end end |
#check_for_formatting_elements ⇒ Object
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 |
# File 'lib/raakt.rb', line 298 def check_for_formatting_elements = [] 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? << ErrorMessage.new(:missing_semantics, "#{formatting_items.join(', ')}") end flicker_elements = %w(blink marquee) flicker_items = (@doc/flicker_elements.join('|')) unless flicker_items.empty? << ErrorMessage.new(:has_flicker) end end |
#check_for_language_info ⇒ Object
Verify that the root documet html element as a lang attribute.
323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/raakt.rb', line 323 def check_for_language_info = [] unless (@doc/'html[@lang]').empty? lang_code = (@doc/"html").first["lang"].to_s if lang_code.length < 2 << ErrorMessage.new(:missing_lang_info) end else << ErrorMessage.new(:missing_lang_info) end end |
#check_for_nested_tables ⇒ Object
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 = [] tables = (@doc/"table") for table in tables unless (table/"table").empty? return << ErrorMessage.new(:has_nested_tables) end end end |
#check_form ⇒ Object
Verify that all form fields have a corresponding label element. See UWEM 1.0 Test 12.4_HTML_02.
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 |
# File 'lib/raakt.rb', line 369 def check_form = [] 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) << ErrorMessage.new(:field_missing_label, field_identifier) end end end |
#check_frames ⇒ Object
Verify that all frame elements have a title atribute.
397 398 399 400 401 402 403 404 405 |
# File 'lib/raakt.rb', line 397 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_heading ⇒ Object
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_images ⇒ Object
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_img ⇒ Object
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 = [] = @doc.search("input").select { |element| element['type'] =~ /image/i } .map { |element| unless element['alt'] << ErrorMessage.new(:missing_input_alt, element['name'] || element['id'] || "") else if element['alt'].length == 0 << ErrorMessage.new(:missing_input_alt_text, element['name'] || element['id'] || "") end end } end |
#check_link_text ⇒ Object
Verify that no link texts are ambiguous. A typical example is the presence of multiple “Read more” links.
356 357 358 359 360 361 362 363 364 365 |
# File 'lib/raakt.rb', line 356 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_refresh ⇒ Object
Verify that the document does not use meta-refresh to redirect the user away after a period of time.
409 410 411 412 413 414 415 |
# File 'lib/raakt.rb', line 409 def check_refresh = (@doc/'meta') .find_all do |element| element["http-equiv"] == "refresh" end.map { ErrorMessage.new(:has_meta_refresh) } end |
#check_tables ⇒ Object
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 = [] 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? << ErrorMessage.new(:missing_th, currenttable.to_s) unless hasth currenttable += 1 end end |
#check_title ⇒ Object
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_code ⇒ Object
Verify that the html element has a valid lang code.
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/raakt.rb', line 338 def check_valid_language_code = [] 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]) << ErrorMessage.new(:wrong_lang_code, doc_main_lang_code) end end 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
434 435 436 437 438 |
# File 'lib/raakt.rb', line 434 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
485 486 487 488 489 490 491 492 |
# File 'lib/raakt.rb', line 485 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_fields ⇒ Object
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/raakt.rb', line 517 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_labels ⇒ Object
512 513 514 |
# File 'lib/raakt.rb', line 512 def get_labels @doc/'label' end |
#get_link_text(link) ⇒ Object
552 553 554 555 |
# File 'lib/raakt.rb', line 552 def get_link_text(link) text = (elements_to_text(link) || '').strip normalize_text(text) end |
#get_link_title(link) ⇒ Object
561 562 563 564 |
# File 'lib/raakt.rb', line 561 def get_link_title(link) text = (link['title'] || '').strip normalize_text(text) end |
#get_link_url(link) ⇒ Object
557 558 559 |
# File 'lib/raakt.rb', line 557 def get_link_url(link) link['href'] end |
#get_links ⇒ Object
458 459 460 |
# File 'lib/raakt.rb', line 458 def get_links (@doc/'a') end |
#headings ⇒ Object
Utility methods
420 421 422 423 424 425 426 |
# File 'lib/raakt.rb', line 420 def headings headings = [] 1.upto(6) do |i| headings.push((@doc/"h#{i}")) if (@doc/"h#{i}").length > 0 end headings.flatten end |
#is_ambiguous_link(link_a, link_b) ⇒ Object
450 451 452 453 454 455 456 |
# File 'lib/raakt.rb', line 450 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_frameset ⇒ Object
534 535 536 |
# File 'lib/raakt.rb', line 534 def is_frameset (@doc/"frameset").length > 0 end |
#langinfo(element) ⇒ Object
462 463 464 465 466 467 468 469 470 471 472 473 474 |
# File 'lib/raakt.rb', line 462 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
429 430 431 |
# File 'lib/raakt.rb', line 429 def level(heading) Integer(heading[1].chr) end |
#link_text_identical?(link_a, link_b) ⇒ Boolean
539 540 541 |
# File 'lib/raakt.rb', line 539 def link_text_identical?(link_a, link_b) get_link_text(link_a) == get_link_text(link_b) end |
#link_title_identical?(link_a, link_b) ⇒ Boolean
543 544 545 |
# File 'lib/raakt.rb', line 543 def link_title_identical?(link_a, link_b) get_link_title(link_a) == get_link_title(link_b) end |
#links_point_to_same_resource?(link_a, link_b) ⇒ Boolean
547 548 549 550 |
# File 'lib/raakt.rb', line 547 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
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/raakt.rb', line 495 def normalize_text(text) text ||= '' retval = text.gsub(/ /, ' ') retval = retval.gsub(/ /, ' ') 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
440 441 442 443 444 445 446 447 |
# File 'lib/raakt.rb', line 440 def parse_charset(contenttype) # get charset identifier from content type string if contenttype=~/charset=(.*)\w?/ then return $1.downcase.strip end return "" end |