Module: Awetestlib::Regression::Tables
- Defined in:
- lib/awetestlib/regression/tables.rb
Overview
Methods for handling Tables, Rows, and Cells Rdoc work in progress
Instance Method Summary collapse
- #count_data_rows(container, data_index, column_index) ⇒ Object
- #count_rows_with_string(browser, table_index, strg) ⇒ Object
- #dump_all_tables(browser, to_report = false) ⇒ Object
- #dump_row_cells(row) ⇒ Object
- #dump_table_and_rows(table, to_report = false) ⇒ Object
- #dump_table_rows_and_cells(tbl) ⇒ Object (also: #dump_table_rows)
- #exercise_sorting(browser, columnList, desc = '') ⇒ Object (also: #validate_sorting)
- #fetch_array_for_table_column(table, column_index, start_row = 2) ⇒ Object
- #fetch_hash_for_table_column(table, column_index, start_row = 2) ⇒ Object
- #get_cell_count(row) ⇒ Object
- #get_cell_text_from_row_with_string(nc_element, table_index, column_index, strg) ⇒ Object
- #get_column_index(table, strg, desc = '', header = false) ⇒ Object
- #get_index_for_column_head(panel, table_index, strg, desc = '') ⇒ Object
-
#get_index_for_table_containing_text(browser, strg, ordinal = 1) ⇒ Fixnum
Return the index of a table in browser containing strg.
- #get_index_of_last_row(table, pad = 2, every = 1) ⇒ Fixnum (also: #get_index_for_last_row)
-
#get_index_of_last_row_with_text(table, strg, column_index = nil) ⇒ Fixnum
(also: #get_index_for_last_row_with_text)
Return the index of the last row of the specified table containing strg When not supplied, the entire row is searched for strg.
-
#get_index_of_row_with_text(table, strg, column_index = nil, fail_if_found = false, after_index = nil) ⇒ Fixnum
Return the index of the first row of the specified table containing strg When not supplied, the entire row is searched for strg.
-
#get_index_of_row_with_textfield_value(table, strg, how, what, column_index = nil) ⇒ Fixnum
Return the index of the first row of the specified table containing strg in a text field identified by how and what.
- #get_parent_row(container, element, how, what, limit = 5) ⇒ Object
- #get_row_cells_text_as_array(row) ⇒ Object
-
#get_table_containing_text(browser, strg, ordinal = 1) ⇒ Watir::Table
Return a reference to a table in browser containing strg.
-
#get_table_headers(table, header_index = 0) ⇒ Hash
Return a hash containing a cross reference of the header names and indexes (columns) for the specified table.
- #text_in_table?(browser, how, what, expected, desc = '') ⇒ Boolean
- #text_in_table_row_with_text?(table, text, target, desc = '') ⇒ Boolean (also: #verify_text_in_table_with_text)
-
#verify_column_order(browser, table_index, header_index, exp_ary) ⇒ Object
Verify that a table’s columns are in the expected order by header names.
- #verify_column_sort(browser, nc_element, strg, table_index, column_index = nil) ⇒ Object
- #verify_column_sort_temp_ff(browser, strg, table_index, column_index = nil) ⇒ Object
Instance Method Details
#count_data_rows(container, data_index, column_index) ⇒ Object
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
# File 'lib/awetestlib/regression/tables.rb', line 485 def count_data_rows(container, data_index, column_index) cnt = 0 # get_objects(container, :tables, true) table = container.tables[data_index] dump_table_and_rows(table) if table table.rows.each do |row| if get_cell_count(row) >= column_index #TODO this assumes column 1 is a number column if row[column_index].text =~ /\d+/ cnt += 1 end end end end sleep_for(2) cnt end |
#count_rows_with_string(browser, table_index, strg) ⇒ Object
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/awetestlib/regression/tables.rb', line 431 def count_rows_with_string(browser, table_index, strg) hit = 0 browser.tables[table_index].each do |row| if get_cell_count(row) >= 1 # debug_to_log("#{__method__}: #{row.text}") #TODO this assumes column 1 is a number column if row[1].text =~ /\d+/ if row.text =~ /#{strg}/i hit += 1 debug_to_log("#{__method__}: #{row.text}") end end end end debug_to_log("#{__method__}: hit row count: #{hit}") hit end |
#dump_all_tables(browser, to_report = false) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/awetestlib/regression/tables.rb', line 8 def dump_all_tables(browser, to_report = false) tables = browser.tables msg = '' tbl_cnt = 0 tables.each do |tbl| tbl_cnt += 1 row_cnt = 0 msg << "\n=================\ntable: #{tbl_cnt}\n=================\n#{tbl}\ntext:\n#{tbl.text}" tbl.rows.each do |row| row_cnt += 1 cell_cnt = 0 msg << "\n=================\ntable: #{tbl_cnt} row: #{row_cnt}\n#{row.inspect}\n#{row}\ntext:'#{row.text}'" row.each do |cell| cell_cnt += 1 msg << " \ncell: #{cell_cnt}\n#{cell.inspect}\n#{row}\ntext: '#{cell.text}'" end end end if to_report debug_to_report(msg) else debug_to_log(msg) end end |
#dump_row_cells(row) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/awetestlib/regression/tables.rb', line 66 def dump_row_cells(row) msg = '' cell_cnt = 0 msg << "\n=================\nrow: #{row.inspect}\n#{row}\ntext:'#{row.text}'" row.each do |cell| cell_cnt += 1 msg << "\ncell: #{cell_cnt}\n#{cell.inspect}\n#{row}\ntext: '#{cell.text}'" end debug_to_log(msg) end |
#dump_table_and_rows(table, to_report = false) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/awetestlib/regression/tables.rb', line 33 def dump_table_and_rows(table, to_report = false) msg = "\n=================\ntable\n=================\nn#{table}\n#{table.to_yaml}\nrows:" cnt = 0 table.rows.each do |r| cnt += 1 msg << "\n#{cnt}: #{r.text}" end msg << "\n=================\n=================" if to_report debug_to_report(msg) else debug_to_log(msg) end end |
#dump_table_rows_and_cells(tbl) ⇒ Object Also known as: dump_table_rows
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/awetestlib/regression/tables.rb', line 48 def dump_table_rows_and_cells(tbl) msg = '' row_cnt = 0 msg << "\n=================\ntable: #{tbl.inspect}\n=================\n#{tbl}\ntext:\n#{tbl.text}" tbl.rows.each do |row| row_cnt += 1 cell_cnt = 0 msg << "\n=================\nrow: #{row_cnt}\n#{row.inspect}\n#{row}\ntext:'#{row.text}'" row.each do |cell| cell_cnt += 1 msg << "\ncell: #{cell_cnt}\n#{cell.inspect}\n#{row}\ntext: '#{cell.text}'" end end debug_to_log(msg) end |
#exercise_sorting(browser, columnList, desc = '') ⇒ Object Also known as: validate_sorting
512 513 514 515 516 517 518 519 |
# File 'lib/awetestlib/regression/tables.rb', line 512 def exercise_sorting(browser, columnList, desc = '') #TODO put rescue inside the do loop #parameters: browser and a list of column link text values #example: exercise_sorting(browser,['Division', 'Payee', 'Date'], 'Sortable columns on this page') columnList.each do |column| click(browser, :link, :text, column, desc) end end |
#fetch_array_for_table_column(table, column_index, start_row = 2) ⇒ Object
449 450 451 452 453 454 455 456 457 458 459 460 461 |
# File 'lib/awetestlib/regression/tables.rb', line 449 def fetch_array_for_table_column(table, column_index, start_row = 2) ary = [] row_count = 0 table.each do |row| row_count += 1 if get_cell_count(row) >= column_index if row_count >= start_row ary << row[column_index].text end end end ary end |
#fetch_hash_for_table_column(table, column_index, start_row = 2) ⇒ Object
463 464 465 466 467 468 469 470 471 472 473 474 475 |
# File 'lib/awetestlib/regression/tables.rb', line 463 def fetch_hash_for_table_column(table, column_index, start_row = 2) hash = Hash.new row_count = 0 table.each do |row| row_count += 1 if get_cell_count(row) >= column_index if row_count >= start_row hash[row_count] = row[column_index].text end end end hash end |
#get_cell_count(row) ⇒ Object
504 505 506 507 508 509 510 |
# File 'lib/awetestlib/regression/tables.rb', line 504 def get_cell_count(row) if $watir_script row.column_count else row.cells.length end end |
#get_cell_text_from_row_with_string(nc_element, table_index, column_index, strg) ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/awetestlib/regression/tables.rb', line 382 def get_cell_text_from_row_with_string(nc_element, table_index, column_index, strg) rgx = Regexp.new(strg) text = '' debug_to_log("strg:'#{strg}', rgx:'#{rgx}', table_index:'#{table_index}', column_index:'#{column_index}'") nc_element.tables[table_index].each do |row| cell_count = get_cell_count(row) if cell_count >= column_index #TODO this assumes column 1 is a number column # debug_to_log("row:'#{row.cells}'") cell_1 = row[1].text if cell_1 =~ /\d+/ row_text = row.text if row_text =~ rgx text = row[column_index].text break end end end end text end |
#get_column_index(table, strg, desc = '', header = false) ⇒ Object
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 |
# File 'lib/awetestlib/regression/tables.rb', line 82 def get_column_index(table, strg, desc = '', header = false) msg1 = " header" if header msg = ("Get index of ", msg1, " column containing #{strg}. ", desc) rgx = Regexp.new(strg) row_idx = 0 index = -1 found = false table.each do |row| row_idx += 1 if row.text =~ rgx col_idx = 1 row.each do |cell| if cell.text =~ rgx index = col_idx found = true break end col_idx += 1 end end break if found or header end if found passed_to_log("#{msg} at index #{index}.") index else failed_to_log("#{msg}") nil end rescue failed_to_log("Unable to #{msg} '#{$!}'") end |
#get_index_for_column_head(panel, table_index, strg, desc = '') ⇒ Object
77 78 79 80 |
# File 'lib/awetestlib/regression/tables.rb', line 77 def get_index_for_column_head(panel, table_index, strg, desc = '') table = panel.tables[table_index] get_column_index(table, strg, desc, true) end |
#get_index_for_table_containing_text(browser, strg, ordinal = 1) ⇒ Fixnum
Return the index of a table in browser containing strg. ordinal indicates whether it is the first, second, third, etc. table found with the matching text in strg
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/awetestlib/regression/tables.rb', line 338 def get_index_for_table_containing_text(browser, strg, ordinal = 1) msg = "Get index for table containing text '#{strg}'" index = 0 found = 0 browser.tables.each do |t| index += 1 if t.text =~ /#{strg}/ found += 1 if ordinal > 0 and found == ordinal break end end end if found passed_to_log("#{msg}: #{index}") index else passed_to_log("#{msg}.") nil end rescue failed_to_log("Unable to find index of table containing text '#{strg}' '#{$!}' ") end |
#get_index_of_last_row(table, pad = 2, every = 1) ⇒ Fixnum Also known as: get_index_for_last_row
163 164 165 166 167 168 |
# File 'lib/awetestlib/regression/tables.rb', line 163 def get_index_of_last_row(table, pad = 2, every = 1) index = calc_index(table.row_count, every) index = index.to_s.rjust(pad, '0') #debug_to_log("#{__method__}: index='#{index}' row_count=#{table.row_count} pad=#{pad} every=#{every}") index end |
#get_index_of_last_row_with_text(table, strg, column_index = nil) ⇒ Fixnum Also known as: get_index_for_last_row_with_text
Return the index of the last row of the specified table containing strg When not supplied, the entire row is searched for strg.
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 208 209 210 211 212 213 214 215 |
# File 'lib/awetestlib/regression/tables.rb', line 178 def get_index_of_last_row_with_text(table, strg, column_index = nil) debug_to_log("#{__method__}: #{get_callers(5)}") msg1 = " in column #{column_index}" if column_index msg = ("Find last row in table :id=#{table.id} with text '#{strg}'", msg1) dbg = ("#{__method__}: #{table.id} text by row", msg1) index = 0 found = false at_index = 0 #row_count = table.row_count table.rows.each do |row| cell_count = get_cell_count(row) index += 1 text = '' if column_index col_idx = column_index.to_i if cell_count >= col_idx text = row[col_idx].text end else text = row.text end dbg << "\n#{index}. [#{text}]" if text =~ /#{strg}/ found = true at_index = index end end debug_to_log(dbg) if found passed_to_log("#{msg} at index #{index}.") at_index else failed_to_log("#{msg}") nil end rescue failed_to_log("Unable to #{msg}. '#{$!}'") end |
#get_index_of_row_with_text(table, strg, column_index = nil, fail_if_found = false, after_index = nil) ⇒ Fixnum
Return the index of the first row of the specified table containing strg When not supplied, the entire row is searched for strg. after the row indicated by this argument. When omitted, the first hit is accepted.
228 229 230 231 232 233 234 235 236 237 238 239 240 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 |
# File 'lib/awetestlib/regression/tables.rb', line 228 def get_index_of_row_with_text(table, strg, column_index = nil, fail_if_found = false, after_index = nil) debug_to_log(with_caller("#{get_callers(5)}")) if $debug if fail_if_found msg = 'No ' else msg = 'Find ' end msg << "row in table :id=#{table.id} with text '#{strg}'" msg << " in column #{column_index}" if column_index dbg = "#{__method__}: #{table.id} text by row " dbg << "in column #{column_index}" if column_index index = 0 found = false table.rows.each do |row| cell_count = row.cells.length index += 1 text = '' if column_index col_idx = column_index.to_i if cell_count >= col_idx text = row[col_idx].text end else text = row.text end dbg << "\n#{index}. [#{text}]" if text =~ /#{strg}/ if after_index and index > after_index found = true break else found = true break end end end debug_to_log(dbg) if found if fail_if_found failed_to_log("#{msg} at index #{index}.") else passed_to_log("#{msg} at index #{index}.") end index else if fail_if_found passed_to_log("#{msg}") else failed_to_log("#{msg}") end nil end rescue failed_to_log("Unable to #{msg}. '#{$!}'") end |
#get_index_of_row_with_textfield_value(table, strg, how, what, column_index = nil) ⇒ Fixnum
Return the index of the first row of the specified table containing strg in a text field identified by how and what. When not supplied, the entire row is searched for strg.
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 |
# File 'lib/awetestlib/regression/tables.rb', line 295 def get_index_of_row_with_textfield_value(table, strg, how, what, column_index = nil) msg = "Find row in table :id=#{table.id} with value '#{strg}' in text_field #{how}=>'#{what} " msg << " in column #{column_index}" if column_index index = 0 found = false table.rows.each do |row| cell_count = get_cell_count(row) index += 1 text = '' if column_index col_idx = column_index.to_i if cell_count >= col_idx if row[col_idx].text_field(how, what).exists? value = row[col_idx].text_field(how, what).value end end else if row.text_field(how, what).exists? value = row.text_field(how, what).value sleep(0.25) end end if value and value =~ /#{strg}/ found = true break end end if found passed_to_log("#{msg} at index #{index}.") else failed_to_log("#{msg}") end index rescue failed_to_log("Unable to #{msg}. '#{$!}'") end |
#get_parent_row(container, element, how, what, limit = 5) ⇒ Object
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 |
# File 'lib/awetestlib/regression/tables.rb', line 115 def get_parent_row(container, element, how, what, limit = 5) msg = "#{__method__}: #{element.to_s.upcase} :#{how}='#{what}'" target = nil parent = nil case element when :link target = container.link(how, what) when :select_list target = container.select_list(how, what) when :text_field target = container.text_field(how, what) when :checkbox target = container.checkbox(how, what) when :radio target = container.radio(how, what) else fail "#{element.to_s.upcase} not supported." end if target count = 0 parent = target.parent until parent.is_a?(Watir::TableRow) do parent = parent.parent count += 1 if count > limit failed_to_log("Parent row not within #{limit} ancestors.") end end else failed_to_log(msg) end if parent.is_a?(Watir::TableRow) passed_to_log(msg) parent else failed_to_log(msg) end rescue failed_to_log(unable_to) end |
#get_row_cells_text_as_array(row) ⇒ Object
477 478 479 480 481 482 483 |
# File 'lib/awetestlib/regression/tables.rb', line 477 def get_row_cells_text_as_array(row) ary = [] row.each do |cell| ary << cell.text end ary end |
#get_table_containing_text(browser, strg, ordinal = 1) ⇒ Watir::Table
Return a reference to a table in browser containing strg. ordinal indicates whether it is the first, second, third, etc. table found with the matching text in strg
368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/awetestlib/regression/tables.rb', line 368 def get_table_containing_text(browser, strg, ordinal = 1) msg = "Get table #{ordinal} containing text '#{strg}'" index = get_index_for_table_containing_text(browser, strg, ordinal) if index passed_to_log(msg) browser.tables[index] else failed_to_log(msg) nil end rescue failed_to_log("Unable to find index of table containing text '#{strg}' '#{$!}' ") end |
#get_table_headers(table, header_index = 0) ⇒ Hash
Return a hash containing a cross reference of the header names and indexes (columns) for the specified table. by the header name, and ‘index’ which allows look-up of the name by the column index.
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/awetestlib/regression/tables.rb', line 411 def get_table_headers(table, header_index = 0) headers = Hash.new headers['index'] = Hash.new headers['name'] = Hash.new count = 0 table[header_index].cells.each do |cell| if cell.text.length > 0 name = cell.text.strip.gsub(/\s+/, ' ') headers['index'][count] = name headers['name'][name] = count end count += 1 end #debug_to_log("#{__method__}: headers:\n#{headers.to_yaml}") headers rescue failed_to_log(unable_to) end |
#text_in_table?(browser, how, what, expected, desc = '') ⇒ Boolean
708 709 710 711 712 713 714 715 716 717 718 |
# File 'lib/awetestlib/regression/tables.rb', line 708 def text_in_table?(browser, how, what, expected, desc = '') msg = ("Table :#{how}=>#{what} contains '#{expected}.", desc) if browser.table(how, what).text =~ expected passed_to_log(msg) true else failed_to_log(msg) end rescue failed_to_log("Unable to verify that #{msg}': '#{$!}'") end |
#text_in_table_row_with_text?(table, text, target, desc = '') ⇒ Boolean Also known as: verify_text_in_table_with_text
720 721 722 723 724 725 726 727 728 729 730 |
# File 'lib/awetestlib/regression/tables.rb', line 720 def text_in_table_row_with_text?(table, text, target, desc = '') #TODO This needs clarification, renaming msg = ("Table :id=>#{table.id} row with text '#{text} also contains '#{target}.", desc) index = get_index_of_row_with_text(table, text) if table[index].text =~ target passed_to_log(msg) true else failed_to_log(msg) end end |
#verify_column_order(browser, table_index, header_index, exp_ary) ⇒ Object
Verify that a table’s columns are in the expected order by header names. The table is identified by its index within the container browser.
695 696 697 698 699 700 701 702 703 704 705 706 |
# File 'lib/awetestlib/regression/tables.rb', line 695 def verify_column_order(browser, table_index, header_index, exp_ary) mark_testlevel("Begin #{__method__.to_s.titleize}", 0) row = browser.tables[table_index][header_index] act_ary = get_row_cells_text_as_array(row) if exp_ary == act_ary passed_to_log("Column order [#{act_ary.join(', ')}] appeared as expected.") else failed_to_log("Column order [#{act_ary.join(', ')}] not as expected [#{exp_ary.join(', ')}].") end mark_testlevel("End #{__method__.to_s.titleize}", 0) end |
#verify_column_sort(browser, nc_element, strg, table_index, column_index = nil) ⇒ Object
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 |
# File 'lib/awetestlib/regression/tables.rb', line 523 def verify_column_sort(browser, nc_element, strg, table_index, column_index=nil) mark_testlevel("Verify Column Sort '#{strg}'", 3) if not column_index column_index = get_index_for_column_head(nc_element, table_index, strg) end if column_index bfr_ary = fetch_array_for_table_column(nc_element, table_index, column_index) if strg =~ /date/i exp_ary = bfr_ary.sort { |x, y| Date.parse(x) <=> Date.parse(y) } else exp_ary = bfr_ary.sort { |x, y| x.gsub(',', '') <=> y.gsub(',', '') } end if click_text(browser, strg) if column_index sleep_for(2.5) else sleep_for(1) end act_ary = fetch_array_for_table_column(nc_element, table_index, column_index) if exp_ary == act_ary passed_to_log("Click on column '#{strg}' produces expected sorted list.") true else failed_to_log("Click on column '#{strg}' fails to produce expected sorted list.") debug_to_log("Original order ['#{bfr_ary.join("', '")}']") debug_to_log("Expected order ['#{exp_ary.join("', '")}']") debug_to_log(" Actual order ['#{act_ary.join("', '")}']") end end else failed_to_log("Unable to locate column index for '#{strg}' to verify sort.") end rescue failed_to_log("Unable to verify sort on column '#{strg}'. #{$!}") end |
#verify_column_sort_temp_ff(browser, strg, table_index, column_index = nil) ⇒ Object
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 |
# File 'lib/awetestlib/regression/tables.rb', line 562 def verify_column_sort_temp_ff(browser, strg, table_index, column_index=nil) mark_testlevel("Verify Column Sort '#{strg}'", 3) if not column_index column_index = get_index_for_column_head(browser, table_index, strg) end if column_index bfr_ary = fetch_array_for_table_column(browser, table_index, column_index) if strg =~ /date/i exp_ary = bfr_ary.sort { |x, y| Date.parse(x) <=> Date.parse(y) } else exp_ary = bfr_ary.sort { |x, y| x.gsub(',', '') <=> y.gsub(',', '') } end if click_text(browser, strg) sleep_for(3) act_ary = fetch_array_for_table_column(browser, table_index, column_index) if exp_ary == act_ary passed_to_log("Click on column '#{strg}' produces expected sorted list.") true else failed_to_log("Click on column '#{strg}' fails to produce expected sorted list.") debug_to_log("Original order ['#{bfr_ary.join("', '")}']") debug_to_log("Expected order ['#{exp_ary.join("', '")}']") debug_to_log(" Actual order ['#{act_ary.join("', '")}']") end end else failed_to_log("Unable to locate column index for '#{strg}' to verify sort.") end rescue failed_to_log("Unable to verify sort on column '#{strg}'. #{$!}") end |