Skip to content

Commit

Permalink
Refactor page.url to return err
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed May 23, 2024
1 parent e4b93fc commit 7ca6b73
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 26 deletions.
2 changes: 1 addition & 1 deletion browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ type pageAPI interface {
Title() (string, error)
Type(selector string, text string, opts goja.Value)
Uncheck(selector string, opts goja.Value)
URL() string
URL() (string, error)
ViewportSize() map[string]float64
WaitForFunction(fn, opts goja.Value, args ...goja.Value) (any, error)
WaitForLoadState(state string, opts goja.Value)
Expand Down
17 changes: 11 additions & 6 deletions common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -1231,15 +1231,20 @@ func (p *Page) Type(selector string, text string, opts goja.Value) {
}

// URL returns the location of the page.
func (p *Page) URL() string {
func (p *Page) URL() (string, error) {
p.logger.Debugf("Page:URL", "sid:%v", p.sessionID())

// TODO: return error

script := `() => document.location.toString()`
v, _ := p.Evaluate(script)
js := `() => document.location.toString()`
v, err := p.Evaluate(js)
if err != nil {
return "", fmt.Errorf("getting page URL: %w", err)
}
s, ok := v.(string)
if !ok {
return "", fmt.Errorf("getting page URL: expected string, got %T", v)
}

return v.(string) //nolint:forcetypeassert
return s, nil
}

// ViewportSize will return information on the viewport width and height.
Expand Down
76 changes: 57 additions & 19 deletions tests/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,9 @@ func TestPageURL(t *testing.T) {
b := newTestBrowser(t, withHTTPServer())

p := b.NewPage(nil)
assert.Equal(t, common.BlankPage, p.URL())
uri, err := p.URL()
require.NoError(t, err)
assert.Equal(t, common.BlankPage, uri)

opts := &common.FrameGotoOptions{
Timeout: common.DefaultTimeout,
Expand All @@ -880,7 +882,9 @@ func TestPageURL(t *testing.T) {
)
require.NoError(t, err)
require.NotNil(t, resp)
assert.Regexp(t, "http://.*/get", p.URL())
uri, err = p.URL()
require.NoError(t, err)
assert.Regexp(t, "http://.*/get", uri)
}

func TestPageClose(t *testing.T) {
Expand Down Expand Up @@ -932,7 +936,9 @@ func TestPageOn(t *testing.T) {
val, err := cm.Args[0].JSONValue()
assert.NoError(t, err)
assert.Equal(t, "this is a log message", val)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
uri, err := cm.Page.URL()
require.NoError(t, err)
assert.True(t, uri == blankPage, "url is not %s", blankPage)
},
},
{
Expand All @@ -945,7 +951,9 @@ func TestPageOn(t *testing.T) {
val, err := cm.Args[0].JSONValue()
assert.NoError(t, err)
assert.Equal(t, "this is a debug message", val)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
uri, err := cm.Page.URL()
require.NoError(t, err)
assert.True(t, uri == blankPage, "url is not %s", blankPage)
},
},
{
Expand All @@ -958,7 +966,9 @@ func TestPageOn(t *testing.T) {
val, err := cm.Args[0].JSONValue()
assert.NoError(t, err)
assert.Equal(t, "this is an info message", val)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
uri, err := cm.Page.URL()
require.NoError(t, err)
assert.True(t, uri == blankPage, "url is not %s", blankPage)
},
},
{
Expand All @@ -971,7 +981,9 @@ func TestPageOn(t *testing.T) {
val, err := cm.Args[0].JSONValue()
assert.NoError(t, err)
assert.Equal(t, "this is an error message", val)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
uri, err := cm.Page.URL()
require.NoError(t, err)
assert.True(t, uri == blankPage, "url is not %s", blankPage)
},
},
{
Expand All @@ -984,7 +996,9 @@ func TestPageOn(t *testing.T) {
val, err := cm.Args[0].JSONValue()
assert.NoError(t, err)
assert.Equal(t, "this is a warning message", val)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
uri, err := cm.Page.URL()
require.NoError(t, err)
assert.True(t, uri == blankPage, "url is not %s", blankPage)
},
},
{
Expand All @@ -994,7 +1008,9 @@ func TestPageOn(t *testing.T) {
t.Helper()
assert.Equal(t, "dir", cm.Type)
assert.Equal(t, "Location", cm.Text)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
uri, err := cm.Page.URL()
require.NoError(t, err)
assert.True(t, uri == blankPage, "url is not %s", blankPage)
},
},
{
Expand All @@ -1004,7 +1020,9 @@ func TestPageOn(t *testing.T) {
t.Helper()
assert.Equal(t, "dirxml", cm.Type)
assert.Equal(t, "Location", cm.Text)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
uri, err := cm.Page.URL()
require.NoError(t, err)
assert.True(t, uri == blankPage, "url is not %s", blankPage)
},
},
{
Expand All @@ -1017,7 +1035,9 @@ func TestPageOn(t *testing.T) {
val, err := cm.Args[0].JSONValue()
assert.NoError(t, err)
assert.Equal(t, `[["Grafana","k6"],["Grafana","Mimir"]]`, val)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
uri, err := cm.Page.URL()
require.NoError(t, err)
assert.True(t, uri == blankPage, "url is not %s", blankPage)
},
},
{
Expand All @@ -1030,7 +1050,9 @@ func TestPageOn(t *testing.T) {
val, err := cm.Args[0].JSONValue()
assert.NoError(t, err)
assert.Equal(t, "trace example", val)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
uri, err := cm.Page.URL()
require.NoError(t, err)
assert.True(t, uri == blankPage, "url is not %s", blankPage)
},
},
{
Expand All @@ -1039,7 +1061,9 @@ func TestPageOn(t *testing.T) {
assertFn: func(t *testing.T, cm *common.ConsoleMessage) {
t.Helper()
assert.Equal(t, "clear", cm.Type)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
uri, err := cm.Page.URL()
require.NoError(t, err)
assert.True(t, uri == blankPage, "url is not %s", blankPage)
},
},
{
Expand All @@ -1049,7 +1073,9 @@ func TestPageOn(t *testing.T) {
t.Helper()
assert.Equal(t, "startGroup", cm.Type)
assert.Equal(t, "console.group", cm.Text)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
uri, err := cm.Page.URL()
require.NoError(t, err)
assert.True(t, uri == blankPage, "url is not %s", blankPage)
},
},
{
Expand All @@ -1059,7 +1085,9 @@ func TestPageOn(t *testing.T) {
t.Helper()
assert.Equal(t, "startGroupCollapsed", cm.Type)
assert.Equal(t, "console.groupCollapsed", cm.Text)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
uri, err := cm.Page.URL()
require.NoError(t, err)
assert.True(t, uri == blankPage, "url is not %s", blankPage)
},
},
{
Expand All @@ -1069,7 +1097,9 @@ func TestPageOn(t *testing.T) {
t.Helper()
assert.Equal(t, "endGroup", cm.Type)
assert.Equal(t, "console.groupEnd", cm.Text)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
uri, err := cm.Page.URL()
require.NoError(t, err)
assert.True(t, uri == blankPage, "url is not %s", blankPage)
},
},
{
Expand All @@ -1079,7 +1109,9 @@ func TestPageOn(t *testing.T) {
t.Helper()
assert.Equal(t, "assert", cm.Type)
assert.Equal(t, "console.assert", cm.Text)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
uri, err := cm.Page.URL()
require.NoError(t, err)
assert.True(t, uri == blankPage, "url is not %s", blankPage)
},
},
{
Expand All @@ -1089,7 +1121,9 @@ func TestPageOn(t *testing.T) {
t.Helper()
assert.Equal(t, "count", cm.Type)
assert.Equal(t, "default: 1", cm.Text)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
uri, err := cm.Page.URL()
require.NoError(t, err)
assert.True(t, uri == blankPage, "url is not %s", blankPage)
},
},
{
Expand All @@ -1099,7 +1133,9 @@ func TestPageOn(t *testing.T) {
t.Helper()
assert.Equal(t, "count", cm.Type)
assert.Equal(t, "k6: 1", cm.Text)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
uri, err := cm.Page.URL()
require.NoError(t, err)
assert.True(t, uri == blankPage, "url is not %s", blankPage)
},
},
{
Expand All @@ -1109,7 +1145,9 @@ func TestPageOn(t *testing.T) {
t.Helper()
assert.Equal(t, "timeEnd", cm.Type)
assert.Regexp(t, `^k6: [0-9]+\.[0-9]+`, cm.Text, `expected prefix "k6: <a float>" but got %q`, cm.Text)
assert.True(t, cm.Page.URL() == blankPage, "url is not %s", blankPage)
uri, err := cm.Page.URL()
require.NoError(t, err)
assert.True(t, uri == blankPage, "url is not %s", blankPage)
},
},
}
Expand Down

0 comments on commit 7ca6b73

Please sign in to comment.