用恶作剧PhantomJS Capybara Driver设置Cookies

时间:2012-11-12 01:06:39

标签: ruby-on-rails-3 cookies capybara phantomjs poltergeist

我正在尝试使用Poltergeist gem设置cookie的问题,并且通过阅读Poltergeist gem中的已关闭问题,看来这是我的用户错误问题,但我花了一整天现在试图正确设置cookie,我似乎无法正确设置。

注意:此测试在硒中效果很好,但由于硒的运行时间问题,我想使用poltergeist。

我正在处理的应用程序是使用“_location”cookie来确定一些整体设置,我需要在首次访问应用程序中的任何页面时访问cookie。否则,我会收到一个javascript错误,因为没有设置位置而查找未设置的值。

The README for the Poltergeist gem显示了Cookie的以下用法

操纵cookie

以下方法用于检查和操作cookie:

page.driver.cookies - 当前页面可访问的Cookie哈希。密钥是cookie名称。值为Cookie对象,具有以下方法:namevaluedomainpathsecure?httponly?,{{1 }}

expires - 设置一个Cookie。选项哈希可以使用以下键:page.driver.set_cookie(name, value, options = {}):domain:path:secure:httponly:expires应该是Time对象。

:expires - 删除Cookie

- specs / spec_helper.rb

page.driver.remove_cookie(name)

- views / admin / article / new.html.haml

Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app, :debug => true)
end

Capybara.javascript_driver = :poltergeist

- specs / requests / article_products_spec.rb

= link_to_function "test js", '$(this).html("js works")'

测试回复

require 'spec_helper'

describe "Associating Articles with Products and Product Categories" do

  before (:all) do
    @user = create(:user, role: "admin")
    5.times { create(:product_category) }
    @product_categories = ProductCategory.all
    @product_categories.each do |pc|
      4.times { create(:product, product_category: pc) }
    end
  end

  before (:each) do
    visit new_user_session_path
    page.driver.set_cookie("_location", "US")
    page.driver.cookies
    fill_in "Username Or Email", with: @user.email
    fill_in "Password", with: @user.password
    click_button "Sign In"
    visit new_admin_article_path
  end

  it "supports js", :js => true do
    click_link "test js"
    page.should have_content("js works")
  end

end

由于这次失败,我想也许我应该在访问页面之前设置cookie,但是当我在我的Capybara脚本中使用“visit”之前我使用page.driver.set_cookie命令时,它似乎没有实际上设置了什么从这个修改过的代码块和这个结果集中可以看出。

- specs / requests / article_products_spec.rb

{"name"=>"visit", "args"=>["http://127.0.0.1:64554/users/sign_in"]}
{"error"=>{"name"=>"Poltergeist.JavascriptError", "args"=>[[{"message"=>"TypeError: 'null' is not an object (evaluating 'response.custentitydefault_shipping_location')", "stack"=>"TypeError: 'null' is not an object (evaluating 'response.custentitydefault_shipping_location')\n    at http://127.0.0.1:64554/assets/non_deferred.js:12338\n    at http://127.0.0.1:64554/assets/non_deferred.js:1076\n    at http://127.0.0.1:64554/assets/non_deferred.js:1194\n    at http://127.0.0.1:64554/assets/non_deferred.js:7539 in done\n    at http://127.0.0.1:64554/assets/non_deferred.js:8325"}]]}}
{"name"=>"reset", "args"=>[]}
{"response"=>true}
F

Failures:

  1) Associating Articles with Products and Product Categories supports js
     Failure/Error: visit new_user_session_path
     Capybara::Poltergeist::JavascriptError:
       One or more errors were raised in the Javascript code on the page:

       TypeError: 'null' is not an object (evaluating 'response.custentitydefault_shipping_location')
           at http://127.0.0.1:64554/assets/non_deferred.js:12338
           at http://127.0.0.1:64554/assets/non_deferred.js:1076
           at http://127.0.0.1:64554/assets/non_deferred.js:1194
           at http://127.0.0.1:64554/assets/non_deferred.js:7539 in done
           at http://127.0.0.1:64554/assets/non_deferred.js:8325
     # ./spec/requests/article_products_spec.rb:15:in `block (2 levels) in <top (required)>'
     # ./spec/support/vcr.rb:14:in `block (2 levels) in <top (required)>'

测试回复

describe "Associating Articles with Products and Product Categories" do

  before (:all) do
    @user = create(:user, role: "admin")
    5.times { create(:product_category) }
    @product_categories = ProductCategory.all
    @product_categories.each do |pc|
      4.times { create(:product, product_category: pc) }
    end
  end

  before (:each) do
    page.driver.set_cookie("_location", "US")
    page.driver.cookies
    visit new_user_session_path
    fill_in "Username Or Email", with: @user.email
    fill_in "Password", with: @user.password
    click_button "Sign In"
    visit new_admin_article_path
  end

  it "supports js", :js => true do
    click_link "test js"
    page.should have_content("js works")
  end
end

我还尝试使用外部cookie文件将phantomjs_options块传递给poltergeist驱动程序。这实际上似乎让我有点远,但我最终得到了一个null cookie_jar。您可以在下面看到此尝试:

- 规格/ spec_helper.rb

{"name"=>"set_cookie", "args"=>[{:name=>"_location", :value=>"US", :domain=>"127.0.0.1"}]}
{"response"=>true}
{"name"=>"cookies", "args"=>[]}
{"response"=>[]}
{"name"=>"visit", "args"=>["http://127.0.0.1:64090/users/sign_in"]}
{"error"=>{"name"=>"Poltergeist.JavascriptError", "args"=>[[{"message"=>"TypeError: 'null' is not an object (evaluating 'response.custentitydefault_shipping_location')", "stack"=>"TypeError: 'null' is not an object (evaluating 'response.custentitydefault_shipping_location')\n    at http://127.0.0.1:64090/assets/non_deferred.js:12338\n    at http://127.0.0.1:64090/assets/non_deferred.js:1076\n    at http://127.0.0.1:64090/assets/non_deferred.js:1194\n    at http://127.0.0.1:64090/assets/non_deferred.js:7539 in done\n    at http://127.0.0.1:64090/assets/non_deferred.js:8325"}]]}}
{"name"=>"reset", "args"=>[]}
{"response"=>true}
F

Failures:

  1) Associating Articles with Products and Product Categories supports js
     Failure/Error: visit new_user_session_path
     Capybara::Poltergeist::JavascriptError:
       One or more errors were raised in the Javascript code on the page:

       TypeError: 'null' is not an object (evaluating 'response.custentitydefault_shipping_location')
           at http://127.0.0.1:64554/assets/non_deferred.js:12338
           at http://127.0.0.1:64554/assets/non_deferred.js:1076
           at http://127.0.0.1:64554/assets/non_deferred.js:1194
           at http://127.0.0.1:64554/assets/non_deferred.js:7539 in done
           at http://127.0.0.1:64554/assets/non_deferred.js:8325
     # ./spec/requests/article_products_spec.rb:15:in `block (2 levels) in <top (required)>'
     # ./spec/support/vcr.rb:14:in `block (2 levels) in <top (required)>'

- cookies.txt

  Capybara.register_driver :poltergeist do |app|
    Capybara::Poltergeist::Driver.new(app, [phantomjs_options: [--cookies-file="#{::Rails.root}/spec/requests/cookies.txt"]] )
  end

  Capybara.javascript_driver = :poltergeist

测试响应     ˚F

phantom.addCookie({
    'name':     '_location',
    'value':    'US',
    'domain':   'localhost',
    'expires':  (new Date()).getTime() + 3600
});

1 个答案:

答案 0 :(得分:3)

我相信你为Poltergeist指定选项的尝试与PhantomJS的选项混淆了。您的注册区块应如下所示:

Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app, :phantomjs_options => ["--cookies-file=#{::Rails.root}/spec/requests/cookies.txt"])
end

希望有所帮助。