Undefined method 'set_cookie' for capybara How can I create a cookie?












1















I am running against a public site



When I try set_cookie I get:



undefined method 'set_cookie' for #<Selenium::WebDriver::Driver:0x531e792e4b07692c browser=:chrome>

require 'rspec'
require 'capybara'
require 'capybara/rspec'
require 'capybara/dsl'
require 'pry'

Capybara.run_server = false
Capybara.app_host = 'http://www.google.com/'

Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

root='/'

describe 'Form flow works', :type => :feature do

before :each do
Capybara.current_driver = :chrome
end

it 'Collect the customer data', :happy do
visit( root )
page.driver.browser.set_cookie('test_disabled', 'true', :domain => 'www.google.com')




None of the current answers on SO address my issue or have worked.

Actual url is not google.



I tried show_me_cookies but that didn't work - details:

Added the gem:
$ gem install show_me_the_cookies
Successfully installed show_me_the_cookies-4.0.0
Parsing documentation for show_me_the_cookies-4.0.0
Done installing documentation for show_me_the_cookies after 0 seconds
1 gem installed



I added the following code to the spec (just using 1 file with all code right now)



RSpec.configure do |c| 
c.treat_symbols_as_metadata_keys_with_true_values = true
c.include ShowMeTheCookies, :type => :feature
end


and then i added show_me_the_cookies in my spec but all I got was ...spec/foo_spec.rb:17:inblock in ': uninitialized constant ShowMeTheCookies (NameError)`










share|improve this question




















  • 1





    try this actually: github.com/nruth/show_me_the_cookies

    – BKSpurgeon
    Nov 22 '18 at 17:37













  • Thanks@BKSpurgeon Unfortunately it didn't work

    – Michael Durrant
    Nov 22 '18 at 18:02
















1















I am running against a public site



When I try set_cookie I get:



undefined method 'set_cookie' for #<Selenium::WebDriver::Driver:0x531e792e4b07692c browser=:chrome>

require 'rspec'
require 'capybara'
require 'capybara/rspec'
require 'capybara/dsl'
require 'pry'

Capybara.run_server = false
Capybara.app_host = 'http://www.google.com/'

Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

root='/'

describe 'Form flow works', :type => :feature do

before :each do
Capybara.current_driver = :chrome
end

it 'Collect the customer data', :happy do
visit( root )
page.driver.browser.set_cookie('test_disabled', 'true', :domain => 'www.google.com')




None of the current answers on SO address my issue or have worked.

Actual url is not google.



I tried show_me_cookies but that didn't work - details:

Added the gem:
$ gem install show_me_the_cookies
Successfully installed show_me_the_cookies-4.0.0
Parsing documentation for show_me_the_cookies-4.0.0
Done installing documentation for show_me_the_cookies after 0 seconds
1 gem installed



I added the following code to the spec (just using 1 file with all code right now)



RSpec.configure do |c| 
c.treat_symbols_as_metadata_keys_with_true_values = true
c.include ShowMeTheCookies, :type => :feature
end


and then i added show_me_the_cookies in my spec but all I got was ...spec/foo_spec.rb:17:inblock in ': uninitialized constant ShowMeTheCookies (NameError)`










share|improve this question




















  • 1





    try this actually: github.com/nruth/show_me_the_cookies

    – BKSpurgeon
    Nov 22 '18 at 17:37













  • Thanks@BKSpurgeon Unfortunately it didn't work

    – Michael Durrant
    Nov 22 '18 at 18:02














1












1








1








I am running against a public site



When I try set_cookie I get:



undefined method 'set_cookie' for #<Selenium::WebDriver::Driver:0x531e792e4b07692c browser=:chrome>

require 'rspec'
require 'capybara'
require 'capybara/rspec'
require 'capybara/dsl'
require 'pry'

Capybara.run_server = false
Capybara.app_host = 'http://www.google.com/'

Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

root='/'

describe 'Form flow works', :type => :feature do

before :each do
Capybara.current_driver = :chrome
end

it 'Collect the customer data', :happy do
visit( root )
page.driver.browser.set_cookie('test_disabled', 'true', :domain => 'www.google.com')




None of the current answers on SO address my issue or have worked.

Actual url is not google.



I tried show_me_cookies but that didn't work - details:

Added the gem:
$ gem install show_me_the_cookies
Successfully installed show_me_the_cookies-4.0.0
Parsing documentation for show_me_the_cookies-4.0.0
Done installing documentation for show_me_the_cookies after 0 seconds
1 gem installed



I added the following code to the spec (just using 1 file with all code right now)



RSpec.configure do |c| 
c.treat_symbols_as_metadata_keys_with_true_values = true
c.include ShowMeTheCookies, :type => :feature
end


and then i added show_me_the_cookies in my spec but all I got was ...spec/foo_spec.rb:17:inblock in ': uninitialized constant ShowMeTheCookies (NameError)`










share|improve this question
















I am running against a public site



When I try set_cookie I get:



undefined method 'set_cookie' for #<Selenium::WebDriver::Driver:0x531e792e4b07692c browser=:chrome>

require 'rspec'
require 'capybara'
require 'capybara/rspec'
require 'capybara/dsl'
require 'pry'

Capybara.run_server = false
Capybara.app_host = 'http://www.google.com/'

Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

root='/'

describe 'Form flow works', :type => :feature do

before :each do
Capybara.current_driver = :chrome
end

it 'Collect the customer data', :happy do
visit( root )
page.driver.browser.set_cookie('test_disabled', 'true', :domain => 'www.google.com')




None of the current answers on SO address my issue or have worked.

Actual url is not google.



I tried show_me_cookies but that didn't work - details:

Added the gem:
$ gem install show_me_the_cookies
Successfully installed show_me_the_cookies-4.0.0
Parsing documentation for show_me_the_cookies-4.0.0
Done installing documentation for show_me_the_cookies after 0 seconds
1 gem installed



I added the following code to the spec (just using 1 file with all code right now)



RSpec.configure do |c| 
c.treat_symbols_as_metadata_keys_with_true_values = true
c.include ShowMeTheCookies, :type => :feature
end


and then i added show_me_the_cookies in my spec but all I got was ...spec/foo_spec.rb:17:inblock in ': uninitialized constant ShowMeTheCookies (NameError)`







ruby selenium rspec capybara






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 18:01







Michael Durrant

















asked Nov 22 '18 at 17:24









Michael DurrantMichael Durrant

58.8k66240366




58.8k66240366








  • 1





    try this actually: github.com/nruth/show_me_the_cookies

    – BKSpurgeon
    Nov 22 '18 at 17:37













  • Thanks@BKSpurgeon Unfortunately it didn't work

    – Michael Durrant
    Nov 22 '18 at 18:02














  • 1





    try this actually: github.com/nruth/show_me_the_cookies

    – BKSpurgeon
    Nov 22 '18 at 17:37













  • Thanks@BKSpurgeon Unfortunately it didn't work

    – Michael Durrant
    Nov 22 '18 at 18:02








1




1





try this actually: github.com/nruth/show_me_the_cookies

– BKSpurgeon
Nov 22 '18 at 17:37







try this actually: github.com/nruth/show_me_the_cookies

– BKSpurgeon
Nov 22 '18 at 17:37















Thanks@BKSpurgeon Unfortunately it didn't work

– Michael Durrant
Nov 22 '18 at 18:02





Thanks@BKSpurgeon Unfortunately it didn't work

– Michael Durrant
Nov 22 '18 at 18:02












2 Answers
2






active

oldest

votes


















0














This solved it:



After visit to create the session I used:



browser = Capybara.current_session.driver.browser
browser.manage.add_cookie :name => 'ab', :value => 'true', :expires => Time.now + 3600





share|improve this answer































    0














    Capybara doesn't provide a cookie API because it's main aim is testing apps, and setting cookies when directly when testing is generally not a good idea. That being said, it sounds like you're scraping the web rather than testing so you have 2 options.





    1. Access the driver specific cookie methods. Since you're using the Selenium driver that would be something like



      page.driver.browser.manage.add_cookie(name: cookie_name, value: cookie_value)



    2. Use a gem that provides a common cookie API across different drivers. That would be the show_me_the_cookies gem recommended in the comments. That would then be



      create_cookie(cookie_name, cookie_value)



    You may be getting uninitialized constant because you need to require 'show_me_the_cookies' in your spec_helper/rails_heper






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435811%2fundefined-method-set-cookie-for-capybara-how-can-i-create-a-cookie%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      This solved it:



      After visit to create the session I used:



      browser = Capybara.current_session.driver.browser
      browser.manage.add_cookie :name => 'ab', :value => 'true', :expires => Time.now + 3600





      share|improve this answer




























        0














        This solved it:



        After visit to create the session I used:



        browser = Capybara.current_session.driver.browser
        browser.manage.add_cookie :name => 'ab', :value => 'true', :expires => Time.now + 3600





        share|improve this answer


























          0












          0








          0







          This solved it:



          After visit to create the session I used:



          browser = Capybara.current_session.driver.browser
          browser.manage.add_cookie :name => 'ab', :value => 'true', :expires => Time.now + 3600





          share|improve this answer













          This solved it:



          After visit to create the session I used:



          browser = Capybara.current_session.driver.browser
          browser.manage.add_cookie :name => 'ab', :value => 'true', :expires => Time.now + 3600






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 18:27









          Michael DurrantMichael Durrant

          58.8k66240366




          58.8k66240366

























              0














              Capybara doesn't provide a cookie API because it's main aim is testing apps, and setting cookies when directly when testing is generally not a good idea. That being said, it sounds like you're scraping the web rather than testing so you have 2 options.





              1. Access the driver specific cookie methods. Since you're using the Selenium driver that would be something like



                page.driver.browser.manage.add_cookie(name: cookie_name, value: cookie_value)



              2. Use a gem that provides a common cookie API across different drivers. That would be the show_me_the_cookies gem recommended in the comments. That would then be



                create_cookie(cookie_name, cookie_value)



              You may be getting uninitialized constant because you need to require 'show_me_the_cookies' in your spec_helper/rails_heper






              share|improve this answer




























                0














                Capybara doesn't provide a cookie API because it's main aim is testing apps, and setting cookies when directly when testing is generally not a good idea. That being said, it sounds like you're scraping the web rather than testing so you have 2 options.





                1. Access the driver specific cookie methods. Since you're using the Selenium driver that would be something like



                  page.driver.browser.manage.add_cookie(name: cookie_name, value: cookie_value)



                2. Use a gem that provides a common cookie API across different drivers. That would be the show_me_the_cookies gem recommended in the comments. That would then be



                  create_cookie(cookie_name, cookie_value)



                You may be getting uninitialized constant because you need to require 'show_me_the_cookies' in your spec_helper/rails_heper






                share|improve this answer


























                  0












                  0








                  0







                  Capybara doesn't provide a cookie API because it's main aim is testing apps, and setting cookies when directly when testing is generally not a good idea. That being said, it sounds like you're scraping the web rather than testing so you have 2 options.





                  1. Access the driver specific cookie methods. Since you're using the Selenium driver that would be something like



                    page.driver.browser.manage.add_cookie(name: cookie_name, value: cookie_value)



                  2. Use a gem that provides a common cookie API across different drivers. That would be the show_me_the_cookies gem recommended in the comments. That would then be



                    create_cookie(cookie_name, cookie_value)



                  You may be getting uninitialized constant because you need to require 'show_me_the_cookies' in your spec_helper/rails_heper






                  share|improve this answer













                  Capybara doesn't provide a cookie API because it's main aim is testing apps, and setting cookies when directly when testing is generally not a good idea. That being said, it sounds like you're scraping the web rather than testing so you have 2 options.





                  1. Access the driver specific cookie methods. Since you're using the Selenium driver that would be something like



                    page.driver.browser.manage.add_cookie(name: cookie_name, value: cookie_value)



                  2. Use a gem that provides a common cookie API across different drivers. That would be the show_me_the_cookies gem recommended in the comments. That would then be



                    create_cookie(cookie_name, cookie_value)



                  You may be getting uninitialized constant because you need to require 'show_me_the_cookies' in your spec_helper/rails_heper







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 18:30









                  Thomas WalpoleThomas Walpole

                  30.6k32747




                  30.6k32747






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435811%2fundefined-method-set-cookie-for-capybara-how-can-i-create-a-cookie%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      Refactoring coordinates for Minecraft Pi buildings written in Python