howitzer icon indicating copy to clipboard operation
howitzer copied to clipboard

How to set cookies on a page?

Open gmox opened this issue 2 years ago • 1 comments

Hello,

I am using Howitzer to write automated tests (duh), and part of the feature set I am testing against is hidden behind a cookie flag that enables or disables the feature based on the values of that cookie.

I believe I can set cookies on the base Capybara/Selenium driver, but that requires a lot of calls to underlying classes. And even if I were to do that, I'm still unsure of the most appropriate place to set cookies: in the page object? somewhere more global?

Insight would be appreciated!

Thank you!

gmox avatar Oct 01 '22 15:10 gmox

Hey @gmox

Thank you for creating this issue.

We have several options on how to achieve that, but I believe it should be implemented via Capybara/Selenium driver, like Capybara.current_session.driver.browser.set_cookie(cookie_string)

Option 1 (global level):

# config/capybara.rb

module CapybaraHelpers
  extend Howitzer::CapybaraHelpers
  
  def self.cookie=(value)
      Capybara.current_session.driver.browser.set_cookie(cookie_string)
  end
end

# then at any part of the framework

CapybaraHelpers.cookie = "xxx"

Option 2 (WebPage level)

# web/pages/your_app_page.rb

class YourAppPage < Howitzer::Web::Page

  def self.cookie=(value)
      Capybara.current_session.driver.browser.set_cookie(cookie_string)
  end

end

class HomePage < YourAppPage
end

# Then in step definitions:

HomePage.cookie = "xxx"

We will add a possibility to add such methods in a more simple way in future releases.

romikoops avatar Oct 05 '22 18:10 romikoops