在Poltergeist中访问响应头值

时间:2015-07-23 06:58:01

标签: ruby selenium cucumber poltergeist

我正在编写一个简单的测试来确保我的网页上的“X-Frame-Options”值设置为Deny,我在黄瓜中使用Selenium Webdriver但是已经读过Selenium不支持这些头文件的检索/设置。我找到了一些小片段,其中poltergeist可以访问这些值并且在控制台中尝试时已经接近了

到目前为止我使用的是

@headers = page.driver.network_traffic[1].response_parts.uniq

在我的情况下返回

#<Capybara::Poltergeist::NetworkTraffic::Response:0x00000003a530b8
 @data=
  {"bodySize"=>4752,
   "contentType"=>"text/html",
   "headers"=>
    [{"name"=>"Date", "value"=>"Thu, 23 Jul 2015 06:23:49 GMT"},
     {"name"=>"Server", "value"=>"Apache/2.2.22 (Ubuntu)"},
     {"name"=>"X-Powered-By", "value"=>"PHP/5.3.10-1ubuntu3.18"},
     {"name"=>"Set-Cookie", "value"=> "PHPSESSID=sessionidhere; path=/; domain=mydomain; secure; HttpOnly"},
     {"name"=>"Expires", "value"=>"Thu, 19 Nov 1981 08:52:00 GMT"},
     {"name"=>"Cache-Control", "value"=> "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"},
     {"name"=>"Pragma", "value"=>"no-cache"},
     {"name"=>"X-Frame-Options", "value"=>"DENY"},
     {"name"=>"Vary", "value"=>"Accept-Encoding"},
     {"name"=>"Content-Encoding", "value"=>"gzip"},
     {"name"=>"Keep-Alive", "value"=>"timeout=5, max=100"},
     {"name"=>"Connection", "value"=>"Keep-Alive"},
     {"name"=>"Content-Type", "value"=>"text/html"}],
      "id"=>2,
      "redirectURL"=>nil,
      "stage"=>"start",
      "status"=>200,
      "statusText"=>"OK",
      "time"=>"2015-07-23T06:29:19.233Z",
      "url"=>"http://mysite-address.co.uk"}>

有没有办法可以拉出X帧选项并将其分配给我的实例变量?我无法浏览数组和键/值。

或者有更好的方法吗?

更新

我对此有了进一步的了解

所以我有

@headers = page.driver.network_traffic[1].response_parts.uniq
@headers[0].headers.find { |h| h['name'] == 'X-Frame-Options' }
# {"name"=>"X-Frame-Options", "value"=>"DENY"}

如果我再尝试

@headers[0].headers.find { |h| h['name'] == 'X-Frame-Options' }['value']
# "DENY"

所以我认为现在已经完成了,除非有人能发现我没有或有更简单的方法吗?

1 个答案:

答案 0 :(得分:2)

如果您有rspec期望,可以使用:

expect(response_headers['X-Frame-Options']).to include('Deny')