Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e7307e619a
Fetching contributors…

Cannot retrieve contributors at this time

file 92 lines (71 sloc) 2.456 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
require File.expand_path("../spec_helper", __FILE__)

describe "Browser#cookies" do
  after { browser.cookies.clear }

  it 'gets an empty list of cookies' do
    browser.goto WatirSpec.url_for 'collections.html' # no cookie set.
    browser.cookies.to_a.should == []
  end

  it "gets any cookies set" do
    browser.goto set_cookie_url

    verify_cookies_count 1

    cookie = browser.cookies.to_a.first
    cookie[:name].should == 'monster'
    cookie[:value].should == '1'
  end

  not_compliant_on [:webdriver, :ie] do
    it 'adds a cookie' do
      browser.goto set_cookie_url
      verify_cookies_count 1

      browser.cookies.add 'foo', 'bar'
      verify_cookies_count 2
    end
  end

  not_compliant_on [:webdriver, :chrome], [:webdriver, :ie] do
    it 'adds a cookie with options' do
      browser.goto set_cookie_url

      expires = Time.now + 10000
      options = {:path => "/set_cookie",
                 :secure => true,
                 :expires => expires}

      deviates_on :watir_classic do
        # secure cookie can't be accessed running on WatirSpec test server
        options.delete(:secure)
      end

      browser.cookies.add 'a', 'b', options
      cookie = browser.cookies.to_a.find { |e| e[:name] == 'a' }
      cookie.should_not be_nil

      cookie[:name].should == 'a'
      cookie[:value].should == 'b'

      not_compliant_on :watir_classic do
        cookie[:path].should == "/set_cookie"
        cookie[:secure].should be_true

        cookie[:expires].should be_kind_of(Time)

        # a few ms slack
        cookie[:expires].to_i.should be_within(2).of(expires.to_i)
      end
    end
  end

  not_compliant_on [:webdriver, :ie] do
    it 'removes a cookie' do
      browser.goto set_cookie_url
      verify_cookies_count 1

      browser.cookies.delete 'monster'
      verify_cookies_count 0
    end

    it 'clears all cookies' do
      browser.goto set_cookie_url
      browser.cookies.add 'foo', 'bar'
      verify_cookies_count 2

      browser.cookies.clear
      verify_cookies_count 0
    end
  end

  def set_cookie_url
    # add timestamp to url to avoid caching in IE8
    WatirSpec.url_for('set_cookie/index.html', :needs_server => true) + "?t=#{Time.now.to_i + Time.now.usec}"
  end

  def verify_cookies_count expected_size
    cookies = browser.cookies.to_a
    cookies.size.should eq(expected_size), "expected #{expected_size} cookies, got #{cookies.size}: #{cookies.inspect}"
  end
end
Something went wrong with that request. Please try again.