Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watir Webdriver : Iterating table and storing its content in an array

I am trying to automate a block appearing on the website and comparing its content through CMS table. The issue is I have managed to automate the block appearing on the UI but when I login as admin and try to save the content of the table in an array using iteration there where I fail to do it.

<table id="nodequeue-dragdrop" class="nodequeue-dragdrop sticky-enabled tabledrag-processed sticky-table">
<thead class="tableHeader-processed">
<tbody>
  <tr class="draggable odd">
    <td>
      <a class="tabledrag-handle" href="#" title="Drag to re-order">
      <a href="/car-news/moscow/new-text-1">New Text 1</a>
    </td>
    <td>
    <td>2012-06-06 10:24</td>
    <td style="display: none;">
    <td>
    <td>
    <td class="position">1</td>
  </tr>
  <tr class="draggable even">
    <td>
      <a class="tabledrag-handle" href="#" title="Drag to re-order">
      <a href="/car-news/new-cars/text-2">Text 2 </a>
    </td>
    <td>
    <td>2012-06-06 10:29</td>
    <td style="display: none;">
    <td>
    <td>
    <td class="position">2</td>
  </tr>
  <tr class="draggable odd">
    <td>
      <a class="tabledrag-handle" href="#" title="Drag to re-order">
      <a href="/car-news/new-cars/this-is-text-3">This is Text 3</a>
    </td>
    <td>
    <td>2012-06-05 12:55</td>
    <td style="display: none;">
    <td>
    <td>
    <td class="position">3</td>
  </tr>

The code that I am using is

@text = Array.new
  x = 1
  y = 0

  until x == 10
    y = x -1

   until y == x
    @text[y] = @browser.table(:id,'nodequeue-dragdrop').tbody.row{x}.cell{1}.link(:href =>/car-news/).text

    puts @text[y]
    y=y+1  
   end

  x=x+1
 end

The problem is the scripts runs successfully but even though i have set an iteration the script only reads the 1st element and displays it text and does not goto the 2nd 3rd...and so on elements.

like image 345
NewTester Avatar asked Dec 16 '22 23:12

NewTester


2 Answers

Justin is headed the right direction with using ruby's built in methods for iterating over collections. But consider this, If I am reading your code right, you know you are after the text from specific links, so why iterate over the rows when you could just make a collection of matching links?

link_text_array = Array.new
@browser.table(:id,'nodequeue-dragdrop').links(:href => /car-news/) do |link|
  link_text_array << link.text
end
like image 52
Chuck van der Linden Avatar answered Dec 26 '22 06:12

Chuck van der Linden


There are built in methods to iterate over the rows/columns. Try this:

table_array = Array.new
table = @browser.table(:id,'nodequeue-dragdrop')
table.rows.each do |row|
    row_array = Array.new
    row.cells.each do |cell|
        row_array << cell.text
    end
    table_array << row_array
end
puts table_array  # This will be an array (row) of arrays (column)
like image 30
Justin Ko Avatar answered Dec 26 '22 07:12

Justin Ko