【问题标题】:Capybara can not match xml pageCapybara 无法匹配 xml 页面
【发布时间】:2015-06-15 22:46:32
【问题描述】:
我在 capybara 的 xml 页面上匹配响应文本时遇到问题。
当我使用 page.should(have_content(arg1)) capybara 时会引发错误,即没有 \html 元素(不应该是 xml)。
当我使用page.should(have_xpath(arg1)) 时,它会引发Element at 40 no longer present in the DOM (Capybara::Webkit::NodeNotAttachedError)
测试xml的正确方法是什么?
【问题讨论】:
标签:
cucumber
capybara
capybara-webkit
【解决方案1】:
当使用 capybara-webkit 时,驱动程序会尝试使用浏览器的 HTML DOM 来寻找元素。这不起作用,因为您没有 HTML 文档。
一种解决方法是回退到 Capybara 的 string 实现:
xml = Capybara.string(page.body)
expect(xml).to have_xpath(arg1)
expect(xml).to have_content(arg1)
假设您的页面返回的内容类型为text/xml,capybara-webkit 根本不会弄乱响应正文,因此您可以将其传递给Capybara.string(如果您愿意,也可以直接传递给Nokogiri)。