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

Commit

Permalink
Add basic locator support
Browse files Browse the repository at this point in the history
We now support creating a locator using one of page.locator and
frame.locator. This commit adds a simple unit test to verify whether we
can create locators using the mentioned methods. However, the test is
not that useful, we can remove it later when we add integration tests.
  • Loading branch information
inancgumus committed May 17, 2022
1 parent 0708178 commit 8f880f7
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type Frame interface {
IsVisible(selector string, opts goja.Value) bool
ID() string
LoaderID() string
// Locator creates and returns a new locator for this frame.
Locator(selector string, opts goja.Value) Locator
Name() string
Query(selector string) ElementHandle
QueryAll(selector string) []ElementHandle
Expand Down
4 changes: 4 additions & 0 deletions api/locator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package api

// Locator represents a way to find element(s) on a page at any moment.
type Locator interface{}
2 changes: 2 additions & 0 deletions api/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type Page interface {
IsEnabled(selector string, opts goja.Value) bool
IsHidden(selector string, opts goja.Value) bool
IsVisible(selector string, opts goja.Value) bool
// Locator creates and returns a new locator for this page (main frame).
Locator(selector string, opts goja.Value) Locator
MainFrame() Frame
Opener() Page
Pause()
Expand Down
7 changes: 7 additions & 0 deletions common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,13 @@ func (f *Frame) ID() string {
return f.id.String()
}

// Locator creates and returns a new locator for this frame.
func (f *Frame) Locator(selector string, opts goja.Value) api.Locator {
f.log.Debugf("Frame:Locator", "fid:%s furl:%q selector:%q opts:%+v", f.ID(), f.URL(), selector, opts)

return NewLocator(f.ctx, selector, f, f.log)
}

// LoaderID returns the ID of the frame that loaded this frame.
func (f *Frame) LoaderID() string {
f.propertiesMu.RLock()
Expand Down
25 changes: 25 additions & 0 deletions common/locator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package common

import (
"context"
)

// Locator represent a way to find element(s) on the page at any moment.
type Locator struct {
selector string

frame *Frame

ctx context.Context
log *Logger
}

// NewLocator creates and returns a new locator.
func NewLocator(ctx context.Context, selector string, f *Frame, l *Logger) *Locator {
return &Locator{
selector: selector,
frame: f,
ctx: ctx,
log: l,
}
}
7 changes: 7 additions & 0 deletions common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,13 @@ func (p *Page) IsVisible(selector string, opts goja.Value) bool {
return p.MainFrame().IsVisible(selector, opts)
}

// Locator creates and returns a new locator for this page (main frame).
func (p *Page) Locator(selector string, opts goja.Value) api.Locator {
p.logger.Debugf("Page:Locator", "sid:%s sel: %q opts:%+v", p.sessionID(), selector, opts)

return p.MainFrame().Locator(selector, opts)
}

// MainFrame returns the main frame on the page.
func (p *Page) MainFrame() api.Frame {
mf := p.frameManager.MainFrame()
Expand Down
31 changes: 31 additions & 0 deletions common/page_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package common

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

// TestPageLocator can be removed later on when we add integration
// tests. Since we don't yet have any of them, it makes sense to keep
// this test.
func TestPageLocator(t *testing.T) {
const (
wantMainFrameID = "1"
wantSelector = "span"
)
ctx := context.TODO()
p := &Page{
ctx: ctx,
frameManager: &FrameManager{
ctx: ctx,
mainFrame: &Frame{id: wantMainFrameID, ctx: ctx},
},
}
l := p.Locator(wantSelector, nil).(*Locator) //nolint:forcetypeassert
assert.Equal(t, wantSelector, l.selector)
assert.Equal(t, wantMainFrameID, string(l.frame.id))

// other behavior will be tested via integration tests
}

0 comments on commit 8f880f7

Please sign in to comment.