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

3

5

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.

Branham answered 7/6, 2012 at 10:7 Comment(0)
I
6

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
Immingle answered 15/6, 2012 at 6:21 Comment(0)
P
6

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)
Parisi answered 7/6, 2012 at 13:29 Comment(2)
@JustinKo - How do I detect a table which has no id, class etc. ? Its enclosed in just a <table> tag ? Thank you.Definitely
@stack1, there are a lot of things you could do. It really depends on the context. Have a look at my in progress book, Watirways for some ideas. Ultimately, if there are no identifiers in the HTML, consider how a user knows which table to look at. At a minimum you should be able to use those identifiers. Other than that, I would suggest opening a new question with the HTML of the page.Parisi
I
6

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
Immingle answered 15/6, 2012 at 6:21 Comment(0)
B
0

Found the solution to my problem

instead of rows{} I used tds{} i.e I changed the code to

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

Its working as I want it to..

Branham answered 10/6, 2012 at 10:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.