Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get class of an element in ruby / watir?

I've got code for a table header:

<thead>
<tr class="ui-jqgrid-labels ui-sortable" role="rowheader" style="">
    <th id="categories_formName" role="columnheader" class="ui-state-default ui-th-column ui-th-ltr" style="width: 250px;">
        <div id="jqgh_categories_formName" class="ui-jqgrid-sortable">Category Name</div>
    </th>
    <th id="categories_totalClicks" role="columnheader" class="ui-state-default ui-th-column ui-th-ltr" style="width: 99px;">
        <div id="jqgh_categories_totalClicks" class="ui-jqgrid-sortable">Clicks</div>
    </th>
    <th id="categories_avgCpc" role="columnheader" class="ui-state-default ui-th-column ui-th-ltr" style="width: 99px;">
        <div id="jqgh_categories_avgCpc" class="ui-jqgrid-sortable">Avg CPC($)</div>
    </th>
    <th id="categories_totalCost" role="columnheader" class="ui-state-default ui-th-column ui-th-ltr" style="width: 99px;">
        <div id="jqgh_categories_totalCost" class="ui-jqgrid-sortable">Total Cost($)</div>
    </th>
    <th id="categories_convertToSale" role="columnheader" class="ui-state-default ui-th-column ui-th-ltr disabledHeader" style="width: 99px;">
        <div id="jqgh_categories_convertToSale" class="ui-jqgrid-sortable">CTS(%)</div>
    </th>
    <th id="categories_totalOrders" role="columnheader" class="ui-state-default ui-th-column ui-th-ltr disabledHeader" style="width: 99px;">
        <div id="jqgh_categories_totalOrders" class="ui-jqgrid-sortable">Total Orders</div>
    </th>
    <th id="categories_totalSales" role="columnheader" class="ui-state-default ui-th-column ui-th-ltr disabledHeader" style="width: 99px;">
        <div id="jqgh_categories_totalSales" class="ui-jqgrid-sortable">Sales($)</div>
    </th>
    <th id="categories_costOfSale" role="columnheader" class="ui-state-default ui-th-column ui-th-ltr disabledHeader" style="width: 96px;">
        <div id="jqgh_categories_costOfSale" class="ui-jqgrid-sortable">COS(%)</div>
    </th>
</tr>

and need to find how many th tags has class "disabledHeader" or at least get class of a particular (addressed by id).

When I do:

cl = b.th(:xpath, '//th[@id="categories_convertToSale"]')
cl.exist?
=> true
cl.inspect
=> "#<Watir::TableHeaderCell:0x..f9b976cc1015b866a located=true selector={:xpath=>\"//th[@id=\\\"categories_convertToSale\\\"]\", :tag_name=>\"th\"}>"
cl.class
=> Watir::TableHeaderCell

cl[@class] or cl(:class) return errors.

b.element(:class, "disabledHeader").size returns method missing error.

How to address all the th-s of this class?

like image 216
earlyadopter Avatar asked Apr 16 '13 17:04

earlyadopter


1 Answers

Zeljko addressed your question about counting the number of th tags that match some pattern.

Regarding getting the class of something, that depends on which flavor of 'class' you are referring to.

Ruby Classes

Ruby is an object oriented language, objects being defined by the 'class' keyword in the language. Watir makes use of this, and has an internal object model that parallels HTML objects. In Ruby, the .class method returns the class of an object, this is hardwired into the language. (in fact you will not see the .class method described anywhere in the watir rDocs) This is what you were doing when you tried code that should have looked like this

b.th(:id => "categories_convertToSale").class

=> Watir::TableHeaderCell

It's telling you that the class of object returned by the .th method is a watir 'TableHeaderCell' (for more info, see the watir rdoc for the TableHeaderCell Object and/or the .th method )

HTML Class Attributes

The other flavor of 'class' is the HTML class attribute which is a standard attribute for nearly all element types in HTML. To get this you need to use watir's .attribute_value method along with the attribute you want to examine, to get attribute values of an element, or any object in watir like a TableHeaderCell that parallels HTML element types.

b.th(:id => "categories_totalCost").attribute_value("class")
like image 135
Chuck van der Linden Avatar answered Oct 11 '22 15:10

Chuck van der Linden