Skip to content

Commit 80527e0

Browse files
author
heraclmene
committed
Add AddCookie functionality tests
1 parent 5582b29 commit 80527e0

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

remote_driver_cookie.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package goselenium
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
)
@@ -27,6 +28,11 @@ type Cookie struct {
2728
HTTPOnly bool `json:"httpOnly"`
2829
}
2930

31+
// AddCookieResponse is the result returned from calling the AddCookie method.
32+
type AddCookieResponse struct {
33+
State string
34+
}
35+
3036
func (s *seleniumWebDriver) AllCookies() (*AllCookiesResponse, error) {
3137
if len(s.sessionID) == 0 {
3238
return nil, newSessionIDError("AllCookies")
@@ -72,3 +78,33 @@ func (s *seleniumWebDriver) Cookie(name string) (*CookieResponse, error) {
7278

7379
return &response, nil
7480
}
81+
82+
func (s *seleniumWebDriver) AddCookie(c *Cookie) (*AddCookieResponse, error) {
83+
if len(s.sessionID) == 0 {
84+
return nil, newSessionIDError("AddCookie")
85+
}
86+
87+
var err error
88+
89+
url := fmt.Sprintf("%s/session/%s/cookie", s.seleniumURL, s.sessionID)
90+
91+
j := map[string]Cookie{
92+
"cookie": *c,
93+
}
94+
b, err := json.Marshal(j)
95+
if err != nil {
96+
return nil, newMarshallingError(err, "AddCookie", c)
97+
}
98+
body := bytes.NewReader(b)
99+
resp, err := s.stateRequest(&request{
100+
url: url,
101+
body: body,
102+
method: "POST",
103+
callingMethod: "AddCookie",
104+
})
105+
if err != nil {
106+
return nil, err
107+
}
108+
109+
return &AddCookieResponse{State: resp.State}, nil
110+
}

remote_driver_cookie_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,75 @@ func Test_CookieCookie_CorrectResponseIsReturned(t *testing.T) {
156156
t.Errorf(correctResponseErrorText)
157157
}
158158
}
159+
160+
/*
161+
AddCookie tests
162+
*/
163+
func Test_CookieAddCookie_InvalidSessionIdResultsInError(t *testing.T) {
164+
api := &testableAPIService{
165+
jsonToReturn: "",
166+
errorToReturn: nil,
167+
}
168+
169+
d := setUpDriver(setUpDefaultCaps(), api)
170+
_, err := d.AddCookie(nil)
171+
if err == nil || !IsSessionIDError(err) {
172+
t.Errorf(sessionIDErrorText)
173+
}
174+
}
175+
176+
func Test_CookieAddCookie_CommunicationErrorIsReturnedCorrectly(t *testing.T) {
177+
api := &testableAPIService{
178+
jsonToReturn: "",
179+
errorToReturn: errors.New("An error :<"),
180+
}
181+
182+
d := setUpDriver(setUpDefaultCaps(), api)
183+
d.sessionID = "12345"
184+
185+
_, err := d.AddCookie(&Cookie{Name: "cookie", Path: "/"})
186+
if err == nil || !IsCommunicationError(err) {
187+
t.Errorf(apiCommunicationErrorText)
188+
}
189+
}
190+
191+
func Test_CookieAddCookie_UnmarshallingErrorIsReturnedCorrectly(t *testing.T) {
192+
api := &testableAPIService{
193+
jsonToReturn: "Invalid JSON!",
194+
errorToReturn: nil,
195+
}
196+
197+
d := setUpDriver(setUpDefaultCaps(), api)
198+
d.sessionID = "12345"
199+
200+
_, err := d.AddCookie(&Cookie{Name: "cookie", Path: "/"})
201+
if err == nil || !IsUnmarshallingError(err) {
202+
t.Errorf(unmarshallingErrorText)
203+
}
204+
}
205+
206+
func Test_CookieAddCookie_CorrectResponseIsReturned(t *testing.T) {
207+
api := &testableAPIService{
208+
jsonToReturn: `{
209+
"state": "success",
210+
"value": {
211+
"name": "Test Cookie",
212+
"value": "Test Value",
213+
"path": "/",
214+
"domain": "www.google.com",
215+
"secure": true,
216+
"httpOnly": true,
217+
"expiry": "2016-12-25T00:00:00Z"
218+
}
219+
}`,
220+
errorToReturn: nil,
221+
}
222+
223+
d := setUpDriver(setUpDefaultCaps(), api)
224+
d.sessionID = "12345"
225+
226+
resp, err := d.AddCookie(&Cookie{Name: "cookie", Path: "/"})
227+
if err != nil || resp.State != "success" {
228+
t.Errorf(correctResponseErrorText)
229+
}
230+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package integrationtests
2+
3+
import (
4+
"testing"
5+
6+
"github.com/bunsenapp/go-selenium"
7+
)
8+
9+
func Test_CookieAddCookie_CanAddCookieWithCorrectFields(t *testing.T) {
10+
setUp()
11+
defer tearDown()
12+
13+
driver := createDriver(t)
14+
_, err := driver.CreateSession()
15+
if err != nil {
16+
errorAndWrap(t, "Error thrown whilst trying to create session.", err)
17+
}
18+
19+
_, err = driver.Go("https://news.ycombinator.com")
20+
if err != nil {
21+
errorAndWrap(t, "Error was thrown whilst navigating or result was not a success.", err)
22+
}
23+
24+
resp, err := driver.AddCookie(&goselenium.Cookie{
25+
Name: "cookie",
26+
Value: "cookieValue",
27+
Path: "/",
28+
Domain: ".ycombinator.com",
29+
SecureOnly: false,
30+
HTTPOnly: true,
31+
})
32+
if err != nil || resp.State != "success" {
33+
errorAndWrap(t, "Error was thrown whilst retrieving cookies", err)
34+
}
35+
36+
printObjectResult(resp)
37+
38+
}

web_driver.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ type WebDriver interface {
198198
// Cookie gets a single named cookie associated with the active URL of the
199199
// current browsing context.
200200
Cookie(name string) (*CookieResponse, error)
201+
202+
// AddCookie adds a cookie to the current browsing context.
203+
AddCookie(c *Cookie) (*AddCookieResponse, error)
201204
}
202205

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

0 commit comments

Comments
 (0)