Skip to content

Commit 5582b29

Browse files
author
heraclmene
committed
Add Cookie functionality and tests
1 parent 7625254 commit 5582b29

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

remote_driver_cookie.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ type AllCookiesResponse struct {
1111
Cookies []Cookie `json:"value"`
1212
}
1313

14+
// CookieResponse is the response returned from the Cookie method.
15+
type CookieResponse struct {
16+
State string `json:"state"`
17+
Cookie Cookie `json:"value"`
18+
}
19+
1420
// Cookie represents a browser cookie.
1521
type Cookie struct {
1622
Name string `json:"name"`
@@ -28,6 +34,7 @@ func (s *seleniumWebDriver) AllCookies() (*AllCookiesResponse, error) {
2834

2935
var response AllCookiesResponse
3036
var err error
37+
3138
url := fmt.Sprintf("%s/session/%s/cookie", s.seleniumURL, s.sessionID)
3239

3340
resp, err := s.apiService.performRequest(url, "GET", nil)
@@ -42,3 +49,26 @@ func (s *seleniumWebDriver) AllCookies() (*AllCookiesResponse, error) {
4249

4350
return &response, nil
4451
}
52+
53+
func (s *seleniumWebDriver) Cookie(name string) (*CookieResponse, error) {
54+
if len(s.sessionID) == 0 {
55+
return nil, newSessionIDError("Cookie")
56+
}
57+
58+
var response CookieResponse
59+
var err error
60+
61+
url := fmt.Sprintf("%s/session/%s/cookie/%s", s.seleniumURL, s.sessionID, name)
62+
63+
resp, err := s.apiService.performRequest(url, "GET", nil)
64+
if err != nil {
65+
return nil, newCommunicationError(err, "Cookie", url, nil)
66+
}
67+
68+
err = json.Unmarshal(resp, &response)
69+
if err != nil {
70+
return nil, newUnmarshallingError(err, "Cookie", string(resp))
71+
}
72+
73+
return &response, nil
74+
}

remote_driver_cookie_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"testing"
66
)
77

8+
/*
9+
AllCookies tests
10+
*/
811
func Test_CookieAllCookies_InvalidSessionIdResultsInError(t *testing.T) {
912
api := &testableAPIService{
1013
jsonToReturn: "",
@@ -78,3 +81,78 @@ func Test_CookieAllCookies_CorrectResponseIsReturned(t *testing.T) {
7881
t.Errorf(correctResponseErrorText)
7982
}
8083
}
84+
85+
/*
86+
Cookie tests
87+
*/
88+
func Test_CookieCookie_InvalidSessionIdResultsInError(t *testing.T) {
89+
api := &testableAPIService{
90+
jsonToReturn: "",
91+
errorToReturn: nil,
92+
}
93+
94+
d := setUpDriver(setUpDefaultCaps(), api)
95+
_, err := d.Cookie("test")
96+
if err == nil || !IsSessionIDError(err) {
97+
t.Errorf(sessionIDErrorText)
98+
}
99+
}
100+
101+
func Test_CookieCookie_CommunicationErrorIsReturnedCorrectly(t *testing.T) {
102+
api := &testableAPIService{
103+
jsonToReturn: "",
104+
errorToReturn: errors.New("An error :<"),
105+
}
106+
107+
d := setUpDriver(setUpDefaultCaps(), api)
108+
d.sessionID = "12345"
109+
110+
_, err := d.Cookie("test")
111+
if err == nil || !IsCommunicationError(err) {
112+
t.Errorf(apiCommunicationErrorText)
113+
}
114+
}
115+
116+
func Test_CookieCookie_UnmarshallingErrorIsReturnedCorrectly(t *testing.T) {
117+
api := &testableAPIService{
118+
jsonToReturn: "Invalid JSON!",
119+
errorToReturn: nil,
120+
}
121+
122+
d := setUpDriver(setUpDefaultCaps(), api)
123+
d.sessionID = "12345"
124+
125+
_, err := d.Cookie("test")
126+
if err == nil || !IsUnmarshallingError(err) {
127+
t.Errorf(unmarshallingErrorText)
128+
}
129+
}
130+
131+
func Test_CookieCookie_CorrectResponseIsReturned(t *testing.T) {
132+
api := &testableAPIService{
133+
jsonToReturn: `{
134+
"state": "success",
135+
"value": {
136+
"name": "Test Cookie",
137+
"value": "Test Value",
138+
"path": "/",
139+
"domain": "www.google.com",
140+
"secure": true,
141+
"httpOnly": true,
142+
"expiry": "2016-12-25T00:00:00Z"
143+
}
144+
}`,
145+
errorToReturn: nil,
146+
}
147+
148+
d := setUpDriver(setUpDefaultCaps(), api)
149+
d.sessionID = "12345"
150+
151+
resp, err := d.Cookie("test")
152+
if err != nil || resp.State != "success" || resp.Cookie.Name != "Test Cookie" ||
153+
resp.Cookie.Value != "Test Value" || resp.Cookie.Path != "/" ||
154+
resp.Cookie.Domain != "www.google.com" || !resp.Cookie.SecureOnly ||
155+
!resp.Cookie.HTTPOnly {
156+
t.Errorf(correctResponseErrorText)
157+
}
158+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package integrationtests
2+
3+
import "testing"
4+
5+
func Test_CookieCookie_CanRetrieveCookieFromWebPage(t *testing.T) {
6+
setUp()
7+
defer tearDown()
8+
9+
driver := createDriver(t)
10+
_, err := driver.CreateSession()
11+
if err != nil {
12+
errorAndWrap(t, "Error thrown whilst trying to create session.", err)
13+
}
14+
15+
_, err = driver.Go("https://news.ycombinator.com")
16+
if err != nil {
17+
errorAndWrap(t, "Error was thrown whilst navigating or result was not a success.", err)
18+
}
19+
20+
resp, err := driver.Cookie("__cfduid")
21+
if err != nil || resp.State != "success" || resp.Cookie.Name == "" {
22+
errorAndWrap(t, "Error was thrown whilst retrieving cookie", err)
23+
}
24+
25+
printObjectResult(resp)
26+
}

web_driver.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ type WebDriver interface {
194194
// AllCookies returns all cookies associated with the active URL of the
195195
// current browsing context.
196196
AllCookies() (*AllCookiesResponse, error)
197+
198+
// Cookie gets a single named cookie associated with the active URL of the
199+
// current browsing context.
200+
Cookie(name string) (*CookieResponse, error)
197201
}
198202

199203
// Element is an interface which specifies what all WebDriver elements

0 commit comments

Comments
 (0)