【问题标题】:Check content of web page in cucumber test在黄瓜测试中检查网页内容
【发布时间】:2012-06-11 19:34:46
【问题描述】:

我开始探索 BDD 和 Cucumber。 所以我想检查我的页面是否显示我拥有的 2 篇文章的内容。 这是我正在使用检查内容是否存在的步骤:

Then /^I should see "([^"]*)"$/ do |desc|
 page.should have_content desc

 expected = Article.find_by_description(desc)
 page.should have_content(expected.navision_code)
 page.should have_content(expected.category.name)
end

通常我猜这应该可以解决问题,但是当我运行测试时,我收到了以下错误消息:

expected there to be content "---\n- tenetur\n" in "Alfa Paints Order System\n\n\n\nAlfa Paints\n\n\n\nSeth abernathy, admin\n\n\nVerander paswoord\n\nLogout\n\n\n\n\n\n\n\n\n\n\n\nAlfa Paints\nordering system\n\n\n\n\n\nOverzicht van alle artikelen\n\n\n\nBack\n\n\n\n\n\n\nProduct ID\nBeschrijving\nCategorie\n3001\nPaint\n---\n- tenetur\n\n3002\nBrush\n---\n- tenetur\n\n\n\n\n\n\n\n\n\n\n
(RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/admin_articles_steps.rb:41:in `/^I should see "([^"]*)"$/'
features/admin_articles.feature:10:in `Then I should see "Paint"'

如您所见,我期望绘制,错误消息显示内容内部有绘制。 但不知何故,黄瓜不认识它。

我是黄瓜新手,我可能遗漏了一些东西,但也许有人可以帮助我。

【问题讨论】:

  • 看起来 Capybara 不能很好地使用换行符。你可以尝试用这样的方式去除它们:praktikanten.brueckenschlaeger.org/2011/11/15/…
  • 我认为您不需要确定“整个”文章是否存在“成功”测试,也许检查文章中的随机行?

标签: ruby-on-rails selenium cucumber capybara bdd


【解决方案1】:

您同时测试三件事,因此很难判断哪个期望失败了。作为第一步,尝试注释掉除第一个期望之外的所有内容,以确保它在您认为的地方失败。

Then /^I should see "([^"]*)"$/ do |desc|
 page.should have_content desc

// expected = Article.find_by_description(desc)
// page.should have_content(expected.navision_code)
// page.should have_content(expected.category.name)
end

如果您仍然遇到同样的问题,请添加调试器,以便您可以试验并从测试环境中查看发生了什么。如果您打算进行大量黄瓜测试,这是一项值得培养的技能。

Then /^I should see "([^"]*)"$/ do |desc|
 debugger
 page.should have_content desc
end

从调试控制台,查看 desc 变量和页面的样子:

p desc
p page.content

【讨论】:

    猜你喜欢
    • 2013-12-11
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多