Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refac(test): omit defer AfterEach() via t.Cleanup #429

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/gorilla/websocket v1.5.1
github.com/h2non/filetype v1.1.3
github.com/mitchellh/go-ps v1.0.0
github.com/orisano/pixelmatch v0.0.0-20230914042517-fa304d1dc785
github.com/stretchr/testify v1.8.4
github.com/tidwall/gjson v1.17.0
go.uber.org/multierr v1.11.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
github.com/orisano/pixelmatch v0.0.0-20230914042517-fa304d1dc785 h1:J1//5K/6QF10cZ59zLcVNFGmBfiSrH8Cho/lNrViK9s=
github.com/orisano/pixelmatch v0.0.0-20230914042517-fa304d1dc785/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
4 changes: 2 additions & 2 deletions tests/apiresponse_assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestAssertionsResponseIsOKPass(t *testing.T) {
BeforeEach(t)
defer AfterEach(t)

response, err := page.Request().Get(server.EMPTY_PAGE)
require.NoError(t, err)
require.NoError(t, expect.APIResponse(response).ToBeOK())
Expand All @@ -18,7 +18,7 @@ func TestAssertionsResponseIsOKPass(t *testing.T) {

func TestAssertionsShouldPrintResponseWithTextContentTypeIfToBeOKFails(t *testing.T) {
BeforeEach(t)
defer AfterEach(t)

server.SetRoute("/text-content-type", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusNotFound)
Expand Down
8 changes: 4 additions & 4 deletions tests/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestBrowserContextExposeBinding(t *testing.T) {
BeforeEach(t)
defer AfterEach(t)

bindingSource := []*playwright.BindingSource{}
binding := func(source *playwright.BindingSource, a, b int) int {
bindingSource = append(bindingSource, source)
Expand All @@ -33,7 +33,7 @@ func TestBrowserContextExposeBinding(t *testing.T) {

func TestBrowserContextExposeFunction(t *testing.T) {
BeforeEach(t)
defer AfterEach(t)

err := context.ExposeFunction("add", func(args ...interface{}) interface{} {
return args[0].(int) + args[1].(int)
})
Expand All @@ -60,7 +60,7 @@ func TestBrowserContextExposeFunction(t *testing.T) {

func TestBrowserContextExposeBindingPanic(t *testing.T) {
BeforeEach(t)
defer AfterEach(t)

err := context.ExposeBinding("woof", func(source *playwright.BindingSource, args ...interface{}) interface{} {
panic(errors.New("WOOF WOOF"))
})
Expand All @@ -83,7 +83,7 @@ func TestBrowserContextExposeBindingPanic(t *testing.T) {

func TestBrowserContextExposeBindingHandleShouldWork(t *testing.T) {
BeforeEach(t)
defer AfterEach(t)

targets := []playwright.JSHandle{}

logme := func(t interface{}) int {
Expand Down
76 changes: 26 additions & 50 deletions tests/browser_context_storage_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestBrowserContextStorageStateShouldCaptureLocalStorage(t *testing.T) {
BeforeEach(t)
defer AfterEach(t)

page1, err := context.NewPage()
require.NoError(t, err)
require.NoError(t, page1.Route("**/*", func(route playwright.Route) {
Expand Down Expand Up @@ -53,36 +53,28 @@ func TestBrowserContextStorageStateShouldCaptureLocalStorage(t *testing.T) {
}

func TestBrowserContextStorageStateSetLocalStorage(t *testing.T) {
BeforeEach(t)
defer AfterEach(t)
context, err := browser.NewContext(
playwright.BrowserNewContextOptions{
StorageState: &playwright.OptionalStorageState{
Origins: []playwright.Origin{
{
Origin: "https://www.example.com",
LocalStorage: []playwright.NameValue{
{
Name: "name1",
Value: "value1",
},
BeforeEach(t, playwright.BrowserNewContextOptions{
StorageState: &playwright.OptionalStorageState{
Origins: []playwright.Origin{
{
Origin: "https://www.example.com",
LocalStorage: []playwright.NameValue{
{
Name: "name1",
Value: "value1",
},
},
},
},
},
)
require.NoError(t, err)
defer context.Close()
page, err := context.NewPage()
require.NoError(t, err)
defer page.Close()
})

require.NoError(t, page.Route("**/*", func(route playwright.Route) {
require.NoError(t, route.Fulfill(playwright.RouteFulfillOptions{
Body: "<html></html>",
}))
}))
_, err = page.Goto("https://www.example.com")
_, err := page.Goto("https://www.example.com")
require.NoError(t, err)
localStorage, err := page.Evaluate("window.localStorage")
require.NoError(t, err)
Expand All @@ -91,18 +83,15 @@ func TestBrowserContextStorageStateSetLocalStorage(t *testing.T) {

func TestBrowserContextStorageStateRoundTripThroughTheFile(t *testing.T) {
BeforeEach(t)
defer AfterEach(t)
page1, err := context.NewPage()
require.NoError(t, err)
defer page1.Close()
require.NoError(t, page1.Route("**/*", func(route playwright.Route) {

require.NoError(t, page.Route("**/*", func(route playwright.Route) {
require.NoError(t, route.Fulfill(playwright.RouteFulfillOptions{
Body: "<html></html>",
}))
}))
_, err = page1.Goto("https://www.example.com")
_, err := page.Goto("https://www.example.com")
require.NoError(t, err)
_, err = page1.Evaluate(`
_, err = page.Evaluate(`
() => {
localStorage["name1"] = "value1"
document.cookie = "username=John Doe"
Expand All @@ -121,15 +110,10 @@ func TestBrowserContextStorageStateRoundTripThroughTheFile(t *testing.T) {
require.NoError(t, err)
require.Equal(t, state, storageState)

context2, err := browser.NewContext(
playwright.BrowserNewContextOptions{
StorageStatePath: playwright.String(tempfile.Name()),
})
require.NoError(t, err)
defer context2.Close()
page2, err := context2.NewPage()
require.NoError(t, err)
defer page2.Close()
_, page2 := newBrowserContextAndPage(t, playwright.BrowserNewContextOptions{
StorageStatePath: playwright.String(tempfile.Name()),
})

require.NoError(t, page2.Route("**/*", func(route playwright.Route) {
require.NoError(t, route.Fulfill(playwright.RouteFulfillOptions{
Body: "<html></html>",
Expand All @@ -147,18 +131,15 @@ func TestBrowserContextStorageStateRoundTripThroughTheFile(t *testing.T) {

func TestBrowserContextStorageStateRoundTripThroughConvert(t *testing.T) {
BeforeEach(t)
defer AfterEach(t)
page1, err := context.NewPage()
require.NoError(t, err)
defer page1.Close()
require.NoError(t, page1.Route("**/*", func(route playwright.Route) {

require.NoError(t, page.Route("**/*", func(route playwright.Route) {
require.NoError(t, route.Fulfill(playwright.RouteFulfillOptions{
Body: "<html></html>",
}))
}))
_, err = page1.Goto("https://www.example.com")
_, err := page.Goto("https://www.example.com")
require.NoError(t, err)
_, err = page1.Evaluate(`
_, err = page.Evaluate(`
() => {
localStorage["name1"] = "value1"
document.cookie = "username=John Doe"
Expand All @@ -170,15 +151,10 @@ func TestBrowserContextStorageStateRoundTripThroughConvert(t *testing.T) {
storageState, err := context.StorageState()
require.NoError(t, err)

context2, err := browser.NewContext(
_, page2 := newBrowserContextAndPage(t,
playwright.BrowserNewContextOptions{
StorageState: storageState.ToOptionalStorageState(),
})
require.NoError(t, err)
defer context2.Close()
page2, err := context2.NewPage()
require.NoError(t, err)
defer page2.Close()
require.NoError(t, page2.Route("**/*", func(route playwright.Route) {
require.NoError(t, route.Fulfill(playwright.RouteFulfillOptions{
Body: "<html></html>",
Expand Down
Loading
Loading