Skip to content

Commit

Permalink
rename ElementMatches to ElementR
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Sep 14, 2020
1 parent fe92941 commit 6356338
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion dev_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (p *Page) Overlay(left, top, width, height float64, msg string) (remove fun
}

// ExposeJSHelper to page's window object, so you can debug helper.js in the browser console.
// Such as run `rod.elementMatches("div", "ok")` in the browser console to test the Page.ElementMatches.
// Such as run `rod.elementR("div", "ok")` in the browser console to test the Page.ElementR.
func (p *Page) ExposeJSHelper() *Page {
p.MustEval(`rod => window.rod = rod`, proto.RuntimeRemoteObjectID(""))
return p
Expand Down
2 changes: 1 addition & 1 deletion examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func Example_wait_for_animation() {

page.MustWaitLoad().MustElement("[data-target='#exampleModalLive']").MustClick()

saveBtn := page.MustElementMatches("#exampleModalLive button", "Close")
saveBtn := page.MustElementR("#exampleModalLive button", "Close")

// Here, WaitStable will wait until the save button's position becomes
// stable. The timeout is 5 seconds, after which it times out (or after 1
Expand Down
2 changes: 1 addition & 1 deletion hijack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (s *S) TestHandleAuth() {

page := s.browser.MustPage(url)
defer page.MustClose()
page.MustElementMatches("p", "ok")
page.MustElementR("p", "ok")

wait := s.browser.HandleAuth("a", "b")
go func() { _, _ = s.browser.Page(url + "/err") }()
Expand Down
2 changes: 1 addition & 1 deletion lib/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const Helper = `() => {
return list
},
elementMatches(...pairs) {
elementR(...pairs) {
for (let i = 0; i < pairs.length - 1; i += 2) {
const selector = pairs[i]
const pattern = pairs[i + 1]
Expand Down
2 changes: 1 addition & 1 deletion lib/assets/helper.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/assets/js/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const (
ElementX NameType = "elementX"
//ElementsX NameType function name
ElementsX NameType = "elementsX"
//ElementMatches NameType function name
ElementMatches NameType = "elementMatches"
//ElementR NameType function name
ElementR NameType = "elementR"
//Parents NameType function name
Parents NameType = "parents"
//ContainsElement NameType function name
Expand Down
2 changes: 1 addition & 1 deletion lib/examples/compare-chromedp/logic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func main() {
page := rod.New().MustConnect().Timeout(time.Second * 15).MustPage("https://github.com/avelino/awesome-go")

section := page.MustElementMatches("p", "Selenium and browser control tools").MustNext()
section := page.MustElementR("p", "Selenium and browser control tools").MustNext()

// query children elements of an element
projects := section.MustElements("li")
Expand Down
2 changes: 1 addition & 1 deletion lib/examples/compare-chromedp/submit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {

page.MustElement(`input[name=q]`).MustWaitVisible().MustInput("chromedp").MustPress(input.Enter)

res := page.MustElementMatches("a", "chromedp").MustParent().MustNext().MustText()
res := page.MustElementR("a", "chromedp").MustParent().MustNext().MustText()

log.Printf("got: `%s`", strings.TrimSpace(res))
}
20 changes: 10 additions & 10 deletions must.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,11 @@ func (p *Page) MustElement(selectors ...string) *Element {
return el
}

// MustElementMatches retries until an element in the page that matches one of the pairs.
// Each pairs is a css selector and a regex. A sample call will look like page.MustElementMatches("div", "click me").
// MustElementR retries until an element in the page that matches one of the pairs.
// Each pairs is a css selector and a regex. A sample call will look like page.MustElementR("div", "click me").
// The regex is the js regex, not golang's.
func (p *Page) MustElementMatches(pairs ...string) *Element {
el, err := p.ElementMatches(pairs...)
func (p *Page) MustElementR(pairs ...string) *Element {
el, err := p.ElementR(pairs...)
utils.E(err)
return el
}
Expand Down Expand Up @@ -466,9 +466,9 @@ func (rc *RaceContext) MustElementX(selector string, callback func(*Element)) *R
})
}

// MustElementMatches the doc is similar with MustElement but has a callback when a match is found
func (rc *RaceContext) MustElementMatches(selector, regex string, callback func(*Element)) *RaceContext {
return rc.ElementMatches(selector, regex, func(el *Element) error {
// MustElementR the doc is similar with MustElement but has a callback when a match is found
func (rc *RaceContext) MustElementR(selector, regex string, callback func(*Element)) *RaceContext {
return rc.ElementR(selector, regex, func(el *Element) error {
callback(el)
return nil
})
Expand Down Expand Up @@ -843,10 +843,10 @@ func (el *Element) MustPrevious() *Element {
return parent
}

// MustElementMatches returns the first element in the page that matches the CSS selector and its text matches the regex.
// MustElementR returns the first element in the page that matches the CSS selector and its text matches the regex.
// The regex is the js regex, not golang's.
func (el *Element) MustElementMatches(selector, regex string) *Element {
el, err := el.ElementMatches(selector, regex)
func (el *Element) MustElementR(selector, regex string) *Element {
el, err := el.ElementR(selector, regex)
utils.E(err)
return el
}
Expand Down
22 changes: 11 additions & 11 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (p *Page) HasX(selectors ...string) (bool, *Element, error) {

// HasMatches doc is similar to the method MustHasMatches
func (p *Page) HasMatches(pairs ...string) (bool, *Element, error) {
el, err := p.Sleeper(nil).ElementMatches(pairs...)
el, err := p.Sleeper(nil).ElementR(pairs...)
if errors.Is(err, ErrElementNotFound) {
return false, nil, nil
}
Expand All @@ -122,9 +122,9 @@ func (p *Page) Element(selectors ...string) (*Element, error) {
return p.ElementByJS(jsHelper(js.Element, JSArgsFromString(selectors)))
}

// ElementMatches doc is similar to the method MustElementMatches
func (p *Page) ElementMatches(pairs ...string) (*Element, error) {
return p.ElementByJS(jsHelper(js.ElementMatches, JSArgsFromString(pairs)))
// ElementR doc is similar to the method MustElementR
func (p *Page) ElementR(pairs ...string) (*Element, error) {
return p.ElementByJS(jsHelper(js.ElementR, JSArgsFromString(pairs)))
}

// ElementX finds elements by XPath
Expand Down Expand Up @@ -338,10 +338,10 @@ func (rc *RaceContext) ElementX(selector string, callback func(*Element) error)
return rc
}

// ElementMatches the doc is similar with ElementMatches but has a callback when a match is found
func (rc *RaceContext) ElementMatches(selector, regex string, callback func(*Element) error) *RaceContext {
// ElementR the doc is similar with ElementR but has a callback when a match is found
func (rc *RaceContext) ElementR(selector, regex string, callback func(*Element) error) *RaceContext {
rc.branches = append(rc.branches, &raceBranch{
func() (*Element, error) { return rc.noSleepPage.ElementMatches(selector, regex) },
func() (*Element, error) { return rc.noSleepPage.ElementR(selector, regex) },
callback,
})
return rc
Expand Down Expand Up @@ -391,7 +391,7 @@ func (el *Element) HasX(selector string) (bool, *Element, error) {

// HasMatches doc is similar to the method MustHasMatches
func (el *Element) HasMatches(selector, regex string) (bool, *Element, error) {
el, err := el.ElementMatches(selector, regex)
el, err := el.ElementR(selector, regex)
if errors.Is(err, ErrElementNotFound) {
return false, nil, nil
}
Expand Down Expand Up @@ -433,9 +433,9 @@ func (el *Element) Previous() (*Element, error) {
return el.ElementByJS(NewEvalOptions(`this.previousElementSibling`, nil))
}

// ElementMatches doc is similar to the method MustElementMatches
func (el *Element) ElementMatches(pairs ...string) (*Element, error) {
return el.ElementByJS(jsHelper(js.ElementMatches, JSArgsFromString(pairs)))
// ElementR doc is similar to the method MustElementR
func (el *Element) ElementR(pairs ...string) (*Element, error) {
return el.ElementByJS(jsHelper(js.ElementR, JSArgsFromString(pairs)))
}

// Elements doc is similar to the method MustElements
Expand Down
10 changes: 5 additions & 5 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (s *S) TestPageRace() {
err := p.Sleeper(func() utils.Sleeper { return utils.CountSleeper(2) }).Race().
MustElement("not-exists", func(el *rod.Element) {}).
MustElementX("//not-exists", func(el *rod.Element) {}).
MustElementMatches("not-exists", "test", func(el *rod.Element) {}).
MustElementR("not-exists", "test", func(el *rod.Element) {}).
Do()
s.Error(err)

Expand All @@ -163,16 +163,16 @@ func (s *S) TestPageElementsX() {
s.Len(list, 5)
}

func (s *S) TestElementMatches() {
func (s *S) TestElementR() {
p := s.page.MustNavigate(srcFile("fixtures/selector.html"))
el := p.MustElementMatches("button", `\d1`)
el := p.MustElementR("button", `\d1`)
s.Equal("01", el.MustText())

el = p.MustElement("div").MustElementMatches("button", `03`)
el = p.MustElement("div").MustElementR("button", `03`)
s.Equal("03", el.MustText())

p = s.page.MustNavigate(srcFile("fixtures/input.html"))
el = p.MustElementMatches("input", `submit`)
el = p.MustElementR("input", `submit`)
s.Equal("submit", el.MustText())
}

Expand Down

0 comments on commit 6356338

Please sign in to comment.