Selenium-webdriverシリーズチュートリアル(13)--tableの処理方法

4476 ワード

Tableオブジェクトは自動化テストでよく処理するオブジェクトです.Webdriverには専用のtableクラスがないため、コードの簡略化を支援するために、拡張しやすいTableクラスを簡単にカプセル化する必要があります.
module EasyWrap
    class EasyWrapError < StandardError;end 
    class NotValidElementError < EasyWrapError;end  
    class IncorrectdIndexError < EasyWrapError;end  
 
    class TableBase
        attr_reader :e
        def initialize e
            raise NotValidElementError unless e.is_a?(Selenium::WebDriver::Element)
            @e = e 
        end
 
        def method_missing(m, *params, &blk)
            @e.send(m, *params) if @e.respond_to?(m)
        end
    end #TableBase
    class Table < TableBase
        def initialize e
            super(e)
            __rows
        end
        def __rows
            @rows = @e.find_elements(:css => 'tr')
        end
        private :__rows
 
        def rows
            all_rows = []   
            @rows.each { |r| rows << TableRow.new(r)}
            all_rows
        end
 
        def row_count
            @rows.size
        end
 
        def [](index)
            valid_index? index
            TableRow.new @rows[index]
        end 
 
        def valid_index?(index)
            raise IncorrectdIndexError if index.to_i > row_count
        end
    end #Table
 
    class TableRow < TableBase
        def initialize e
            super(e)
            __cells
        end
 
        def __cells 
            @cells = @e.find_elements(:tag_name => 'td') 
            #  td th
            @cells = @e.find_elements(:tag_name => 'th') if @cells.empty?
        end
        private :__cells
 
        def cells
            all_cells = []
            @cells.each {|c| all_cells << TableCell.new(c)} 
        end
 
        def cell_count
            @cells.size
        end
 
        def [](index)
            valid_index? index
            TableCell.new @cells[index]
        end
 
        def valid_index?(index)
            raise IncorrectdIndexError if index.to_i > cell_count
        end
    end #TableRow
 
    class TableCell < TableBase
    end #TableCell
end #EasyWrap

EasyWrapは3つのクラスを定義しています
  • Tableクラスは、1つのtableオブジェクトを表し、そのオブジェクトによってそのtableの行と列にアクセスすることができる.
  • Table Rowクラスはtableのある1行を表し、tableのある列にアクセスすることができる.
  • Table Cellクラスはtableの1つのセル
  • を表す.
    tableの3行目1列目をクリックする必要がある場合は、EasyWrapを通じて、このように実現することができます.
    talbe = EasyWrap::Table(dr.find_element(:id => 'table_id'))
    table[2][0].click
    

    次のhtmlコードを例に挙げます.
    <html>
        <head>
            <title>Table</title>
            <style>
                table {border: 1px solid #ccc}
            </style>
        </head>
        <body>
            <table id = "t">
                <th>C1</th><th>C2</th><th>C3</th>
                <tr>
                    <td>v1</td>
                    <td>v2</td>
                    <td>v3</td>
                </tr>
                <tr>
                    <td>k1</td>
                    <td>k2</td>
                    <td>k3</td>
                </tr>
            </table>
        </body>
    </html>
    

    次のスクリプトは、ハイライトページのtable要素の最初の行の最初の列の機能を実現します.
    require 'rubygems'
    require 'selenium-webdriver'
    require './easy_wrap'
    include EasyWrap
     
    dr = Selenium::WebDriver.for :firefox
    table_file = 'file:///'+File.expand_path(File.join(File.dirname(__FILE__), 'table.html'))
    dr.get table_file
    t = dr.find_element(:id => 't')
    table = Table.new t
    p table.row_count
    highlight = <<JS
        arguments[0].style.border = "2px solid red"
    JS
    dr.execute_script highlight, table[0][0].e
    #  2 2 
    puts table[1][1].text