Rspec /水豚 - 测试html lang

问题描述:

我有脚本动态定义html lang =“XX”在html代码的顶部。Rspec /水豚 - 测试html lang

例如,即使在危地马拉,我也有一些htl lang =“de”的页面,因为它们不取决于IP的国家,而取决于其他模型的数据。

反正,我怎么可以断言在使用RSpec和水豚的HTML的lang属性是“XX”的测试,这将是沿着线:

expect(find('html')).to have_css('[lang="es"]') 

实际的HTML页面上是

<html lang="XX" class="deal-page turbolinks-progress-bar" xmlns:fb="http://ogp.me/ns/fb#"> 

我得到这个错误:

expected to find css "[lang=\"es\"]" but there were no matches 

have_css匹配检查当前范围的后代(在您的示例中为html元素),它与给定的CSS匹配。您可以使用match_css匹配,检查当前范围元素是否给定的CSS

expect(find('html')).to match_css('[lang="es"]') 

或匹配(假设你没有其他使用了HTML元素)只是做

expect(page).to have_css('html[lang="es"]') 

这将是更好的性能。

+0

只需要知道,你可以访问'lang'属性并使用'match'来检查它,就像'expect(find('html')[:lang])。'匹配'('es')'? –

+1

@SebastiánPalma然而,如果页面动态变化,不会等待/重试匹配,那么最好不要在属性值上使用文本匹配器。从技术上讲,你也可以做'expect(find('html [lang =“es”]')),但'have_css'会更好。 –