Skip to content

Commit

Permalink
feat: 支持启动 App 时, 等待直到某些元素出现
Browse files Browse the repository at this point in the history
  • Loading branch information
electricbubble committed Aug 31, 2020
1 parent 0c8bff0 commit 37b8ea2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ go get -u github.com/electricbubble/guia2
> ```
### `guia2.NewUSBDriver()`
该函数使用期间, `Android` 设备必须一直保持 `USB` 的连接
该函数使用期间, `Android` 设备必须一直保持 `USB` 的连接 (`模拟器` 也使用该函数)
### `guia2.NewWiFiDriver("192.168.1.28")`
1. 先通过 `USB` 连接 `Android` 设备
Expand Down Expand Up @@ -74,6 +74,11 @@ func main() {
driver, err := guia2.NewUSBDriver()
// driver, err := guia2.NewWiFiDriver("192.168.1.28")
checkErr(err)
defer func() { _ = driver.Dispose() }()
// err = driver.AppLaunch("tv.danmaku.bili")
err = driver.AppLaunch("tv.danmaku.bili", guia2.BySelector{ResourceIdID: "tv.danmaku.bili:id/action_bar_root"})
checkErr(err, "launch the app until the element appears")
// fmt.Println(driver.Source())
// return
Expand Down
5 changes: 5 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ func main() {
driver, err := guia2.NewUSBDriver()
// driver, err := guia2.NewWiFiDriver("192.168.1.28")
checkErr(err)
defer func() { _ = driver.Dispose() }()

// err = driver.AppLaunch("tv.danmaku.bili")
err = driver.AppLaunch("tv.danmaku.bili", guia2.BySelector{ResourceIdID: "tv.danmaku.bili:id/action_bar_root"})
checkErr(err, "launch the app until the element appears")

// fmt.Println(driver.Source())
// return
Expand Down
18 changes: 17 additions & 1 deletion ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (d *Driver) ActiveAppPackageName() (appPackageName string, err error) {
return
}

func (d *Driver) AppLaunch(appPackageName string) (err error) {
func (d *Driver) AppLaunch(appPackageName string, waitForComplete ...BySelector) (err error) {
if err = d.check(); err != nil {
return err
}
Expand All @@ -147,6 +147,22 @@ func (d *Driver) AppLaunch(appPackageName string) (err error) {
if strings.Contains(sOutput, "monkey aborted") {
return fmt.Errorf("app launch: %s", strings.TrimSpace(sOutput))
}

if len(waitForComplete) != 0 {
var ce error
exists := func(_d *Driver) (bool, error) {
for i := range waitForComplete {
_, ce = _d.FindElement(waitForComplete[i])
if ce == nil {
return true, nil
}
}
return false, nil
}
if err = d.WaitWithTimeoutAndInterval(exists, 45, 1.5); err != nil {
return fmt.Errorf("app launch (waitForComplete): %s: %w", err.Error(), ce)
}
}
return
}

Expand Down
9 changes: 8 additions & 1 deletion ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,17 @@ func TestDriver_AppLaunch(t *testing.T) {
}
defer driver.Dispose()

err = driver.AppLaunch("tv.danmaku.bili")
// err = driver.AppLaunch("tv.danmaku.bili", BySelector{ResourceIdID: "tv.danmaku.bili:id/action_bar_root"})
err = driver.AppLaunch("com.android.settings", BySelector{ResourceIdID: "android:id/list"})
if err != nil {
t.Fatal(err)
}

// screenshot, err := driver.Screenshot()
// if err != nil {
// t.Fatal(err)
// }
// t.Log(ioutil.WriteFile("/Users/hero/Desktop/s1.png", screenshot.Bytes(), 0600))
}

func TestDriver_AppTerminate(t *testing.T) {
Expand Down

0 comments on commit 37b8ea2

Please sign in to comment.