在使用Selenium / geckodriver和Poltergeist / phantomjs驱动程序运行时,水豚测试之间的行为有所不同。最终,我想知道为什么在一组测试中会看到不同的CSS结果以及如何实现
如果您觉得我没有很好地阐明这个问题,请原谅。该问题存在于我正在开发的当前项目中,以帮助我提高RSpec / Capybara / JavaScript技能。
简而言之,我有一个页面,允许在页面上提交3位或6位十六进制值。现在,将十六进制值(现在作为结果页面上的查询参数值)分配给CSS属性,以为结果页面上的元素的背景着色。 JavaScript负责将参数值传递到CSS属性:
if (document.readyState === "complete") { processForm(); }
window.onload = function processForm()
{
var url_parameters = location.search.substring(1).split("&");
var param_value = url_parameters[0].split("=");
colour_value = decodeURI(param_value[1]);
document.getElementById('html_custom_colour').innerHTML = colour_value.toString();
var html = document.getElementsByTagName('html')[0];
html.style.cssText = "--custom-colour: #" + colour_value;
}
我的Capybara测试配置为提交十六进制值,将其转换为RGB值,然后RSpec匹配器期望RGB值作为CSS属性,从而在结果页面上创建元素的背景色:
我在此配置中使用Selenium驱动程序:
Capybara.default_driver = :selenium
Capybara.javascript_driver = :poltergeist
使用Selenium驱动程序时,这些测试通过,期望值与结果页面上的值匹配。使用pp
,我可以输出从样式中获取的结果:
And I should see the text "The selected colour is #<colour>" # features/colours.feature:27
Examples:
| colour |
{"background-color"=>"rgb(102, 0, 255)"}
| 6600ff |
{"background-color"=>"rgb(255, 85, 0)"}
| f50 |
{"background-color"=>"rgb(255, 255, 255)"}
| ffffff |
3 scenarios (3 passed)
24 steps (24 passed)
0m3.881s
我已经为匹配器测试了不正确的值,以确保如果不查找结果页面所生成的值,测试可能会失败。
当我从Capybara.default driver
切换到poltergeist
时,测试失败。对于每个场景示例,对RGB值(现在是rgba
值)的期望都相同:rgba(0, 0, 0, 0)
,指示JavaScript尚未正确设置该值:
colour_value 6600ff
{"background-color"=>"rgba(0, 0, 0, 0)"}
| 6600ff |
expected to find text "rgb(102, 0, 255)" in "{\"background-color\"=>\"rgba(0, 0, 0, 0)\"}" (RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/colours.rb:47:in `/^I should see the colour "(.*?)"$/'
features/colours.feature:32:26:in `I should see the colour "6600ff"'
colour_value f50
{"background-color"=>"rgba(0, 0, 0, 0)"}
| f50 |
expected to find text "rgb(255, 85, 0)" in "{\"background-color\"=>\"rgba(0, 0, 0, 0)\"}" (RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/colours.rb:47:in `/^I should see the colour "(.*?)"$/'
features/colours.feature:33:26:in `I should see the colour "f50"'
colour_value ffffff
{"background-color"=>"rgba(0, 0, 0, 0)"}
| ffffff |
expected to find text "rgb(255, 255, 255)" in "{\"background-color\"=>\"rgba(0, 0, 0, 0)\"}" (RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/colours.rb:47:in `/^I should see the colour "(.*?)"$/'
features/colours.feature:34:26:in `I should see the colour "ffffff"'
有趣的是,如果我注释掉javascript_driver
,则对于Selenium驱动程序,测试在相同的结果上将失败。为什么现在要寻找rgba
而不是rgb
的值?为什么值总是rgba(0, 0, 0, 0)
?
我阅读了以下文章,怀疑这可能是从JavaScript页面中获取元素的方式:
Javascript Error Null is not an Object
Javascript - How to detect if document has loaded (IE 7/Firefox 3)
我以为我以正确的方式加载了JavaScript,但是看来我不是真的在等待页面达到“完成”状态,然后才试图从页面中获取元素。由于window.onload()
:
如果我删除它以使JavaScript 必须等待页面状态“完成”,那么Selenium测试将以Poltergeist测试相同的方式失败。
那么它如何通过Selenium驱动程序而不通过Poltergeist驱动程序传递? 问题与驱动程序或JavaScript加载方式有关吗?还是其他?
答案 0 :(得分:0)
Poltergeist依赖于PhantomJS,今天的PhantomJS等同于5岁以上的Safari版本。它不支持现代CSS / JS,所以我猜您的JS和/或CSS中有一些它不支持的内容。如今尝试使用poltergeist将会是一种令人沮丧的练习。