水豚测试通过Selenium,但由于Poltergeist而失败

问题描述 投票:0回答:1

当使用Selenium / geckodriver和Poltergeist / phantomjs驱动程序运行时,在Capybara测试之间,我的行为有所不同。最终,我想知道为什么我会在一组测试中看到不同的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 driverpoltergeist时,测试失败。对于每个场景示例,对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 ObjectJavascript - How to detect if document has loaded (IE 7/Firefox 3)

我以为我是以正确的方式加载JavaScript的,但是由于[原因,似乎我不是really等待页面到达“完成”状态再尝试从页面中获取元素。 C0]:如果删除它,以便JavaScript must等待页面状态为“完成”,则Selenium测试将以Poltergeist测试相同的方式失败。

那么,如何通过Selenium驱动程序而不通过Poltergeist驱动程序?问题与驱动程序或JavaScript加载方式有关吗?还是其他?

javascript selenium-webdriver phantomjs capybara poltergeist
1个回答
0
投票

Poltergeist依赖于PhantomJS,今天的PhantomJS等同于5岁以上的Safari版本。它不支持现代CSS / JS,所以我猜您的JS和/或CSS中有一些它不支持的内容。如今,尝试使用poltergeist将是一种令人沮丧的练习。

© www.soinside.com 2019 - 2024. All rights reserved.