Skip to content

Commit

Permalink
refac(test): omit defer AfterEach() via t.Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
canstand committed Mar 19, 2024
1 parent 4311525 commit 6ff932d
Show file tree
Hide file tree
Showing 35 changed files with 411 additions and 442 deletions.
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
8 changes: 4 additions & 4 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 @@ -54,7 +54,7 @@ func TestBrowserContextStorageStateShouldCaptureLocalStorage(t *testing.T) {

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

context, err := browser.NewContext(
playwright.BrowserNewContextOptions{
StorageState: &playwright.OptionalStorageState{
Expand Down Expand Up @@ -91,7 +91,7 @@ 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()
Expand Down Expand Up @@ -147,7 +147,7 @@ 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()
Expand Down
56 changes: 28 additions & 28 deletions tests/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (

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

require.Equal(t, context.Browser(), browser)
}

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

require.Equal(t, 1, len(browser.Contexts()))
context2, err := browser.NewContext()
require.NoError(t, err)
Expand All @@ -35,7 +35,7 @@ func TestBrowserContextNewContext(t *testing.T) {

func TestBrowserContextClose(t *testing.T) {
BeforeEach(t)
defer AfterEach(t, false)

context2, err := browser.NewContext()
require.NoError(t, err)
require.Equal(t, 2, len(browser.Contexts()))
Expand All @@ -49,7 +49,7 @@ func TestBrowserContextClose(t *testing.T) {

func TestBrowserContextCloseWithHarDownload(t *testing.T) {
BeforeEach(t)
defer AfterEach(t, false)

tmpFile := filepath.Join(t.TempDir(), "test.har")
context2, err := browser.NewContext(playwright.BrowserNewContextOptions{
RecordHarPath: playwright.String(tmpFile),
Expand All @@ -64,7 +64,7 @@ func TestBrowserContextCloseWithHarDownload(t *testing.T) {

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

offline, err := page.Evaluate("window.navigator.onLine")
require.NoError(t, err)
require.True(t, offline.(bool))
Expand All @@ -82,7 +82,7 @@ func TestBrowserContextOffline(t *testing.T) {

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

require.NoError(t, context.SetExtraHTTPHeaders(map[string]string{
"extra-http": "42",
}))
Expand All @@ -100,14 +100,15 @@ func TestBrowserContextSetExtraHTTPHeaders(t *testing.T) {

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

server.SetBasicAuth("/empty.html", "user", "pass")

response, err := page.Goto(server.EMPTY_PAGE)
require.NoError(t, err)
require.Equal(t, 401, response.Status())
context.Close()
newContextWithOptions(t, playwright.BrowserNewContextOptions{

context, page = newBrowserContextAndPage(t, playwright.BrowserNewContextOptions{
AcceptDownloads: playwright.Bool(true),
HasTouch: playwright.Bool(true),
HttpCredentials: &playwright.HttpCredentials{
Expand All @@ -122,7 +123,7 @@ func TestBrowserContextSetHttpCredentials(t *testing.T) {

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

cdpSession, err := page.Context().NewCDPSession(page)
if isChromium {
require.NoError(t, err)
Expand All @@ -134,7 +135,7 @@ func TestBrowserContextNewCDPSession(t *testing.T) {

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

require.NoError(t, context.GrantPermissions([]string{"geolocation"}))
_, err := page.Goto(server.EMPTY_PAGE)
require.NoError(t, err)
Expand All @@ -155,7 +156,7 @@ func TestBrowserContextSetGeolocation(t *testing.T) {

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

_, err := page.Goto(server.EMPTY_PAGE)
require.NoError(t, err)
require.NoError(t, context.AddCookies([]playwright.OptionalCookie{
Expand Down Expand Up @@ -199,7 +200,7 @@ func TestBrowserContextAddCookies(t *testing.T) {

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

require.NoError(t, context.AddInitScript(playwright.Script{
Content: playwright.String(`window['injected'] = 123;`),
}))
Expand All @@ -212,7 +213,7 @@ func TestBrowserContextAddInitScript(t *testing.T) {

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

require.NoError(t, context.AddInitScript(playwright.Script{
Path: playwright.String(Asset("injectedfile.js")),
}))
Expand All @@ -225,7 +226,7 @@ func TestBrowserContextAddInitScriptWithPath(t *testing.T) {

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

_, err := page.Goto(server.EMPTY_PAGE)
require.NoError(t, err)
popupEvent, err := page.ExpectEvent("popup", func() error {
Expand All @@ -239,7 +240,6 @@ func TestBrowserContextWindowOpenshouldUseParentTabContext(t *testing.T) {

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

intercepted := []int{}
handler1 := func(route playwright.Route) {
Expand Down Expand Up @@ -281,7 +281,7 @@ func TestBrowserContextUnrouteShouldWork(t *testing.T) {

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

if !isChromium {
t.Skip()
}
Expand Down Expand Up @@ -336,7 +336,7 @@ func TestBrowserContextShouldReturnBackgroundPage(t *testing.T) {

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

context.OnPage(func(p playwright.Page) {
require.Equal(t, server.EMPTY_PAGE, p.URL())
})
Expand All @@ -350,7 +350,7 @@ func TestPageEventShouldHaveURL(t *testing.T) {

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

context.OnConsole(func(message playwright.ConsoleMessage) {
require.Equal(t, "hello", message.Text())
})
Expand All @@ -365,7 +365,7 @@ func TestConsoleEventShouldWork(t *testing.T) {

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

var requests []playwright.Request
context.OnRequest(func(request playwright.Request) {
requests = append(requests, request)
Expand All @@ -388,7 +388,7 @@ func TestBrowserContextEventsRequest(t *testing.T) {

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

var responses []playwright.Response
context.OnResponse(func(response playwright.Response) {
responses = append(responses, response)
Expand All @@ -411,7 +411,7 @@ func TestBrowserContextEventsResponse(t *testing.T) {

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

server.SetRoute("/one-style.css", func(w http.ResponseWriter, r *http.Request) {
hw, ok := w.(http.Hijacker)
if ok {
Expand All @@ -433,7 +433,7 @@ func TestBrowserContextEventsRequestFailed(t *testing.T) {

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

var events []string
context.OnRequest(func(request playwright.Request) {
events = append(events, "request")
Expand All @@ -454,7 +454,7 @@ func TestBrowerContextEventsShouldFireInProperOrder(t *testing.T) {

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

browser, err := browserType.Launch()
require.NoError(t, err)
context, err := browser.NewContext()
Expand All @@ -469,7 +469,7 @@ func TestBrowserContextShouldFireCloseEvent(t *testing.T) {

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

if isFirefox {
t.Skip("flaky on firefox")
}
Expand All @@ -492,7 +492,7 @@ func TestDialogEventShouldWorkInImmdiatelyClosedPopup(t *testing.T) {

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

_, err := context.ExpectPage(func() error {
return context.Close()
})
Expand All @@ -501,7 +501,7 @@ func TestBrowserContextCloseShouldAbortWaitForEvent(t *testing.T) {

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

countClose := atomic.Int32{}
context.OnClose(func(bc playwright.BrowserContext) {
countClose.Add(1)
Expand All @@ -514,7 +514,7 @@ func TestBrowserContextCloseShouldBeCallableTwice(t *testing.T) {

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

ret, err := page.Context().ExpectEvent("weberror", func() error {
return page.SetContent(`<script>throw new Error("boom")</script>`)
})
Expand All @@ -528,7 +528,7 @@ func TestPageErrorEventShouldWork(t *testing.T) {

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

responseChan := make(chan playwright.Response, 1)
context.OnResponse(func(response playwright.Response) {
responseChan <- response
Expand Down

0 comments on commit 6ff932d

Please sign in to comment.