diff --git a/env/env.go b/env/env.go index 4029ca122..0d5c986d7 100644 --- a/env/env.go +++ b/env/env.go @@ -1,7 +1,10 @@ // Package env provides types to interact with environment setup. package env -import "os" +import ( + "os" + "strconv" +) // Execution specific. const ( @@ -66,11 +69,25 @@ const ( type LookupFunc func(key string) (string, bool) // EmptyLookup is a LookupFunc that always returns "" and false. -func EmptyLookup(key string) (string, bool) { return "", false } +func EmptyLookup(_ string) (string, bool) { return "", false } // Lookup is a LookupFunc that uses os.LookupEnv. func Lookup(key string) (string, bool) { return os.LookupEnv(key) } +// LookupBool is a LookupFunc that returns the result of Lookup as a bool. +// Returns false either if the key does not exist or the value is not a bool. +func LookupBool(key string) bool { + v, ok := os.LookupEnv(key) + if !ok { + return false + } + bv, err := strconv.ParseBool(v) + if err != nil { + return false + } + return bv +} + // ConstLookup is a LookupFunc that always returns the given value and true // if the key matches the given key. Otherwise it returns EmptyLookup // behaviour. Useful for testing. diff --git a/examples/context.js b/examples/context.js new file mode 100644 index 000000000..255021a16 --- /dev/null +++ b/examples/context.js @@ -0,0 +1,22 @@ +import { browser } from 'k6/x/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + } +} + +export default async function() { + const context1 = browser.newContext(); + context1.close(); + + const context2 = browser.newContext(); + context2.close() +} diff --git a/tests/frame_test.go b/tests/frame_test.go index 7aedab24a..282e76f4e 100644 --- a/tests/frame_test.go +++ b/tests/frame_test.go @@ -3,7 +3,6 @@ package tests import ( "context" "net/http" - "strconv" "testing" "time" @@ -72,15 +71,13 @@ func TestFrameDismissDialogBox(t *testing.T) { func TestFrameNoPanicWithEmbeddedIFrame(t *testing.T) { t.Parallel() - if s, ok := env.Lookup(env.BrowserHeadless); ok { - if v, err := strconv.ParseBool(s); err == nil && v { - // We're skipping this when running in headless - // environments since the bug that the test fixes - // only surfaces when in headfull mode. - // Remove this skip once we have headfull mode in - // CI: https://github.com/grafana/xk6-browser/issues/678 - t.Skip("skipped when in headless mode") - } + // We're skipping this when running in headless + // environments since the bug that the test fixes + // only surfaces when in headfull mode. + // Remove this skip once we have headfull mode in + // CI: https://github.com/grafana/xk6-browser/issues/678 + if env.LookupBool(env.BrowserHeadless) { + t.Skip("skipped when in headless mode") } // run the browser in headfull mode. @@ -110,15 +107,13 @@ func TestFrameNoPanicWithEmbeddedIFrame(t *testing.T) { func TestFrameNoPanicNavigateAndClickOnPageWithIFrames(t *testing.T) { t.Parallel() - if s, ok := env.Lookup(env.BrowserHeadless); ok { - if v, err := strconv.ParseBool(s); err == nil && v { - // We're skipping this when running in headless - // environments since the bug that the test fixes - // only surfaces when in headfull mode. - // Remove this skip once we have headfull mode in - // CI: https://github.com/grafana/xk6-browser/issues/678 - t.Skip("skipped when in headless mode") - } + // We're skipping this when running in headless + // environments since the bug that the test fixes + // only surfaces when in headfull mode. + // Remove this skip once we have headfull mode in + // CI: https://github.com/grafana/xk6-browser/issues/678 + if env.LookupBool(env.BrowserHeadless) { + t.Skip("skipped when in headless mode") } tb := newTestBrowser(