如何在水豚中断言 JQuery 表达式?
How to assert JQuery expression in Capybara?
我应该检查带有标签 article 且带有特定 class 的元素是 DOM 数组中的第一个元素。我写了 JQuery 表达式 正在工作:
$($('article')[0]).hasClass('styles__highlighted___2C5Yh')'
并在控制台中检查只有 0
个元素 returns true
。之后我创建了这样的步骤定义:
Then(/^Tag "(.*)" has the first element "(.*)"/) do |text, identifier|
page.execute_script("$($('#{text}')[0]).hasClass('#{identifier}')")
end
在我的测试中:
Then Tag "article" has the first element "styles__highlighted___2C5Yh"
它通过 0
和另一个数字(当我 运行 测试时),但是控制台中的 jQuery 表达式 returns 只有 0
.我想我应该以某种方式修改我的步骤定义文件,但不知道如何修改。
两个技术问题。
您想处理 JS 的结果,但您使用的是 execute_script
而不是 evaluate_script
你实际上并没有期待任何事情
如果我们解决这两个问题,我们会得到
Then(/^Tag "(.*)" has the first element "(.*)"/) do |text, identifier|
result = page.evaluate_script("$($('#{text}')[0]).hasClass('#{identifier}')")
expect(result).to be true # RSpec - will be different if using minitest
end
这将修复您的代码,但它确实不是测试您想要的内容的好方法,因为它可能导致动态页面上的不稳定测试(无需等待重试)
最佳解决方案取决于页面的确切结构。例如,如果所有 'article' 元素都是同级元素,那么您可以执行类似
的操作
Then(/^The first article should be highlighted$/) do
expect(page).to have_selector('article.styles__highlighted___2C5Yh:first-of-type')
end
一个不太理想的选择(虽然仍然比 jQuery 好)是
first_article = find('article', match: :first)
expect(first_article).to match_css('.styles__highlighted___2C5Yh')
谢谢。
修复了它以这种方式更改:
Then(/^Tag "(.*)" has the first element "(.*)"/) do |text, identifier|
result = page.evaluate_script("$($('#{text}')[0]).hasClass('#{identifier}')")
result.should be true
end
我应该检查带有标签 article 且带有特定 class 的元素是 DOM 数组中的第一个元素。我写了 JQuery 表达式 正在工作:
$($('article')[0]).hasClass('styles__highlighted___2C5Yh')'
并在控制台中检查只有 0
个元素 returns true
。之后我创建了这样的步骤定义:
Then(/^Tag "(.*)" has the first element "(.*)"/) do |text, identifier|
page.execute_script("$($('#{text}')[0]).hasClass('#{identifier}')")
end
在我的测试中:
Then Tag "article" has the first element "styles__highlighted___2C5Yh"
它通过 0
和另一个数字(当我 运行 测试时),但是控制台中的 jQuery 表达式 returns 只有 0
.我想我应该以某种方式修改我的步骤定义文件,但不知道如何修改。
两个技术问题。
您想处理 JS 的结果,但您使用的是
execute_script
而不是evaluate_script
你实际上并没有期待任何事情
如果我们解决这两个问题,我们会得到
Then(/^Tag "(.*)" has the first element "(.*)"/) do |text, identifier|
result = page.evaluate_script("$($('#{text}')[0]).hasClass('#{identifier}')")
expect(result).to be true # RSpec - will be different if using minitest
end
这将修复您的代码,但它确实不是测试您想要的内容的好方法,因为它可能导致动态页面上的不稳定测试(无需等待重试)
最佳解决方案取决于页面的确切结构。例如,如果所有 'article' 元素都是同级元素,那么您可以执行类似
的操作Then(/^The first article should be highlighted$/) do
expect(page).to have_selector('article.styles__highlighted___2C5Yh:first-of-type')
end
一个不太理想的选择(虽然仍然比 jQuery 好)是
first_article = find('article', match: :first)
expect(first_article).to match_css('.styles__highlighted___2C5Yh')
谢谢。 修复了它以这种方式更改:
Then(/^Tag "(.*)" has the first element "(.*)"/) do |text, identifier|
result = page.evaluate_script("$($('#{text}')[0]).hasClass('#{identifier}')")
result.should be true
end