4

Can we able to handle the cookies in a browser at the time of execution?

Actually I need to get the cookies from the browser and need to set the modified cookies in between the scenario execution. Can we able to do it using Page Object?

I check the page object gem, we having the gem for clear the cookies but I need to get and set some cookies values. Any suggestions please....,

KAK
  • 905
  • 4
  • 14
  • 33

2 Answers2

2

You will need to interact with the watir-webdriver (or selenium-webdriver) browser directly to access the add/delete cookie API.

Assuming you are using watir-webdriver:

page.browser.cookies.clear
page.browser.cookies.add 'foo', 'bar'
page.browser.cookies.delete 'foo'

Note that page.browser is used to access the underlying watir-webdriver browser of the page object.

You can read more about the watir-webdriver cookie API:

If you are using selenium-webdriver without watir-webdriver, the API is documented in the Selenium::WebDriver::Options.

Justin Ko
  • 46,526
  • 5
  • 91
  • 101
  • Hi, Thanks for your suggestion. But I need to know, how to **interact the Selenium / Watir driver using Page Object** comments. I need the commend that used to get the current cookie of the browser and set / update the cookie value and run the browser. – KAK Jul 16 '13 at 13:14
  • This answer is using the page-object commands - `page` is the page-object. There is no direct method in the page-object gem to do this (though you could easily add it). This is why we call `browser` to access the underlying watir-webdriver. – Justin Ko Jul 16 '13 at 13:20
2

In any PageObject page (class), you can define a method that handles cookies, and access it using PageObject. There are several ways to implement PageObject, depending on what other frameworks you may be using, but here's an example using IRB.

# Using watir-webdriver

class MyPage 
  include PageObject

  def delete_cookies
    # Just wrapping this so it's convenient in my PageObject
    @browser.cookies.clear
  end

  def get_cookies_as_array
    # Returns an array of hashes for each cookie
    @browser.cookies.to_a
  end

  def set_browser_cookies( cookie_hash )
    # Store the cookie name and value
    @browser.cookies.add( cookie_hash[:name], cookie_hash[:value] )
  end

  def restore_browser_cookies( cookie_array )
    cookie_array.each do | cookie_hash_from_array |
      self.set_browser_cookies( cookie_hash_from_array )
    end
  end
end

IRB Example:

>> require "watir-webdriver"
>> require "page-object"
>> @browser = Watir::Browser.start "http://stackoverflow.com"
>> my_page = MyPage.new(@browser)
>> @cookies_to_keep = my_page.get_cookies_as_array
# Observe the cookies look like this:
# [0]
# ...
# [5] {
#        :name => "gauthed",
#       :value => "1",
#        :path => "/",
#      :domain => "stackoverflow.com",
#     :expires => nil,
#      :secure => false
# }
#
>> my_page.delete_cookies
"" # Empty string is returned
>> my_page.get_cookies_as_array
[] # Empty Array returned because there are no cookies
>> my_page.restore_browser_cookies( @cookies_to_keep )
# Cookie array is returned

The original cookies are restored with their original :name and :value.


The API Docs that Justin Ko pointed you to are a very valuable reference.

Sonja Leaf
  • 223
  • 1
  • 12