我正在運行試圖使用Poltergeist gem設置cookie的問題,並且通過在Poltergeist gem中讀取已關閉的問題,看起來這是我的一個用戶錯誤問題,但我已經花了整整一天的時間嘗試正確設置cookie,而且我似乎無法正確設置。用Poltergeist PhantomJS Capybara驅動程序設置餅乾
注意:此測試在硒中正常工作,但由於運行時硒問題,我想使用poltergeist。
我正在使用的應用程序使用「_location」cookie來確定一些總體設置,並且我需要在應用程序中第一次訪問任何頁面時訪問cookie。否則,我會得到一個javascript錯誤,查找由於未設置位置而未設置的值。
The README for the Poltergeist gem顯示了餅乾
操縱餅乾
下面的方法來檢查和處理餅乾以下用法:
page.driver.cookies
- 當前頁面訪問餅乾的哈希值。密鑰是cookie名稱。值爲Cookie對象,具有以下方法:name
,value
, domain
, path
,secure?
,httponly?
,expires
。
page.driver.set_cookie(name, value, options = {})
- 設置一個cookie。選項散列可以採用以下鍵::domain
,:path
,:secure
,:httponly
,:expires
。 :expires
應該是一個Time對象。
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
- 視圖/管理/文/ new.html.haml
= link_to_function "test js", '$(this).html("js works")'
- 規格/requests/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,但似乎當我用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)>'
我也試圖在phantomjs_options通過阻止對吵鬧鬼司機,與外部的cookies文件。這實際上似乎讓我有點進一步,但我結束了一個空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
- cookie.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
謝謝,我們結束了不同的路線,但你的解釋看起來是正確的。 –