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

Add a LookupBool utility function #1003

Merged
merged 4 commits into from
Aug 18, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 28 additions & 2 deletions env/env.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Package env provides types to interact with environment setup.
package env

import "os"
import (
"os"
"strconv"
)

// Execution specific.
const (
Expand Down Expand Up @@ -66,7 +69,7 @@ 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) }
Expand All @@ -82,3 +85,26 @@ func ConstLookup(k, v string) LookupFunc {
return EmptyLookup(key)
}
}

// LookupBool returns the result of Lookup as a bool.
// If the key does not exist or the value is not a valid bool, it returns false.
// Otherwise it returns the bool value and true.
func LookupBool(key string) (value bool, ok bool) {
v, ok := Lookup(key)
if !ok {
return false, false
}
bv, err := strconv.ParseBool(v)
if err != nil {
return false, true
}
return bv, true
}

// IsBrowserHeadless returns true if the BrowserHeadless environment
// variable is not set or set to true.
// The default behaviour is to run the browser in headless mode.
func IsBrowserHeadless() bool {
v, ok := LookupBool(BrowserHeadless)
return !ok || v
}
33 changes: 14 additions & 19 deletions tests/frame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tests
import (
"context"
"net/http"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -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.IsBrowserHeadless() {
t.Skip("skipped when in headless mode")
}

// run the browser in headfull mode.
Expand Down Expand Up @@ -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.IsBrowserHeadless() {
t.Skip("skipped when in headless mode")
}

tb := newTestBrowser(
Expand Down