Module: Awetestlib::Regression::Tables

Included in:
Runner
Defined in:
lib/awetestlib/regression/tables.rb

Overview

Methods for handling Tables, Rows, and Cells Rdoc work in progress

Instance Method Summary collapse

Instance Method Details

#count_data_rows(container, data_index, column_index) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/awetestlib/regression/tables.rb', line 367

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

Parameters:

  • browser (Watir::Browser)

    A reference to the browser window or container element to be tested.



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/awetestlib/regression/tables.rb', line 314

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

#exercise_sorting(browser, columnList, desc = '') ⇒ Object Also known as: validate_sorting



394
395
396
397
398
399
400
401
# File 'lib/awetestlib/regression/tables.rb', line 394

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(nc_element, table_index, column_index) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/awetestlib/regression/tables.rb', line 332

def fetch_array_for_table_column(nc_element, table_index, column_index)
  ary = []
  nc_element.tables[table_index].each do |row|
    if get_cell_count(row) >= column_index
      #TODO this assumes column 1 is a number column
      if row[1].text =~ /\d+/
        ary << row[column_index].text
      end
    end
  end
  return ary f
end

#fetch_hash_for_table_column(table, column_index, start_row = 2) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/awetestlib/regression/tables.rb', line 345

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



386
387
388
389
390
391
392
# File 'lib/awetestlib/regression/tables.rb', line 386

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



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/awetestlib/regression/tables.rb', line 265

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/awetestlib/regression/tables.rb', line 13

def get_column_index(table, strg, desc = '', header = false)
  msg1 = " header" if header
  msg = build_message("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



8
9
10
11
# File 'lib/awetestlib/regression/tables.rb', line 8

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

Parameters:

  • browser (Watir::Browser)

    A reference to the browser window or container element to be tested.

  • strg (String, Regexp)

    A string or regular expression to search for in the table..

  • ordinal (Fixnum) (defaults to: 1)

    A number indicating which matching table will have its index returned.

Returns:

  • (Fixnum)

    the index of the table containing strg



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/awetestlib/regression/tables.rb', line 221

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

Return the index of the last row of the specified table.

Parameters:

  • table (Watir::Table)

    A reference to the table in question.

  • pad (Fixnum) (defaults to: 2)

    The number of zeroes to prefix the index to allow correct sorting.

  • every (Fixnum) (defaults to: 1)

    A number indicating which rows in the table actually carry data if the table is padded with empty rows. 1 = every row, 2 = every other row, 3 = every third row, and etc.

Returns:

  • (Fixnum)


53
54
55
56
57
58
# File 'lib/awetestlib/regression/tables.rb', line 53

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.

Parameters:

  • table (Watir::Table)

    A reference to the table in question.

  • strg (String, Regexp)

    A string or regular expression to search for in the table..

  • column_index (Fixnum) (defaults to: nil)

    A number indicating which rows the column to focus the search in.

Returns:

  • (Fixnum)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/awetestlib/regression/tables.rb', line 68

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 = build_message("Find last row in table :id=#{table.id} with text '#{strg}'", msg1)
  dbg = build_message("#{__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) ⇒ Fixnum

Return the index of the first row of the specified table containing strg When not supplied, the entire row is searched for strg.

Parameters:

  • table (Watir::Table)

    A reference to the table in question.

  • strg (String, Regexp)

    A string or regular expression to search for in the table..

  • column_index (Fixnum) (defaults to: nil)

    A number indicating which rows the column to focus the search in.

  • fail_if_found (Boolean) (defaults to: false)

    If true log a failure if strg is found.

Returns:

  • (Fixnum)

    the index of the row containing strg



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
# File 'lib/awetestlib/regression/tables.rb', line 116

def get_index_of_row_with_text(table, strg, column_index = nil, fail_if_found = false)
  debug_to_log("#{__method__}: #{get_callers(5)}")
  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}/
      found = true
      break
    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.

Parameters:

  • table (Watir::Table)

    A reference to the table in question.

  • strg (String, Regexp)

    A string or regular expression to search for in the table..

  • how (Symbol)

    The element attribute used to identify the specific element. Valid values depend on the kind of element. Common values: :text, :id, :title, :name, :class, :href (:link only)

  • what (String, Regexp)

    A string or a regular expression to be found in the how attribute that uniquely identifies the element.

  • column_index (Fixnum) (defaults to: nil)

    A number indicating which rows the column to focus the search in.

Returns:

  • (Fixnum)

    the index of the row containing 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
# File 'lib/awetestlib/regression/tables.rb', line 178

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_row_cells_text_as_array(row) ⇒ Object



359
360
361
362
363
364
365
# File 'lib/awetestlib/regression/tables.rb', line 359

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

Parameters:

  • browser (Watir::Browser)

    A reference to the browser window or container element to be tested.

  • strg (String, Regexp)

    A string or regular expression to search for in the table..

  • ordinal (Fixnum) (defaults to: 1)

    A number indicating which matching table will have its index returned.

Returns:

  • (Watir::Table)

    the table containing strg



251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/awetestlib/regression/tables.rb', line 251

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 = 1) ⇒ 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.

Examples:

(need example and usage)

Parameters:

  • table (Watir::Table)

    A reference to the table.

  • header_index (Fixnum) (defaults to: 1)

    The index of the row containing the header names.

Returns:

  • (Hash)

    Two level hash of hashes. Internal hashes are ‘name’ which allows look-up of a column index



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/awetestlib/regression/tables.rb', line 294

def get_table_headers(table, header_index = 1)
  headers          = Hash.new
  headers['index'] = Hash.new
  headers['name']  = Hash.new
  count            = 1
  table[header_index].each do |cell|
    if cell.text.length > 0
      name                    = cell.text.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 get content headers. '#{$!}'")
end

#text_in_table?(browser, how, what, expected, desc = '') ⇒ Boolean

Returns:

  • (Boolean)


590
591
592
593
594
595
596
597
598
599
600
# File 'lib/awetestlib/regression/tables.rb', line 590

def text_in_table?(browser, how, what, expected, desc = '')
  msg = build_message("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

Returns:

  • (Boolean)


602
603
604
605
606
607
608
609
610
611
612
# File 'lib/awetestlib/regression/tables.rb', line 602

def text_in_table_row_with_text?(table, text, target, desc = '')
  #TODO This needs clarification, renaming
  msg   = build_message("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.

Parameters:

  • browser (Watir::Browser)

    A reference to the browser window or container element to be tested.



577
578
579
580
581
582
583
584
585
586
587
588
# File 'lib/awetestlib/regression/tables.rb', line 577

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



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
# File 'lib/awetestlib/regression/tables.rb', line 405

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



444
445
446
447
448
449
450
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
# File 'lib/awetestlib/regression/tables.rb', line 444

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