Skip to content

Commit

Permalink
Remove happy path test and check cookies
Browse files Browse the repository at this point in the history
Incorporates the happy path test (which was already existed) into the
test table and checks whether cookies are set with another field.

We don't join AddCookie and Cookies tests because they test the code
from different perspectives and in a more involved way for their own.
  • Loading branch information
inancgumus committed Sep 6, 2023
1 parent 8445629 commit 46efce0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 53 deletions.
96 changes: 54 additions & 42 deletions tests/browser_context_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tests

import (
"fmt"
"net/http"
"testing"

Expand All @@ -15,48 +14,11 @@ import (
func TestBrowserContextAddCookies(t *testing.T) {
t.Parallel()

t.Run("document_cookies", func(t *testing.T) {
t.Parallel()

tb := newTestBrowser(t, withFileServer())

const (
testCookieName = "test_cookie_name"
testCookieValue = "test_cookie_value"
)

bc, err := tb.NewContext(nil)
require.NoError(t, err)
err = bc.AddCookies([]*api.Cookie{
{
Name: testCookieName,
Value: testCookieValue,
URL: tb.url(""),
},
})
require.NoError(t, err)

p, err := bc.NewPage()
require.NoError(t, err)

_, err = p.Goto(
tb.staticURL("add_cookies.html"),
tb.toGojaValue(struct {
WaitUntil string `js:"waitUntil"`
}{
WaitUntil: "load",
}),
)
require.NoError(t, err)

result := p.TextContent("#cookies", nil)
assert.EqualValues(t, fmt.Sprintf("%v=%v", testCookieName, testCookieValue), result)
})

tests := map[string]struct {
name string
cookies []*api.Cookie
wantErr bool
name string
cookies []*api.Cookie
wantCookiesToSet []*api.Cookie
wantErr bool
}{
"cookie": {
cookies: []*api.Cookie{
Expand All @@ -66,6 +28,18 @@ func TestBrowserContextAddCookies(t *testing.T) {
URL: "http://test.go",
},
},
wantCookiesToSet: []*api.Cookie{
{
Name: "test_cookie_name",
Value: "test_cookie_value",
Domain: "test.go",
Expires: -1,
HTTPOnly: false,
Path: "/",
SameSite: "",
Secure: false,
},
},
wantErr: false,
},
"cookie_with_url": {
Expand All @@ -76,6 +50,18 @@ func TestBrowserContextAddCookies(t *testing.T) {
URL: "http://test.go",
},
},
wantCookiesToSet: []*api.Cookie{
{
Name: "test_cookie_name",
Value: "test_cookie_value",
Domain: "test.go",
Expires: -1,
HTTPOnly: false,
Path: "/",
SameSite: "",
Secure: false,
},
},
wantErr: false,
},
"cookie_with_domain_and_path": {
Expand All @@ -87,6 +73,18 @@ func TestBrowserContextAddCookies(t *testing.T) {
Path: "/to/page",
},
},
wantCookiesToSet: []*api.Cookie{
{
Name: "test_cookie_name",
Value: "test_cookie_value",
Domain: "test.go",
Expires: -1,
HTTPOnly: false,
Path: "/to/page",
SameSite: "",
Secure: false,
},
},
wantErr: false,
},
"nil_cookies": {
Expand Down Expand Up @@ -156,6 +154,20 @@ func TestBrowserContextAddCookies(t *testing.T) {
return
}
require.NoError(t, err)

// ensure cookies are set.
cookies, err := bc.Cookies()
require.NoErrorf(t,
err, "failed to get cookies from the browser context",
)
require.Lenf(t,
tt.wantCookiesToSet, len(tt.cookies),
"incorrect number of cookies received from the browser context",
)
assert.Equalf(t,
tt.wantCookiesToSet, cookies,
"incorrect cookies received from the browser context",
)
})
}
}
Expand Down
11 changes: 0 additions & 11 deletions tests/static/add_cookies.html

This file was deleted.

0 comments on commit 46efce0

Please sign in to comment.