0

如何编写用于编辑项目的集成测试?我的“创建”测试如下所示:

  it "lets a user create a product" do
    login_user
    click_link("Products")
    click_link("New")
    fill_in "Identifier", :with => "MyString"
    click_button "Create"
    assert page.has_content?("Product was successfully created")
  end

这很好用。我感到困惑的是如何进行编辑和销毁测试。我的索引页面提供了所有产品的列表。所以首先我使用工厂来创建几​​个产品。现在我处于有多个“编辑”和“销毁”按钮的情况。我不能只说:

click_button "Destroy"

因为有两个。我如何告诉它点击哪一个?

如果我确实点击了正确的“销毁”按钮,如何在弹出的 Javascript 窗口中点击“确定”按钮?

4

1 回答 1

1

假设您使用的是 Webrat,您可以使用“内部”选择器。

Webrat "within" 方法将 CSS 选择器作为参数。假设您的“Destroy”按钮位于一个 id 为“#product-2”的 div 中,您可以使用以下命令隔离该按钮:

within "#product-2" do |scope|
  scope.click_button "Destroy"
end

如果您需要/宁愿使用 XPath,您可以执行以下操作:

 response.should have_xpath(xpath) do |button|
   click_button(button)
 end

或者,如果你使用 Capybara,那么你可以使用“find”方法:

find("#product-2").find("button").click
find(:xpath, "//div/div/button").click
于 2012-06-04T23:02:07.927 回答