我在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的正确方法是什么?
使用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)。