Skip to content

Commit

Permalink
docs: 简化示例代码 等待元素 的操作 (waitForElement)
Browse files Browse the repository at this point in the history
  • Loading branch information
electricbubble committed Aug 28, 2020
1 parent 91d2a9d commit 0c8bff0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
36 changes: 24 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ go get -u github.com/electricbubble/guia2
package main
import (
"fmt"
"github.com/electricbubble/guia2"
"io/ioutil"
"log"
Expand Down Expand Up @@ -101,15 +102,7 @@ func main() {
checkErr(err)
bySelector := guia2.BySelector{UiAutomator: guia2.NewUiSelectorHelper().Focused(true).String()}
exists := func(d *guia2.Driver) (bool, error) {
element, err = d.FindElement(bySelector)
if err == nil {
return true, nil
}
return false, nil
}
err = driver.Wait(exists)
element, err = waitForElement(driver, bySelector)
checkErr(err)
err = element.SendKeys("雾山五行")
Expand All @@ -119,15 +112,18 @@ func main() {
checkErr(err)
bySelector = guia2.BySelector{UiAutomator: guia2.NewUiSelectorHelper().TextStartsWith("番剧").String()}
checkErr(driver.Wait(exists))
element, err = waitForElement(driver, bySelector)
checkErr(err)
checkErr(element.Click())
bySelector = guia2.BySelector{UiAutomator: guia2.NewUiSelectorHelper().Text("立即观看").String()}
checkErr(driver.Wait(exists))
element, err = waitForElement(driver, bySelector)
checkErr(err)
checkErr(element.Click())
bySelector = guia2.BySelector{ResourceIdID: "tv.danmaku.bili:id/videoview_container_space"}
checkErr(driver.Wait(exists))
element, err = waitForElement(driver, bySelector)
checkErr(err)
// time.Sleep(time.Second * 5)
Expand All @@ -143,6 +139,22 @@ func main() {
checkErr(err)
}
func waitForElement(driver *guia2.Driver, bySelector guia2.BySelector) (element *guia2.Element, err error) {
var ce error
exists := func(d *guia2.Driver) (bool, error) {
element, ce = d.FindElement(bySelector)
if ce == nil {
return true, nil
}
// 如果直接返回 error 将直接终止 `driver.Wait`
return false, nil
}
if err = driver.Wait(exists); err != nil {
return nil, fmt.Errorf("%s: %w", err.Error(), ce)
}
return
}
func checkErr(err error, msg ...string) {
if err == nil {
return
Expand Down
36 changes: 24 additions & 12 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"github.com/electricbubble/guia2"
"io/ioutil"
"log"
Expand Down Expand Up @@ -41,15 +42,7 @@ func main() {
checkErr(err)

bySelector := guia2.BySelector{UiAutomator: guia2.NewUiSelectorHelper().Focused(true).String()}
exists := func(d *guia2.Driver) (bool, error) {
element, err = d.FindElement(bySelector)
if err == nil {
return true, nil
}
return false, nil
}

err = driver.Wait(exists)
element, err = waitForElement(driver, bySelector)
checkErr(err)

err = element.SendKeys("雾山五行")
Expand All @@ -59,15 +52,18 @@ func main() {
checkErr(err)

bySelector = guia2.BySelector{UiAutomator: guia2.NewUiSelectorHelper().TextStartsWith("番剧").String()}
checkErr(driver.Wait(exists))
element, err = waitForElement(driver, bySelector)
checkErr(err)
checkErr(element.Click())

bySelector = guia2.BySelector{UiAutomator: guia2.NewUiSelectorHelper().Text("立即观看").String()}
checkErr(driver.Wait(exists))
element, err = waitForElement(driver, bySelector)
checkErr(err)
checkErr(element.Click())

bySelector = guia2.BySelector{ResourceIdID: "tv.danmaku.bili:id/videoview_container_space"}
checkErr(driver.Wait(exists))
element, err = waitForElement(driver, bySelector)
checkErr(err)

// time.Sleep(time.Second * 5)

Expand All @@ -83,6 +79,22 @@ func main() {
checkErr(err)
}

func waitForElement(driver *guia2.Driver, bySelector guia2.BySelector) (element *guia2.Element, err error) {
var ce error
exists := func(d *guia2.Driver) (bool, error) {
element, ce = d.FindElement(bySelector)
if ce == nil {
return true, nil
}
// 如果直接返回 error 将直接终止 `driver.Wait`
return false, nil
}
if err = driver.Wait(exists); err != nil {
return nil, fmt.Errorf("%s: %w", err.Error(), ce)
}
return
}

func checkErr(err error, msg ...string) {
if err == nil {
return
Expand Down

0 comments on commit 0c8bff0

Please sign in to comment.