Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Move all not implemented APIs to return promises (#586)
Browse files Browse the repository at this point in the history
Updates #428
  • Loading branch information
mstoykov authored Oct 13, 2022
1 parent cbb74af commit c9fe0cf
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 71 deletions.
18 changes: 9 additions & 9 deletions api/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ import (

// BrowserContext is the public interface of a CDP browser context.
type BrowserContext interface {
AddCookies(cookies goja.Value)
AddCookies(cookies goja.Value) *goja.Promise
AddInitScript(script goja.Value, arg goja.Value)
Browser() Browser
ClearCookies()
ClearPermissions()
Close()
Cookies() []goja.Object
ExposeBinding(name string, callback goja.Callable, opts goja.Value)
ExposeFunction(name string, callback goja.Callable)
Cookies() *goja.Promise
ExposeBinding(name string, callback goja.Callable, opts goja.Value) *goja.Promise
ExposeFunction(name string, callback goja.Callable) *goja.Promise
GrantPermissions(permissions []string, opts goja.Value)
NewCDPSession() CDPSession
NewCDPSession() *goja.Promise
NewPage() Page
Pages() []Page
Route(url goja.Value, handler goja.Callable)
Route(url goja.Value, handler goja.Callable) *goja.Promise
SetDefaultNavigationTimeout(timeout int64)
SetDefaultTimeout(timeout int64)
SetExtraHTTPHeaders(headers map[string]string)
SetExtraHTTPHeaders(headers map[string]string) *goja.Promise
SetGeolocation(geolocation goja.Value)
// SetHTTPCredentials sets username/password credentials to use for HTTP authentication.
//
Expand All @@ -52,7 +52,7 @@ type BrowserContext interface {
// - https://github.com/microsoft/playwright/pull/2763
SetHTTPCredentials(httpCredentials goja.Value)
SetOffline(offline bool)
StorageState(opts goja.Value)
Unroute(url goja.Value, handler goja.Callable)
StorageState(opts goja.Value) *goja.Promise
Unroute(url goja.Value, handler goja.Callable) *goja.Promise
WaitForEvent(event string, optsOrPredicate goja.Value) interface{}
}
4 changes: 2 additions & 2 deletions api/browser_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (

// BrowserType is the public interface of a CDP browser client.
type BrowserType interface {
Connect(opts goja.Value)
Connect(opts goja.Value) *goja.Promise
ExecutablePath() string
Launch(opts goja.Value) Browser
LaunchPersistentContext(userDataDir string, opts goja.Value) Browser
LaunchPersistentContext(userDataDir string, opts goja.Value) *goja.Promise
Name() string
}
2 changes: 1 addition & 1 deletion api/element_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type ElementHandle interface {
ScrollIntoViewIfNeeded(opts goja.Value)
SelectOption(values goja.Value, opts goja.Value) []string
SelectText(opts goja.Value)
SetInputFiles(files goja.Value, opts goja.Value)
SetInputFiles(files goja.Value, opts goja.Value) *goja.Promise
Tap(opts goja.Value)
TextContent() string
Type(text string, opts goja.Value)
Expand Down
6 changes: 3 additions & 3 deletions api/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import "github.com/dop251/goja"

// Frame is the interface of a CDP target frame.
type Frame interface {
AddScriptTag(opts goja.Value)
AddStyleTag(opts goja.Value)
AddScriptTag(opts goja.Value) *goja.Promise
AddStyleTag(opts goja.Value) *goja.Promise
Check(selector string, opts goja.Value)
ChildFrames() []Frame
Click(selector string, opts goja.Value) *goja.Promise
Expand Down Expand Up @@ -42,7 +42,7 @@ type Frame interface {
Press(selector string, key string, opts goja.Value)
SelectOption(selector string, values goja.Value, opts goja.Value) []string
SetContent(html string, opts goja.Value)
SetInputFiles(selector string, files goja.Value, opts goja.Value)
SetInputFiles(selector string, files goja.Value, opts goja.Value) *goja.Promise
Tap(selector string, opts goja.Value)
TextContent(selector string, opts goja.Value) string
Title() string
Expand Down
36 changes: 18 additions & 18 deletions api/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import "github.com/dop251/goja"

// Page is the interface of a single browser tab.
type Page interface {
AddInitScript(script goja.Value, arg goja.Value)
AddScriptTag(opts goja.Value)
AddStyleTag(opts goja.Value)
AddInitScript(script goja.Value, arg goja.Value) *goja.Promise
AddScriptTag(opts goja.Value) *goja.Promise
AddStyleTag(opts goja.Value) *goja.Promise
BringToFront()
Check(selector string, opts goja.Value)
Click(selector string, opts goja.Value) *goja.Promise
Expand All @@ -35,20 +35,20 @@ type Page interface {
Context() BrowserContext
Dblclick(selector string, opts goja.Value)
DispatchEvent(selector string, typ string, eventInit goja.Value, opts goja.Value)
DragAndDrop(source string, target string, opts goja.Value)
DragAndDrop(source string, target string, opts goja.Value) *goja.Promise
EmulateMedia(opts goja.Value)
EmulateVisionDeficiency(typ string)
Evaluate(pageFunc goja.Value, arg ...goja.Value) interface{}
EvaluateHandle(pageFunc goja.Value, arg ...goja.Value) JSHandle
ExposeBinding(name string, callback goja.Callable, opts goja.Value)
ExposeFunction(name string, callback goja.Callable)
ExposeBinding(name string, callback goja.Callable, opts goja.Value) *goja.Promise
ExposeFunction(name string, callback goja.Callable) *goja.Promise
Fill(selector string, value string, opts goja.Value)
Focus(selector string, opts goja.Value)
Frame(frameSelector goja.Value) Frame
Frame(frameSelector goja.Value) *goja.Promise
Frames() []Frame
GetAttribute(selector string, name string, opts goja.Value) goja.Value
GoBack(opts goja.Value) Response
GoForward(opts goja.Value) Response
GoBack(opts goja.Value) *goja.Promise
GoForward(opts goja.Value) *goja.Promise
Goto(url string, opts goja.Value) *goja.Promise
Hover(selector string, opts goja.Value)
InnerHTML(selector string, opts goja.Value) string
Expand All @@ -65,36 +65,36 @@ type Page interface {
Locator(selector string, opts goja.Value) Locator
MainFrame() Frame
Opener() Page
Pause()
Pdf(opts goja.Value) goja.ArrayBuffer
Pause() *goja.Promise
Pdf(opts goja.Value) *goja.Promise
Press(selector string, key string, opts goja.Value)
Query(selector string) ElementHandle
QueryAll(selector string) []ElementHandle
Reload(opts goja.Value) Response
Route(url goja.Value, handler goja.Callable)
Route(url goja.Value, handler goja.Callable) *goja.Promise
Screenshot(opts goja.Value) goja.ArrayBuffer
SelectOption(selector string, values goja.Value, opts goja.Value) []string
SetContent(html string, opts goja.Value)
SetDefaultNavigationTimeout(timeout int64)
SetDefaultTimeout(timeout int64)
SetExtraHTTPHeaders(headers map[string]string)
SetInputFiles(selector string, files goja.Value, opts goja.Value)
SetInputFiles(selector string, files goja.Value, opts goja.Value) *goja.Promise
SetViewportSize(viewportSize goja.Value)
Tap(selector string, opts goja.Value)
TextContent(selector string, opts goja.Value) string
Title() string
Type(selector string, text string, opts goja.Value)
Uncheck(selector string, opts goja.Value)
Unroute(url goja.Value, handler goja.Callable)
Unroute(url goja.Value, handler goja.Callable) *goja.Promise
URL() string
Video() Video
Video() *goja.Promise
ViewportSize() map[string]float64
WaitForEvent(event string, optsOrPredicate goja.Value) interface{}
WaitForEvent(event string, optsOrPredicate goja.Value) *goja.Promise
WaitForFunction(fn, opts goja.Value, args ...goja.Value) *goja.Promise
WaitForLoadState(state string, opts goja.Value)
WaitForNavigation(opts goja.Value) *goja.Promise
WaitForRequest(urlOrPredicate, opts goja.Value) Request
WaitForResponse(urlOrPredicate, opts goja.Value) Response
WaitForRequest(urlOrPredicate, opts goja.Value) *goja.Promise
WaitForResponse(urlOrPredicate, opts goja.Value) *goja.Promise
WaitForSelector(selector string, opts goja.Value) ElementHandle
WaitForTimeout(timeout int64)
Workers() []Worker
Expand Down
2 changes: 1 addition & 1 deletion api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import "github.com/dop251/goja"
type Response interface {
AllHeaders() map[string]string
Body() goja.ArrayBuffer
Finished() bool // TODO: should return nil|Error
Finished() *goja.Promise
Frame() Frame
HeaderValue(string) goja.Value
HeaderValues(string) []string
Expand Down
5 changes: 3 additions & 2 deletions chromium/browser_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ func NewBrowserType(vu k6modules.VU) api.BrowserType {
}

// Connect attaches k6 browser to an existing browser instance.
func (b *BrowserType) Connect(opts goja.Value) {
func (b *BrowserType) Connect(opts goja.Value) *goja.Promise {
rt := b.vu.Runtime()
k6common.Throw(rt, errors.New("BrowserType.connect() has not been implemented yet"))
return nil
}

// ExecutablePath returns the path where the extension expects to find the browser executable.
Expand Down Expand Up @@ -197,7 +198,7 @@ func (b *BrowserType) launch(ctx context.Context, opts *common.LaunchOptions) (*
}

// LaunchPersistentContext launches the browser with persistent storage.
func (b *BrowserType) LaunchPersistentContext(userDataDir string, opts goja.Value) api.Browser {
func (b *BrowserType) LaunchPersistentContext(userDataDir string, opts goja.Value) *goja.Promise {
rt := b.vu.Runtime()
k6common.Throw(rt, errors.New("BrowserType.LaunchPersistentContext(userDataDir, opts) has not been implemented yet"))
return nil
Expand Down
33 changes: 24 additions & 9 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ func NewBrowserContext(
return &b
}

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

// AddInitScript adds a script that will be initialized on all new pages.
Expand Down Expand Up @@ -160,17 +162,22 @@ func (b *BrowserContext) Close() {
}
}

func (b *BrowserContext) Cookies() []goja.Object {
// Cookies is not implemented.
func (b *BrowserContext) Cookies() *goja.Promise {
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) {
// ExposeBinding is not implemented.
func (b *BrowserContext) ExposeBinding(name string, callback goja.Callable, opts goja.Value) *goja.Promise {
k6ext.Panic(b.ctx, "BrowserContext.exposeBinding(name, callback, opts) has not been implemented yet")
return nil
}

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

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

// NewCDPSession returns a new CDP session attached to this target.
func (b *BrowserContext) NewCDPSession() api.CDPSession {
func (b *BrowserContext) NewCDPSession() *goja.Promise {
k6ext.Panic(b.ctx, "BrowserContext.newCDPSession() has not been implemented yet")
return nil
}
Expand Down Expand Up @@ -257,8 +264,10 @@ func (b *BrowserContext) Pages() []api.Page {
return pages
}

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

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

func (b *BrowserContext) SetExtraHTTPHeaders(headers map[string]string) {
// SetExtraHTTPHeaders is not implemented.
func (b *BrowserContext) SetExtraHTTPHeaders(headers map[string]string) *goja.Promise {
k6ext.Panic(b.ctx, "BrowserContext.setExtraHTTPHeaders(headers) has not been implemented yet")
return nil
}

// SetGeolocation overrides the geo location of the user.
Expand Down Expand Up @@ -328,12 +339,16 @@ func (b *BrowserContext) SetOffline(offline bool) {
}
}

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

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

func (b *BrowserContext) WaitForEvent(event string, optsOrPredicate goja.Value) interface{} {
Expand Down
4 changes: 3 additions & 1 deletion common/element_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -1233,9 +1233,11 @@ func (h *ElementHandle) SelectText(opts goja.Value) {
applySlowMo(h.ctx)
}

func (h *ElementHandle) SetInputFiles(files goja.Value, opts goja.Value) {
// SetInputFiles is not implemented.
func (h *ElementHandle) SetInputFiles(files goja.Value, opts goja.Value) *goja.Promise {
// TODO: implement
k6ext.Panic(h.ctx, "ElementHandle.setInputFiles() has not been implemented yet")
return nil
}

func (h *ElementHandle) Tap(opts goja.Value) {
Expand Down
12 changes: 9 additions & 3 deletions common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,14 +668,18 @@ func (f *Frame) waitForSelector(selector string, opts *FrameWaitForSelectorOptio
return handle, nil
}

func (f *Frame) AddScriptTag(opts goja.Value) {
// AddScriptTag is not implemented.
func (f *Frame) AddScriptTag(opts goja.Value) *goja.Promise {
k6ext.Panic(f.ctx, "Frame.AddScriptTag() has not been implemented yet")
applySlowMo(f.ctx)
return nil
}

func (f *Frame) AddStyleTag(opts goja.Value) {
// AddStyleTag is not implemented.
func (f *Frame) AddStyleTag(opts goja.Value) *goja.Promise {
k6ext.Panic(f.ctx, "Frame.AddStyleTag() has not been implemented yet")
applySlowMo(f.ctx)
return nil
}

// ChildFrames returns a list of child frames.
Expand Down Expand Up @@ -1627,8 +1631,10 @@ func (f *Frame) SetContent(html string, opts goja.Value) {
applySlowMo(f.ctx)
}

func (f *Frame) SetInputFiles(selector string, files goja.Value, opts goja.Value) {
// SetInputFiles is not implemented.
func (f *Frame) SetInputFiles(selector string, files goja.Value, opts goja.Value) *goja.Promise {
k6ext.Panic(f.ctx, "Frame.setInputFiles(selector, files, opts) has not been implemented yet")
return nil
// TODO: needs slowMo
}

Expand Down
Loading

0 comments on commit c9fe0cf

Please sign in to comment.