Skip to content

Commit

Permalink
add Page.WaitElementsMoreThan/Page.MustWaitElementsMoreThan (go-rod#386)
Browse files Browse the repository at this point in the history
Co-authored-by: Yad Smood <[email protected]>
  • Loading branch information
shuiRong and ysmood authored Apr 10, 2021
1 parent 252ec2a commit a4f7826
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
18 changes: 18 additions & 0 deletions fixtures/wait_elements.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<html>
<body>
<ul>
<li>list 1</li>
<li>list 2</li>
<li>list 3</li>
<li>list 4</li>
<li>list 5</li>
</ul>
<script>
setTimeout(function () {
var li = document.createElement('li')
li.textContent = 'list 6'
document.querySelector('ul').appendChild(li)
}, 1000)
</script>
</body>
</html>
6 changes: 6 additions & 0 deletions must.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,12 @@ func (p *Page) MustWait(js string, params ...interface{}) *Page {
return p
}

// MustWaitElementsMoreThan is similar to Page.WaitElementsMoreThan
func (p *Page) MustWaitElementsMoreThan(selector string, num int) *Page {
utils.E(p.WaitElementsMoreThan(selector, num))
return p
}

// MustObjectToJSON is similar to Page.ObjectToJSON
func (p *Page) MustObjectToJSON(obj *proto.RuntimeRemoteObject) gson.JSON {
j, err := p.ObjectToJSON(obj)
Expand Down
5 changes: 5 additions & 0 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,11 @@ func (p *Page) Wait(this *proto.RuntimeRemoteObject, js string, params []interfa
})
}

// WaitElementsMoreThan Wait until there are more than <num> <selector> elements.
func (p *Page) WaitElementsMoreThan(selector string, num int) error {
return p.Wait(nil, `(s, n) => document.querySelectorAll(s).length > n`, []interface{}{selector, num})
}

// ObjectToJSON by object id
func (p *Page) ObjectToJSON(obj *proto.RuntimeRemoteObject) (gson.JSON, error) {
if obj.ObjectID == "" {
Expand Down
5 changes: 5 additions & 0 deletions page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ func (t T) PageHTML() {
t.Err(p.HTML())
}

func (t T) MustWaitElementsMoreThan() {
p := t.page.MustNavigate(t.srcFile("fixtures/wait_elements.html")).MustWaitElementsMoreThan("li", 5)
t.Gt(len(p.MustElements("li")), 5)
}

func (t T) PageCloseCancel() {
page := t.browser.MustPage(t.srcFile("fixtures/prevent-close.html"))
page.MustElement("body").MustClick() // only focused page will handle beforeunload event
Expand Down

0 comments on commit a4f7826

Please sign in to comment.