我在尝试使用 Poltergeist gem 设置 cookie 时遇到了问题,并且通过阅读 Poltergeist gem 上的已关闭问题,这似乎是我的用户错误问题,但我现在已经花了一整天的时间来尝试正确设置cookie,我似乎无法正确设置。
注意:此测试在 selenium 中运行良好,但由于 selenium 的运行时间问题,我想使用 poltergeist。
我正在处理的应用程序正在使用“_location”cookie 来确定一些整体设置,并且我需要在我第一次访问应用程序中的任何页面时访问该 cookie。否则,我会收到一个 javascript 错误,以查找由于未设置位置而未设置的值。
Poltergeist gem 的自述文件显示了 cookie 的以下用法
操作 cookie
以下方法用于检查和操作 cookie:
page.driver.cookies
- 当前页面可访问的 cookie 哈希。键是 cookie 名称。这些值是 Cookie 对象,具有以下方法:name
, value
, domain
, path
, secure?
, httponly?
, expires
.
page.driver.set_cookie(name, value, options = {})
- 设置一个cookie。选项哈希可以采用以下键::domain
, :path
, :secure
, :httponly
, :expires
. :expires
应该是一个时间对象。
page.driver.remove_cookie(name)
- 删除 cookie
-- 规格/spec_helper.rb
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, :debug => true)
end
Capybara.javascript_driver = :poltergeist
--views/admin/article/new.html.haml
= link_to_function "test js", '$(this).html("js works")'
-- 规格/请求/article_products_spec.rb
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
测试响应
{"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)>'
由于这个失败,我想也许我应该在访问页面之前设置 cookie,但是当我在 Capybara 脚本中使用“访问”之前使用 page.driver.set_cookie 命令时,它实际上并没有设置任何东西. 从这个修改后的代码块和这个结果集可以看出。
-- 规格/请求/article_products_spec.rb
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
测试响应
{"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)>'
我还尝试使用外部 cookie 文件将 phantomjs_options 块传递给 poltergeist 驱动程序。这实际上似乎让我走得更远,但我最终得到了一个空 cookie_jar。您可以在下面看到此尝试:
--spec/spec_helper.rb
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
--cookies.txt
phantom.addCookie({
'name': '_location',
'value': 'US',
'domain': 'localhost',
'expires': (new Date()).getTime() + 3600
});
测试响应 F
Failures:
1) Associating Articles with Products and Product Categories supports js
Failure/Error: visit new_user_session_path
NoMethodError:
undefined method `cookie_jar' for nil:NilClass
# ./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)>'
Finished in 2.42 seconds
1 example, 1 failure