我想设置背景颜色并使用 Cucumber 来验证是否将正确的颜色设置为网页的背景颜色。
我有以下场景:
Then I follow "Use theme"
Then the page background should be blue
步骤定义为:
Then /^the page background should be blue$/ do
page.evaluate_script("%Q[jQuery('body').css('background-color');]").should == 'rgb(1, 31, 69)' end
但是 htis 抛出一个 javascript 错误 有人可以告诉我哪里出错了吗? (PS:我不是很擅长jQuery)
错误:
Then the page background should be blue syntax error (Selenium::WebDriver::Error::JavascriptError)
最佳答案
问题出在 "%Q[ ]"
部分。看起来您不小心试图在 Ruby 字符串中定义 Ruby 字符串。您要么想将字符串包装在 ""或 %Q[] 中,但不能同时包装。
以下任一方法都有效:
# Using %Q[]
puts page.evaluate_script(%Q[jQuery('body').css('background-color');])
# Using ""
puts page.evaluate_script("jQuery('body').css('background-color');")
# Using $ instead of jQuery
puts page.evaluate_script('$("body").css("background-color")')
关于jquery - cucumber 测试网页的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20211777/