Skip to content

Commit

Permalink
Add Cookie functionality and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
heraclmene committed Dec 22, 2016
1 parent 7625254 commit 5582b29
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
30 changes: 30 additions & 0 deletions remote_driver_cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ type AllCookiesResponse struct {
Cookies []Cookie `json:"value"`
}

// CookieResponse is the response returned from the Cookie method.
type CookieResponse struct {
State string `json:"state"`
Cookie Cookie `json:"value"`
}

// Cookie represents a browser cookie.
type Cookie struct {
Name string `json:"name"`
Expand All @@ -28,6 +34,7 @@ func (s *seleniumWebDriver) AllCookies() (*AllCookiesResponse, error) {

var response AllCookiesResponse
var err error

url := fmt.Sprintf("%s/session/%s/cookie", s.seleniumURL, s.sessionID)

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

return &response, nil
}

func (s *seleniumWebDriver) Cookie(name string) (*CookieResponse, error) {
if len(s.sessionID) == 0 {
return nil, newSessionIDError("Cookie")
}

var response CookieResponse
var err error

url := fmt.Sprintf("%s/session/%s/cookie/%s", s.seleniumURL, s.sessionID, name)

resp, err := s.apiService.performRequest(url, "GET", nil)
if err != nil {
return nil, newCommunicationError(err, "Cookie", url, nil)
}

err = json.Unmarshal(resp, &response)
if err != nil {
return nil, newUnmarshallingError(err, "Cookie", string(resp))
}

return &response, nil
}
78 changes: 78 additions & 0 deletions remote_driver_cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"testing"
)

/*
AllCookies tests
*/
func Test_CookieAllCookies_InvalidSessionIdResultsInError(t *testing.T) {
api := &testableAPIService{
jsonToReturn: "",
Expand Down Expand Up @@ -78,3 +81,78 @@ func Test_CookieAllCookies_CorrectResponseIsReturned(t *testing.T) {
t.Errorf(correctResponseErrorText)
}
}

/*
Cookie tests
*/
func Test_CookieCookie_InvalidSessionIdResultsInError(t *testing.T) {
api := &testableAPIService{
jsonToReturn: "",
errorToReturn: nil,
}

d := setUpDriver(setUpDefaultCaps(), api)
_, err := d.Cookie("test")
if err == nil || !IsSessionIDError(err) {
t.Errorf(sessionIDErrorText)
}
}

func Test_CookieCookie_CommunicationErrorIsReturnedCorrectly(t *testing.T) {
api := &testableAPIService{
jsonToReturn: "",
errorToReturn: errors.New("An error :<"),
}

d := setUpDriver(setUpDefaultCaps(), api)
d.sessionID = "12345"

_, err := d.Cookie("test")
if err == nil || !IsCommunicationError(err) {
t.Errorf(apiCommunicationErrorText)
}
}

func Test_CookieCookie_UnmarshallingErrorIsReturnedCorrectly(t *testing.T) {
api := &testableAPIService{
jsonToReturn: "Invalid JSON!",
errorToReturn: nil,
}

d := setUpDriver(setUpDefaultCaps(), api)
d.sessionID = "12345"

_, err := d.Cookie("test")
if err == nil || !IsUnmarshallingError(err) {
t.Errorf(unmarshallingErrorText)
}
}

func Test_CookieCookie_CorrectResponseIsReturned(t *testing.T) {
api := &testableAPIService{
jsonToReturn: `{
"state": "success",
"value": {
"name": "Test Cookie",
"value": "Test Value",
"path": "/",
"domain": "www.google.com",
"secure": true,
"httpOnly": true,
"expiry": "2016-12-25T00:00:00Z"
}
}`,
errorToReturn: nil,
}

d := setUpDriver(setUpDefaultCaps(), api)
d.sessionID = "12345"

resp, err := d.Cookie("test")
if err != nil || resp.State != "success" || resp.Cookie.Name != "Test Cookie" ||
resp.Cookie.Value != "Test Value" || resp.Cookie.Path != "/" ||
resp.Cookie.Domain != "www.google.com" || !resp.Cookie.SecureOnly ||
!resp.Cookie.HTTPOnly {
t.Errorf(correctResponseErrorText)
}
}
26 changes: 26 additions & 0 deletions test/integration_tests/cookie_cookie_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package integrationtests

import "testing"

func Test_CookieCookie_CanRetrieveCookieFromWebPage(t *testing.T) {
setUp()
defer tearDown()

driver := createDriver(t)
_, err := driver.CreateSession()
if err != nil {
errorAndWrap(t, "Error thrown whilst trying to create session.", err)
}

_, err = driver.Go("https://news.ycombinator.com")
if err != nil {
errorAndWrap(t, "Error was thrown whilst navigating or result was not a success.", err)
}

resp, err := driver.Cookie("__cfduid")
if err != nil || resp.State != "success" || resp.Cookie.Name == "" {
errorAndWrap(t, "Error was thrown whilst retrieving cookie", err)
}

printObjectResult(resp)
}
4 changes: 4 additions & 0 deletions web_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ type WebDriver interface {
// AllCookies returns all cookies associated with the active URL of the
// current browsing context.
AllCookies() (*AllCookiesResponse, error)

// Cookie gets a single named cookie associated with the active URL of the
// current browsing context.
Cookie(name string) (*CookieResponse, error)
}

// Element is an interface which specifies what all WebDriver elements
Expand Down

0 comments on commit 5582b29

Please sign in to comment.