forked from watir/watirspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookies_spec.rb
145 lines (114 loc) · 3.84 KB
/
cookies_spec.rb
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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.
expect(browser.cookies.to_a).to eq []
end
it "gets any cookies set" do
browser.goto set_cookie_url
verify_cookies_count 1
cookie = browser.cookies.to_a.first
expect(cookie[:name]).to eq 'monster'
expect(cookie[:value]).to eq '1'
end
describe '#[]' do
before do
browser.goto set_cookie_url
verify_cookies_count 1
end
it 'returns cookie by symbol name' do
cookie = browser.cookies[:monster]
expect(cookie[:name]).to eq('monster')
expect(cookie[:value]).to eq('1')
end
it 'returns cookie by string name' do
cookie = browser.cookies['monster']
expect(cookie[:name]).to eq('monster')
expect(cookie[:value]).to eq('1')
end
it 'returns nil if there is no cookie with such name' do
expect(browser.cookies[:non_monster]).to eq(nil)
end
end
not_compliant_on %i(webdriver internet_explorer) 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 %i(webdriver chrome), %i(webdriver internet_explorer), :phantomjs 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' }
expect(cookie).to_not be_nil
expect(cookie[:name]).to eq 'a'
expect(cookie[:value]).to eq 'b'
not_compliant_on :watir_classic do
expect(cookie[:path]).to eq "/set_cookie"
expect(cookie[:secure]).to be true
expect(cookie[:expires]).to be_kind_of(Time)
# a few ms slack
expect((cookie[:expires]).to_i).to be_within(2).of(expires.to_i)
end
end
end
not_compliant_on %i(webdriver internet_explorer) 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
not_compliant_on %i(webdriver internet_explorer), :phantomjs do
let(:file) { "#{Dir.tmpdir}/cookies" }
before do
browser.goto set_cookie_url
browser.cookies.save file
end
describe '#save' do
it 'saves cookies to file' do
expect(IO.read(file)).to eq(browser.cookies.to_a.to_yaml)
end
end
describe '#load' do
it 'loads cookies from file' do
browser.cookies.clear
browser.cookies.load file
expected = browser.cookies.to_a
actual = YAML.load(IO.read(file))
# https://code.google.com/p/selenium/issues/detail?id=6834
expected.each { |cookie| cookie.delete(:expires) }
actual.each { |cookie| cookie.delete(:expires) }
expect(actual).to eq(expected)
end
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
expect(cookies.size).to eq(expected_size), "expected #{expected_size} cookies, got #{cookies.size}: #{cookies.inspect}"
end
end