访问表格内的表格(Watir / PageObject) [英] Accessing a table within a table (Watir/PageObject)

查看:134
本文介绍了访问表格内的表格(Watir / PageObject)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Cheezy的PageObject for Cucumber测试运行Watir-webdriver。我是ruby,测试,pageobjects和watir的新手。



我试图访问一个表。具体来说,我试图点击第7列中的图片。



这是我的代码:

  tables.downcase! 
tables.gsub!(/ \s + /,'_')
tables_elements = self.send(#{tables} s_elements)
the_table = tables_elements [table_number]
the_table [row_number] [column_number] .click

运行此代码后,显然它相信表(列,行,一切)都是一个单元格。在查看html之后,看起来这里有另一个表格。该表是某种类型的gwt表格。这意味着我拥有的ID不是用于我想要的表格,而是用于包含我想要的表格的表格。有无论如何得到我想要的?例如:

  the_table [0] .element [row_number] [column_number] 

或者我必须直接操作html吗?因为,可悲的是,似乎没有太多的操作,并且我也没有办法看到通过原始的java代码设置一个类(每个页面上有多个这样的表)。如果你能想到另一种点击图片的方式,我很乐意听到它。



预先感谢您

解决方案

从表中访问表格

您可以通过表格获取表格链接元素。假设你希望表中的第一个表有一个id,你可以这样做:

  your_table_element.table_element 

例如,假设页面html为:

 < table id =known_id> 
< tr>
< td>
< table>
< tr>
< td> A0< / td>
< td> B0< / td>
< / tr>
< / table>
< / td>
< / tr>
< / table>

您可以通过引用已知表格创建一个页面对象:

  class MyPage 
包含PageObject

表(:your_table,:id =>'known_id')
结束

下面给出的是第一行的子表格的第二个单元格:

  puts page.your_table_element.table_element [0] [1] .text 
#=> B0

请注意,您可以将其他定位器传递给 table_element 方法。例如,如果子表实际上是第二个而不是第一个表,则可以这样做:

  puts page.your_table_element。 table_element(:index => 1)[0] [1] .text 
#=> B0

直接访问子表

或者,假设您始终需要子表,您可以改变页面对象来访问它。这是通过将块传递给访问方法来完成的:

pre $ class $ MyPage
包含PageObject

table(:actual_table){table_element(:id =>'known_id')。table_element}
end

这意味着你不需要调用额外的 table_element 方法:

  puts page.actual_table_element [0] [1] .text 
#=> B0


I'm running Watir-webdriver with Cheezy's PageObject for Cucumber testing. I am new to ruby, testing, pageobjects, and watir.

I am trying to access a table. Specifically, I'm trying to click an image in the 7th column.

This is my code:

tables.downcase!
tables.gsub!(/\s+/, '_')
tables_elements = self.send("#{tables}s_elements")
the_table = tables_elements[table_number]
the_table[row_number][column_number].click

With this code running, it becomes apparent that it believes the table (columns, rows, everything) is one cell. After looking at the html, it appears there is another table within this one. The table is a gwt table of some sort. What this means is that the ID I have is not for the table I want, but for a table containing the table I want. Is there anyway to get what I want? For instance:

the_table[0].element[row_number][column_number]

Or do I have to manipulate the html directly? Because, sadly, there doesn't appear to be much to manipulate, and no way that I can see to set a class (multiple of these tables on every page) through the original java code. If you can think of another way to click the image, I'd be happy to hear it

Thank you in advance

解决方案

Accessing table from table:

You can get the table within a table by chaining elements. Assuming that you want the first table within the table with an id, you can do:

your_table_element.table_element

For example, say the page html is:

<table id="known_id">
    <tr>
        <td>
            <table>
                <tr>
                    <td>A0</td>
                    <td>B0</td>
                </tr>
            </table>
        </td>
    </tr>
</table>

You can create a page object with a reference to the known table:

class MyPage
    include PageObject

    table(:your_table, :id => 'known_id')
end

The following would then give you the first row's second cell of the child table:

puts page.your_table_element.table_element[0][1].text
#=> "B0"

Note that you can pass additional locators to the table_element method. For example, if the child table is actually the second instead of the first table, you can do:

puts page.your_table_element.table_element(:index => 1)[0][1].text
#=> "B0"

Accessing child table directly:

Alternatively, assuming you always want the child table, you can change the page object to access it instead. This is done by pass a block to the accessor method:

class MyPage
    include PageObject

    table(:actual_table){ table_element(:id => 'known_id').table_element }
end

Which then means you do not need to call the extra table_element method:

puts page.actual_table_element[0][1].text
#=> "B0"

这篇关于访问表格内的表格(Watir / PageObject)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆