Skip to content

Commit

Permalink
Add xk6-browser as an experimental module (#2884)
Browse files Browse the repository at this point in the history
* Add xk6-browser as an experimental module

* Update to release version of xk6-browser

* Update to the experimental import

* Add vendored xk6-browser dependency

* Update version of xk6-browser from main

This version of xk6-browser deps now match k6.

* Add the vendor of the deps

* Add wrapper RootModule for browser tests

This wrapper RootModule will allow browser tests to run if the correct
K6_BROWSER_ENABLE_RUN env var is set.

* Add tests for the browser wrapper RootModule

* Refactor to use VU to get env vars in RootModule

* Apply suggestions from code review

Co-authored-by: na-- <n@andreev.sh>

* fixup! Apply suggestions from code review

* fixup! Add wrapper RootModule for browser tests

* Apply suggestions from code review

Co-authored-by: Ivan Mirić <ivan.miric@grafana.com>

---------

Co-authored-by: na-- <n@andreev.sh>
Co-authored-by: Ivan Mirić <ivan.miric@grafana.com>
  • Loading branch information
3 people authored Feb 2, 2023
1 parent 134ca65 commit 1da5da3
Show file tree
Hide file tree
Showing 267 changed files with 163,038 additions and 0 deletions.
74 changes: 74 additions & 0 deletions cmd/tests/cmd_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1644,3 +1644,77 @@ func TestPrometheusRemoteWriteOutput(t *testing.T) {

assert.Contains(t, stdout, "output: Prometheus remote write")
}

func TestBrowserPermissions(t *testing.T) {
t.Parallel()

tests := []struct {
name string
envVarValue string
envVarMsgValue string
expectedExitCode exitcodes.ExitCode
expectedError string
}{
{
name: "no env var set",
envVarValue: "",
expectedExitCode: 107,
expectedError: "To run browser tests set env var K6_BROWSER_ENABLED=true",
},
{
name: "env var set but set to false",
envVarValue: "false",
expectedExitCode: 107,
expectedError: "To run browser tests set env var K6_BROWSER_ENABLED=true",
},
{
name: "env var set but set to 09adsu",
envVarValue: "09adsu",
expectedExitCode: 107,
expectedError: "To run browser tests set env var K6_BROWSER_ENABLED=true",
},
{
name: "with custom message",
envVarValue: "09adsu",
envVarMsgValue: "Try again later",
expectedExitCode: 107,
expectedError: "Try again later",
},
{
name: "env var set and set to true",
envVarValue: "true",
expectedExitCode: 0,
expectedError: "",
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
script := `
import { chromium } from 'k6/experimental/browser';
export default function() {};
`

ts := getSingleFileTestState(t, script, []string{}, tt.expectedExitCode)
if tt.envVarValue != "" {
ts.Env["K6_BROWSER_ENABLED"] = tt.envVarValue
}
if tt.envVarMsgValue != "" {
ts.Env["K6_BROWSER_ENABLED_MSG"] = tt.envVarMsgValue
}
cmd.ExecuteWithGlobalState(ts.GlobalState)

loglines := ts.LoggerHook.Drain()

if tt.expectedError == "" {
require.Len(t, loglines, 0)
return
}

assert.Contains(t, loglines[0].Message, tt.expectedError)
})
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/go-sourcemap/sourcemap v2.1.4-0.20211119122758-180fcef48034+incompatible
github.com/golang/protobuf v1.5.2
github.com/gorilla/websocket v1.5.0
github.com/grafana/xk6-browser v0.8.1-0.20230131153655-c347eae1e46a
github.com/grafana/xk6-output-prometheus-remote v0.1.0
github.com/grafana/xk6-redis v0.1.1
github.com/grafana/xk6-timers v0.1.2
Expand Down Expand Up @@ -52,6 +53,8 @@ require (
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/chromedp/cdproto v0.0.0-20221023212508-67ada9507fb2 // indirect
github.com/chromedp/sysutil v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dlclark/regexp2 v1.7.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chromedp/cdproto v0.0.0-20221023212508-67ada9507fb2 h1:xESwMZNYkDnZf9MUk+6lXfMbpDnEJwlEuIxKYKM1vJY=
github.com/chromedp/cdproto v0.0.0-20221023212508-67ada9507fb2/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs=
github.com/chromedp/sysutil v1.0.0 h1:+ZxhTpfpZlmchB58ih/LBHX52ky7w2VhQVKQMucy3Ic=
github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moAV0xufSww=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down Expand Up @@ -184,6 +188,8 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grafana/xk6-browser v0.8.1-0.20230131153655-c347eae1e46a h1:KWu67+49qu24PvzoRj+qVTsDfhY51m0WMFMUZolqcXE=
github.com/grafana/xk6-browser v0.8.1-0.20230131153655-c347eae1e46a/go.mod h1:elvssKBUHI8rLShlWb+lOUhkaGcn0RcipKE6MRcezVw=
github.com/grafana/xk6-output-prometheus-remote v0.1.0 h1:yJc09O6TeBYLFfNG/dqBDtvHmM9P1B2ZFTyr0HgvsHY=
github.com/grafana/xk6-output-prometheus-remote v0.1.0/go.mod h1:R4o0VbIfbQNNPSGkeeiCBLzwNfG+DEdfKYNsV1oww1Y=
github.com/grafana/xk6-redis v0.1.1 h1:rvWnLanRB2qzDwuY6NMBe6PXei3wJ3kjYvfCwRJ+q+8=
Expand Down Expand Up @@ -366,6 +372,7 @@ go.k6.io/k6 v0.38.2/go.mod h1:1bTdDsXTT2V3in3ZgdR15MDW6SQQh5nWni59tirqNB8=
go.k6.io/k6 v0.40.1-0.20221017105932-3c97ec7d1231/go.mod h1:vaSQ1A2rnC+wrKRJ4ExZCT6kKLfAEYoaIY9UR0uAjjk=
go.k6.io/k6 v0.41.0/go.mod h1:ZrgrR06UZbzZt9u+so/yQhlkJFH5gkJ3qCp1hwq1mEU=
go.k6.io/k6 v0.41.1-0.20221116104224-5fa71b761185/go.mod h1:JHBf2FpDMqdDvcHMPmSzE2YBBI9HPHn86ndgp5gYWQM=
go.k6.io/k6 v0.42.1-0.20230130080633-582ec4d3940c/go.mod h1:0ThS9XqUtlglwwsZ7IxXs+QJTkjkMUZgQ/N1r6Wajm8=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
Expand Down Expand Up @@ -475,6 +482,7 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
2 changes: 2 additions & 0 deletions js/jsmodules.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"go.k6.io/k6/js/modules/k6/data"
"go.k6.io/k6/js/modules/k6/encoding"
"go.k6.io/k6/js/modules/k6/execution"
"go.k6.io/k6/js/modules/k6/experimental/browser"
"go.k6.io/k6/js/modules/k6/experimental/tracing"
"go.k6.io/k6/js/modules/k6/grpc"
"go.k6.io/k6/js/modules/k6/html"
Expand All @@ -32,6 +33,7 @@ func getInternalJSModules() map[string]interface{} {
"k6/experimental/websockets": &expws.RootModule{},
"k6/experimental/timers": timers.New(),
"k6/experimental/tracing": tracing.New(),
"k6/experimental/browser": browser.New(),
"k6/net/grpc": grpc.New(),
"k6/html": html.New(),
"k6/http": http.New(),
Expand Down
1 change: 1 addition & 0 deletions js/modules/k6/experimental/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Although [accessible in k6 scripts](../../../initcontext.go) under the `k6/exper
* [`k6/experimental/k6-redis`](https://github.com/grafana/xk6-redis)
* [`k6/experimental/k6-websockets`](https://github.com/grafana/xk6-websockets)
* [`k6/experimental/k6-timers`](https://github.com/grafana/xk6-timers)
* [`k6/experimental/k6-browser`](https://github.com/grafana/xk6-browser)

While we intend to keep these modules as stable as possible, we may need to add features or introduce breaking changes. This could happen at any time until we release the module as stable. **use them at your own risk**.

Expand Down
58 changes: 58 additions & 0 deletions js/modules/k6/experimental/browser/rootmodule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Package browser contains a RootModule wrapper
// that wraps around the experimental browser
// RootModule.
package browser

import (
"errors"
"strconv"

xk6browser "github.com/grafana/xk6-browser/browser"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"
)

type (
// RootModule is a wrapper around the experimental
// browser RootModule. It will prevent browser test
// runs unless K6_BROWSER_ENABLED env var is set.
RootModule struct {
rm *xk6browser.RootModule
}
)

// New creates an experimental browser RootModule
// and wraps it around this internal RootModule.
func New() *RootModule {
return &RootModule{
rm: xk6browser.New(),
}
}

// NewModuleInstance will check to see if
// K6_BROWSER_ENABLED is set before allowing
// test runs to continue.
func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
env := vu.InitEnv()

throwError := func() {
msg := "To run browser tests set env var K6_BROWSER_ENABLED=true"
if m, ok := env.LookupEnv("K6_BROWSER_ENABLED_MSG"); ok && m != "" {
msg = m
}

common.Throw(vu.Runtime(), errors.New(msg))
}

vs, ok := env.LookupEnv("K6_BROWSER_ENABLED")
if !ok {
throwError()
}

v, err := strconv.ParseBool(vs)
if err != nil || !v {
throwError()
}

return r.rm.NewModuleInstance(vu)
}
41 changes: 41 additions & 0 deletions samples/experimental/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { check } from 'k6';
import { chromium } from 'k6/experimental/browser';

export const options = {
thresholds: {
checks: ["rate==1.0"]
}
}

export default async function() {
const browser = chromium.launch({
headless: __ENV.XK6_HEADLESS ? true : false,
});
const context = browser.newContext();
const page = context.newPage();

try {
// Goto front page, find login link and click it
await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' });
await Promise.all([
page.waitForNavigation(),
page.locator('a[href="/my_messages.php"]').click(),
]);
// Enter login credentials and login
page.locator('input[name="login"]').type('admin');
page.locator('input[name="password"]').type('123');
// We expect the form submission to trigger a navigation, so to prevent a
// race condition, setup a waiter concurrently while waiting for the click
// to resolve.
await Promise.all([
page.waitForNavigation(),
page.locator('input[type="submit"]').click(),
]);
check(page, {
'header': page.locator('h2').textContent() == 'Welcome, admin!',
});
} finally {
page.close();
browser.close();
}
}
2 changes: 2 additions & 0 deletions vendor/github.com/chromedp/cdproto/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/chromedp/cdproto/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions vendor/github.com/chromedp/cdproto/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1da5da3

Please sign in to comment.