Skip to content

Commit 9010172

Browse files
author
heraclmene
committed
Add AllCookies functionality and tests.
1 parent 3ac422d commit 9010172

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

remote_driver_cookie.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package goselenium
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
// AllCookiesResponse is the response returned from the AllCookies method.
9+
type AllCookiesResponse struct {
10+
State string `json:"state"`
11+
Cookies []Cookie `json:"value"`
12+
}
13+
14+
// Cookie represents a browser cookie.
15+
type Cookie struct {
16+
Name string `json:"name"`
17+
Value string `json:"value"`
18+
Path string `json:"path"`
19+
Domain string `json:"domain"`
20+
SecureOnly bool `json:"secure"`
21+
HTTPOnly bool `json:"httpOnly"`
22+
}
23+
24+
func (s *seleniumWebDriver) AllCookies() (*AllCookiesResponse, error) {
25+
if len(s.sessionID) == 0 {
26+
return nil, newSessionIDError("AllCookies")
27+
}
28+
29+
var response AllCookiesResponse
30+
var err error
31+
url := fmt.Sprintf("%s/session/%s/cookie", s.seleniumURL, s.sessionID)
32+
33+
resp, err := s.apiService.performRequest(url, "GET", nil)
34+
if err != nil {
35+
return nil, newCommunicationError(err, "AllCookies", url, nil)
36+
}
37+
38+
err = json.Unmarshal(resp, &response)
39+
if err != nil {
40+
return nil, newUnmarshallingError(err, "AllCookies", string(resp))
41+
}
42+
43+
return &response, nil
44+
}

remote_driver_cookie_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package goselenium
2+
3+
import (
4+
"errors"
5+
"testing"
6+
)
7+
8+
func Test_CookieAllCookies_InvalidSessionIdResultsInError(t *testing.T) {
9+
api := &testableAPIService{
10+
jsonToReturn: "",
11+
errorToReturn: nil,
12+
}
13+
14+
d := setUpDriver(setUpDefaultCaps(), api)
15+
_, err := d.AllCookies()
16+
if err == nil || !IsSessionIDError(err) {
17+
t.Errorf(sessionIDErrorText)
18+
}
19+
}
20+
21+
func Test_CookieAllCookies_CommunicationErrorIsReturnedCorrectly(t *testing.T) {
22+
api := &testableAPIService{
23+
jsonToReturn: "",
24+
errorToReturn: errors.New("An error :<"),
25+
}
26+
27+
d := setUpDriver(setUpDefaultCaps(), api)
28+
d.sessionID = "12345"
29+
30+
_, err := d.AllCookies()
31+
if err == nil || !IsCommunicationError(err) {
32+
t.Errorf(apiCommunicationErrorText)
33+
}
34+
}
35+
36+
func Test_CookieAllCookies_UnmarshallingErrorIsReturnedCorrectly(t *testing.T) {
37+
api := &testableAPIService{
38+
jsonToReturn: "Invalid JSON!",
39+
errorToReturn: nil,
40+
}
41+
42+
d := setUpDriver(setUpDefaultCaps(), api)
43+
d.sessionID = "12345"
44+
45+
_, err := d.AllCookies()
46+
if err == nil || !IsUnmarshallingError(err) {
47+
t.Errorf(unmarshallingErrorText)
48+
}
49+
}
50+
51+
func Test_CookieAllCookies_CorrectResponseIsReturned(t *testing.T) {
52+
api := &testableAPIService{
53+
jsonToReturn: `{
54+
"state": "success",
55+
"value": [
56+
{
57+
"name": "Test Cookie",
58+
"value": "Test Value",
59+
"path": "/",
60+
"domain": "www.google.com",
61+
"secure": true,
62+
"httpOnly": true,
63+
"expiry": "2016-12-25T00:00:00Z"
64+
}
65+
]
66+
}`,
67+
errorToReturn: nil,
68+
}
69+
70+
d := setUpDriver(setUpDefaultCaps(), api)
71+
d.sessionID = "12345"
72+
73+
resp, err := d.AllCookies()
74+
if err != nil || resp.State != "success" || resp.Cookies[0].Name != "Test Cookie" ||
75+
resp.Cookies[0].Value != "Test Value" || resp.Cookies[0].Path != "/" ||
76+
resp.Cookies[0].Domain != "www.google.com" || !resp.Cookies[0].SecureOnly ||
77+
!resp.Cookies[0].HTTPOnly {
78+
t.Errorf(correctResponseErrorText)
79+
}
80+
}
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_CookieAllCookies_CanRetrieveAllCookiesFromWebPage(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 creating 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.AllCookies()
21+
if err != nil || resp.State != "success" || resp.Cookies[0].Name == "" {
22+
errorAndWrap(t, "Error was thrown whilst retrieving cookies", err)
23+
}
24+
25+
printObjectResult(resp)
26+
}

web_driver.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,14 @@ type WebDriver interface {
186186
// doLongWindedTask();
187187
// callback();
188188
ExecuteScriptAsync(script string) (*ExecuteScriptResponse, error)
189+
190+
/*
191+
COOKIE METHODS
192+
*/
193+
194+
// AllCookies returns all cookies associated with the active URL of the
195+
// current browsing context.
196+
AllCookies() (*AllCookiesResponse, error)
189197
}
190198

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

0 commit comments

Comments
 (0)