Rspec/capybara - 测试 html 语言

Rspec/capybara - test html lang

我有脚本在 html 代码的顶部动态定义 html lang="XX"。

例如,即使在危地马拉,我也有某些带有 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 匹配器检查与给定 CSS 匹配的当前范围(在您的示例中为 html 元素)的后代。您可以使用 match_css 匹配器来检查当前范围元素是否与给定的 CSS

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

或(假设 html 元素没有其他用途)只做

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

哪个性能更好。