2012-01-16 26 views
1

我有點新的黃瓜,並已得到了掛在一個測試用例。黃瓜 - 如何寫一個測試用例來更改表中所列項目的排序順序?

頁面上有一張表列出了一堆產品,其中一個單元格包含了向下的圖形 - 這些是用戶點擊的控件,用於在產品目錄中上下移動該產品的排序順序最終用戶可以瀏覽哪些內容。

如何選擇表中列出的第二個產品,發現其「向上」或下行鏈路的ID &點擊?

這裏是(縮短了可讀性)表:

<table id="product_container"> 
<tr> 
    <th>Order Position</th> 
</tr> 
<tr> 
    <td><a href="#" class="product_up" id="product_sku_goes_here">Up</a> 
     <a href="#" class="product_down" id="product_sku_goes_here">Down</a> 
    </td> 
</tr> 
</table> 

感謝您的諮詢!

回答

1

HTML元素的id屬性需要在頁面上獨一無二的:http://www.w3.org/TR/html401/struct/global.html#h-7.5.2

選擇的產品將得到一個參考,以它的行最簡單的方法:

class ProductsTable 
    def initialize(driver) 
    @driver = driver 
    end 

    def table 
    @driver.find_element(:id, "product_container") 
    end 

    def products 
    table.find_elements(:tag_name, "td").map{|element| 
     Products.new(element) 
    } 
    end 
end 

class Products 
    def initialize(element) 
    @elem = element 
    end 

    def up 
    @elem.find_element(:class, "product_up") 
    end 

    def down 
    @elem.find_element(:class, "product_down") 
    end 
end 

driver = Selenium::WebDriver.for :chrome 
driver.get "http://link_to_testing_page" 
tabl = ProductsTable.new(driver) 

要推的第一款產品達:

tabl.products.first.up.click 

下來:

tabl.products.first.down.click 

黃瓜一步定義:

When /^I push product (\d+) (.*)$/ do |product, where| 
    product = product.to_i - 1 
    tabl.products[product].send(where).click 
end