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

Rename k6 pkg to k6ext #328

Merged
merged 1 commit into from
May 31, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
"github.com/grafana/xk6-browser/log"

k6common "go.k6.io/k6/js/common"
Expand Down Expand Up @@ -48,7 +48,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 @@ -151,8 +151,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 @@ -408,7 +408,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) (*log.Logger, error) {
var (
k6Logger = k6.GetVU(ctx).State().Logger
k6Logger = k6ext.GetVU(ctx).State().Logger
reCategoryFilter, _ = regexp.Compile(launchOpts.LogCategoryFilter)
logger = log.New(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
24 changes: 13 additions & 11 deletions common/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ import (
"sync"
"sync/atomic"

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

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

"github.com/chromedp/cdproto"
cdpbrowser "github.com/chromedp/cdproto/browser"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/target"
"github.com/dop251/goja"
"github.com/gorilla/websocket"
k6modules "go.k6.io/k6/js/modules"

"github.com/grafana/xk6-browser/api"
"github.com/grafana/xk6-browser/k6"
"github.com/grafana/xk6-browser/log"
)

Expand Down Expand Up @@ -119,7 +121,7 @@ func newBrowser(
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 @@ -259,7 +261,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 @@ -298,7 +300,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 @@ -413,7 +415,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 @@ -450,12 +452,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 @@ -476,7 +478,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 @@ -506,7 +508,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 @@ -516,7 +518,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"
"github.com/grafana/xk6-browser/log"

k6modules "go.k6.io/k6/js/modules"
Expand Down Expand Up @@ -72,7 +72,7 @@ func NewBrowserContext(
id: id,
opts: opts,
logger: logger,
vu: k6.GetVU(ctx),
vu: k6ext.GetVU(ctx),
timeoutSettings: NewTimeoutSettings(nil),
}

Expand All @@ -84,7 +84,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 @@ -134,7 +134,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 @@ -144,7 +144,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 @@ -153,24 +153,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 @@ -214,13 +214,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 @@ -230,7 +230,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 @@ -258,7 +258,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 @@ -276,7 +276,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 @@ -285,13 +285,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 @@ -309,7 +309,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 @@ -329,11 +329,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 @@ -355,7 +355,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 @@ -364,7 +364,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