Skip to content

Commit

Permalink
Rename k6 pkg to k6ext
Browse files Browse the repository at this point in the history
Due to a bug in xk6 (grafana/xk6#45), we can't
have a directory or a file with a name k6.

As discussed internally, @imiric suggested renaming the k6 pkg to k6ext:
"k6ext follows the k6 convention of doing the same: errext, netext,
etc., so let's stick to that.".
  • Loading branch information
inancgumus committed May 30, 2022
1 parent 15a43a4 commit 93c5ac4
Show file tree
Hide file tree
Showing 41 changed files with 335 additions and 342 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ jobs:
go version
GOPRIVATE="go.k6.io/k6" xk6 build latest \
--output ./k6ext \
--output ./k6extension \
--with github.com/grafana/xk6-browser=.
./k6ext version
./k6extension version
- name: Install Chrome dependencies
if: runner.os == 'Linux'
run: "${GITHUB_WORKSPACE}/.github/bin/install_chrome_deps_linux.sh"
Expand All @@ -66,7 +66,7 @@ jobs:
set -x
export XK6_HEADLESS=true
for f in examples/*.js; do
./k6ext run "$f"
./k6extension run "$f"
done
- name: Check screenshot
# TODO: Do something more sophisticated?
Expand Down
10 changes: 5 additions & 5 deletions chromium/browser_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

"github.com/grafana/xk6-browser/api"
"github.com/grafana/xk6-browser/common"
"github.com/grafana/xk6-browser/k6"
"github.com/grafana/xk6-browser/k6ext"

k6common "go.k6.io/k6/js/common"
k6modules "go.k6.io/k6/js/modules"
Expand Down Expand Up @@ -47,7 +47,7 @@ type BrowserType struct {
// - Initializes the goja runtime.
func NewBrowserType(ctx context.Context) api.BrowserType {
var (
vu = k6.GetVU(ctx)
vu = k6ext.GetVU(ctx)
rt = vu.Runtime()
hooks = common.NewHooks()
)
Expand Down Expand Up @@ -150,8 +150,8 @@ func (b *BrowserType) Launch(opts goja.Value) api.Browser {

// attach the browser process ID to the context
// so that we can kill it afterward if it lingers
// see: k6.Panic function.
b.Ctx = k6.WithProcessID(b.Ctx, browserProc.Pid())
// see: k6ext.Panic function.
b.Ctx = k6ext.WithProcessID(b.Ctx, browserProc.Pid())
browser, err := common.NewBrowser(b.Ctx, b.CancelFn, browserProc, launchOpts, logger)
if err != nil {
k6common.Throw(rt, err)
Expand Down Expand Up @@ -407,7 +407,7 @@ func parseWebsocketURL(ctx context.Context, rc io.Reader) (wsURL string, _ error
// makeLogger makes and returns an extension wide logger.
func makeLogger(ctx context.Context, launchOpts *common.LaunchOptions) (*common.Logger, error) {
var (
k6Logger = k6.GetVU(ctx).State().Logger
k6Logger = k6ext.GetVU(ctx).State().Logger
reCategoryFilter, _ = regexp.Compile(launchOpts.LogCategoryFilter)
logger = common.NewLogger(ctx, k6Logger, launchOpts.Debug, reCategoryFilter)
)
Expand Down
1 change: 0 additions & 1 deletion chromium/browser_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/stretchr/testify/require"
)

//nolint:funlen
func TestBrowserTypePrepareFlags(t *testing.T) {
t.Parallel()

Expand Down
20 changes: 10 additions & 10 deletions common/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"sync/atomic"

"github.com/grafana/xk6-browser/api"
"github.com/grafana/xk6-browser/k6"
"github.com/grafana/xk6-browser/k6ext"

k6modules "go.k6.io/k6/js/modules"

Expand Down Expand Up @@ -107,7 +107,7 @@ func newBrowser(ctx context.Context, cancelFn context.CancelFunc, browserProc *B
contexts: make(map[cdp.BrowserContextID]*BrowserContext),
pages: make(map[target.ID]*Page),
sessionIDtoTargetID: make(map[target.SessionID]target.ID),
vu: k6.GetVU(ctx),
vu: k6ext.GetVU(ctx),
logger: logger,
}
}
Expand Down Expand Up @@ -247,7 +247,7 @@ func (b *Browser) onAttachedToTarget(ev *target.EventAttachedToTarget) {
ev.SessionID, evti.TargetID, b.ctx.Err())
return // ignore
default:
k6.Panic(b.ctx, "cannot create NewPage for background_page event: %w", err)
k6ext.Panic(b.ctx, "cannot create NewPage for background_page event: %w", err)
}
}

Expand Down Expand Up @@ -286,7 +286,7 @@ func (b *Browser) onAttachedToTarget(ev *target.EventAttachedToTarget) {
ev.SessionID, evti.TargetID, b.ctx.Err())
return // ignore
default:
k6.Panic(b.ctx, "cannot create NewPage for page event: %w", err)
k6ext.Panic(b.ctx, "cannot create NewPage for page event: %w", err)
}
}

Expand Down Expand Up @@ -401,7 +401,7 @@ func (b *Browser) Close() {
action := cdpbrowser.Close()
if err := action.Do(cdp.WithExecutor(b.ctx, b.conn)); err != nil {
if _, ok := err.(*websocket.CloseError); !ok {
k6.Panic(b.ctx, "unable to execute %T: %v", action, err)
k6ext.Panic(b.ctx, "unable to execute %T: %v", action, err)
}
}

Expand Down Expand Up @@ -438,12 +438,12 @@ func (b *Browser) NewContext(opts goja.Value) api.BrowserContext {
browserContextID, err := action.Do(cdp.WithExecutor(b.ctx, b.conn))
b.logger.Debugf("Browser:NewContext", "bctxid:%v", browserContextID)
if err != nil {
k6.Panic(b.ctx, "cannot create browser context (%s): %w", browserContextID, err)
k6ext.Panic(b.ctx, "cannot create browser context (%s): %w", browserContextID, err)
}

browserCtxOpts := NewBrowserContextOptions()
if err := browserCtxOpts.Parse(b.ctx, opts); err != nil {
k6.Panic(b.ctx, "failed parsing options: %w", err)
k6ext.Panic(b.ctx, "failed parsing options: %w", err)
}

b.contextsMu.Lock()
Expand All @@ -464,7 +464,7 @@ func (b *Browser) NewPage(opts goja.Value) api.Page {
// The only accepted event value is "disconnected".
func (b *Browser) On(event string) *goja.Promise {
if event != EventBrowserDisconnected {
k6.Panic(b.ctx, "unknown browser event: %q, must be %q", event, EventBrowserDisconnected)
k6ext.Panic(b.ctx, "unknown browser event: %q, must be %q", event, EventBrowserDisconnected)
}

rt := b.vu.Runtime()
Expand Down Expand Up @@ -494,7 +494,7 @@ func (b *Browser) UserAgent() string {
action := cdpbrowser.GetVersion()
_, _, _, ua, _, err := action.Do(cdp.WithExecutor(b.ctx, b.conn))
if err != nil {
k6.Panic(b.ctx, "unable to get browser user agent: %w", err)
k6ext.Panic(b.ctx, "unable to get browser user agent: %w", err)
}
return ua
}
Expand All @@ -504,7 +504,7 @@ func (b *Browser) Version() string {
action := cdpbrowser.GetVersion()
_, product, _, _, _, err := action.Do(cdp.WithExecutor(b.ctx, b.conn))
if err != nil {
k6.Panic(b.ctx, "unable to get browser version: %w", err)
k6ext.Panic(b.ctx, "unable to get browser version: %w", err)
}
i := strings.Index(product, "/")
if i == -1 {
Expand Down
44 changes: 22 additions & 22 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"time"

"github.com/grafana/xk6-browser/api"
"github.com/grafana/xk6-browser/k6"
"github.com/grafana/xk6-browser/k6ext"

k6modules "go.k6.io/k6/js/modules"

Expand Down Expand Up @@ -71,7 +71,7 @@ func NewBrowserContext(
id: id,
opts: opts,
logger: logger,
vu: k6.GetVU(ctx),
vu: k6ext.GetVU(ctx),
timeoutSettings: NewTimeoutSettings(nil),
}

Expand All @@ -83,7 +83,7 @@ func NewBrowserContext(
}

func (b *BrowserContext) AddCookies(cookies goja.Value) {
k6.Panic(b.ctx, "BrowserContext.addCookies(cookies) has not been implemented yet")
k6ext.Panic(b.ctx, "BrowserContext.addCookies(cookies) has not been implemented yet")
}

// AddInitScript adds a script that will be initialized on all new pages.
Expand Down Expand Up @@ -133,7 +133,7 @@ func (b *BrowserContext) ClearCookies() {

action := storage.ClearCookies().WithBrowserContextID(b.id)
if err := action.Do(b.ctx); err != nil {
k6.Panic(b.ctx, "unable to clear cookies permissions: %w", err)
k6ext.Panic(b.ctx, "unable to clear cookies permissions: %w", err)
}
}

Expand All @@ -143,7 +143,7 @@ func (b *BrowserContext) ClearPermissions() {

action := cdpbrowser.ResetPermissions().WithBrowserContextID(b.id)
if err := action.Do(b.ctx); err != nil {
k6.Panic(b.ctx, "unable to clear override permissions: %w", err)
k6ext.Panic(b.ctx, "unable to clear override permissions: %w", err)
}
}

Expand All @@ -152,24 +152,24 @@ func (b *BrowserContext) Close() {
b.logger.Debugf("BrowserContext:Close", "bctxid:%v", b.id)

if b.id == "" {
k6.Panic(b.ctx, "default browser context can't be closed")
k6ext.Panic(b.ctx, "default browser context can't be closed")
}
if err := b.browser.disposeContext(b.id); err != nil {
k6.Panic(b.ctx, "cannot dispose browser context: %w", err)
k6ext.Panic(b.ctx, "cannot dispose browser context: %w", err)
}
}

func (b *BrowserContext) Cookies() []goja.Object {
k6.Panic(b.ctx, "BrowserContext.cookies() has not been implemented yet")
k6ext.Panic(b.ctx, "BrowserContext.cookies() has not been implemented yet")
return nil
}

func (b *BrowserContext) ExposeBinding(name string, callback goja.Callable, opts goja.Value) {
k6.Panic(b.ctx, "BrowserContext.exposeBinding(name, callback, opts) has not been implemented yet")
k6ext.Panic(b.ctx, "BrowserContext.exposeBinding(name, callback, opts) has not been implemented yet")
}

func (b *BrowserContext) ExposeFunction(name string, callback goja.Callable) {
k6.Panic(b.ctx, "BrowserContext.exposeFunction(name, callback) has not been implemented yet")
k6ext.Panic(b.ctx, "BrowserContext.exposeFunction(name, callback) has not been implemented yet")
}

// GrantPermissions enables the specified permissions, all others will be disabled.
Expand Down Expand Up @@ -213,13 +213,13 @@ func (b *BrowserContext) GrantPermissions(permissions []string, opts goja.Value)

action := cdpbrowser.GrantPermissions(perms).WithOrigin(origin).WithBrowserContextID(b.id)
if err := action.Do(cdp.WithExecutor(b.ctx, b.browser.conn)); err != nil {
k6.Panic(b.ctx, "override permissions: %w", err)
k6ext.Panic(b.ctx, "override permissions: %w", err)
}
}

// NewCDPSession returns a new CDP session attached to this target.
func (b *BrowserContext) NewCDPSession() api.CDPSession {
k6.Panic(b.ctx, "BrowserContext.newCDPSession() has not been implemented yet")
k6ext.Panic(b.ctx, "BrowserContext.newCDPSession() has not been implemented yet")
return nil
}

Expand All @@ -229,7 +229,7 @@ func (b *BrowserContext) NewPage() api.Page {

p, err := b.browser.newPageInContext(b.id)
if err != nil {
k6.Panic(b.ctx, "newPageInContext: %w", err)
k6ext.Panic(b.ctx, "newPageInContext: %w", err)
}

var (
Expand Down Expand Up @@ -257,7 +257,7 @@ func (b *BrowserContext) Pages() []api.Page {
}

func (b *BrowserContext) Route(url goja.Value, handler goja.Callable) {
k6.Panic(b.ctx, "BrowserContext.route(url, handler) has not been implemented yet")
k6ext.Panic(b.ctx, "BrowserContext.route(url, handler) has not been implemented yet")
}

// SetDefaultNavigationTimeout sets the default navigation timeout in milliseconds.
Expand All @@ -275,7 +275,7 @@ func (b *BrowserContext) SetDefaultTimeout(timeout int64) {
}

func (b *BrowserContext) SetExtraHTTPHeaders(headers map[string]string) {
k6.Panic(b.ctx, "BrowserContext.setExtraHTTPHeaders(headers) has not been implemented yet")
k6ext.Panic(b.ctx, "BrowserContext.setExtraHTTPHeaders(headers) has not been implemented yet")
}

// SetGeolocation overrides the geo location of the user.
Expand All @@ -284,13 +284,13 @@ func (b *BrowserContext) SetGeolocation(geolocation goja.Value) {

g := NewGeolocation()
if err := g.Parse(b.ctx, geolocation); err != nil {
k6.Panic(b.ctx, "cannot parse geo location: %v", err)
k6ext.Panic(b.ctx, "cannot parse geo location: %v", err)
}

b.opts.Geolocation = g
for _, p := range b.browser.getPages() {
if err := p.updateGeolocation(); err != nil {
k6.Panic(b.ctx, "cannot update geo location in target (%s): %w", p.targetID, err)
k6ext.Panic(b.ctx, "cannot update geo location in target (%s): %w", p.targetID, err)
}
}
}
Expand All @@ -308,7 +308,7 @@ func (b *BrowserContext) SetHTTPCredentials(httpCredentials goja.Value) {

c := NewCredentials()
if err := c.Parse(b.ctx, httpCredentials); err != nil {
k6.Panic(b.ctx, "cannot set HTTP credentials: %w", err)
k6ext.Panic(b.ctx, "cannot set HTTP credentials: %w", err)
}

b.opts.HttpCredentials = c
Expand All @@ -328,11 +328,11 @@ func (b *BrowserContext) SetOffline(offline bool) {
}

func (b *BrowserContext) StorageState(opts goja.Value) {
k6.Panic(b.ctx, "BrowserContext.storageState(opts) has not been implemented yet")
k6ext.Panic(b.ctx, "BrowserContext.storageState(opts) has not been implemented yet")
}

func (b *BrowserContext) Unroute(url goja.Value, handler goja.Callable) {
k6.Panic(b.ctx, "BrowserContext.unroute(url, handler) has not been implemented yet")
k6ext.Panic(b.ctx, "BrowserContext.unroute(url, handler) has not been implemented yet")
}

func (b *BrowserContext) WaitForEvent(event string, optsOrPredicate goja.Value) interface{} {
Expand All @@ -354,7 +354,7 @@ func (b *BrowserContext) WaitForEvent(event string, optsOrPredicate goja.Value)
case "predicate":
predicateFn, isCallable = goja.AssertFunction(opts.Get(k))
if !isCallable {
k6.Panic(b.ctx, "expected callable predicate")
k6ext.Panic(b.ctx, "expected callable predicate")
}
case "timeout":
timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond
Expand All @@ -363,7 +363,7 @@ func (b *BrowserContext) WaitForEvent(event string, optsOrPredicate goja.Value)
default:
predicateFn, isCallable = goja.AssertFunction(optsOrPredicate)
if !isCallable {
k6.Panic(b.ctx, "expected callable predicate")
k6ext.Panic(b.ctx, "expected callable predicate")
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions common/browser_context_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"context"
"fmt"

"github.com/grafana/xk6-browser/k6"
"github.com/grafana/xk6-browser/k6ext"

"github.com/dop251/goja"
)
Expand Down Expand Up @@ -69,7 +69,7 @@ func NewBrowserContextOptions() *BrowserContextOptions {
}

func (b *BrowserContextOptions) Parse(ctx context.Context, opts goja.Value) error {
rt := k6.Runtime(ctx)
rt := k6ext.Runtime(ctx)
if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) {
opts := opts.ToObject(rt)
for _, k := range opts.Keys() {
Expand Down
2 changes: 1 addition & 1 deletion common/browser_context_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package common
import (
"testing"

"github.com/grafana/xk6-browser/k6/k6test"
"github.com/grafana/xk6-browser/k6ext/k6test"

"github.com/stretchr/testify/assert"
)
Expand Down
Loading

0 comments on commit 93c5ac4

Please sign in to comment.