Skip to content

Commit

Permalink
docs: 替换 示例代码 和 GIF
Browse files Browse the repository at this point in the history
  • Loading branch information
electricbubble committed Aug 9, 2020
1 parent c189e7b commit 4534993
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 86 deletions.
60 changes: 16 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ package main
import (
"github.com/electricbubble/guia2"
"log"
"time"
)
func main() {
Expand Down Expand Up @@ -52,62 +51,35 @@ func main() {
err = driver.SwipePointF(startPoint, endPoint)
checkErr(err)
element, err := driver.FindElement(guia2.BySelector{UiAutomator: "new UiSelector().className(\"android.view.ViewGroup\");"})
element, err := driver.FindElement(guia2.BySelector{UiAutomator: "new UiSelector().textStartsWith(\"MIUI\");"})
checkErr(err)
elem, err := element.FindElement(guia2.BySelector{UiAutomator: "new UiSelector().className(\"android.widget.LinearLayout\").index(6);"})
checkErr(err)
rect, err := elem.Rect()
checkErr(err)
x := rect.X + int(float64(rect.Width)*2)
y := rect.Y + rect.Height/2
err = driver.Tap(x, y)
checkErr(err)
time.Sleep(time.Millisecond * 600)
element, err = driver.FindElement(guia2.BySelector{UiAutomator: "new UiSelector().text(\"科技\");"})
checkErr(err)
time.Sleep(time.Millisecond * 1200)
err = element.Click()
checkErr(err)
time.Sleep(time.Second * 1)
err = driver.Swipe(startX, startY, endX, endY/2)
element, err = driver.FindElement(guia2.BySelector{UiAutomator: "new UiSelector().textStartsWith(\"查看更多\");"})
checkErr(err)
elements, err := driver.FindElements(guia2.BySelector{ResourceIdID: "cn.xuexi.android:id/general_card_image_id"})
checkErr(err)
time.Sleep(time.Second * 1)
elem = elements[len(elements)-1]
checkErr(elem.Click())
time.Sleep(time.Second * 1)
err = driver.ScrollTo(guia2.BySelector{ContentDescription: "点赞"})
checkErr(err)
element, err = driver.FindElement(guia2.BySelector{ClassName: "android.widget.TextView"})
checkErr(err)
checkErr(element.Click())
time.Sleep(time.Millisecond * 500)
elem, err = driver.FindElement(guia2.BySelector{ClassName: "android.widget.EditText"})
exists := func(d *guia2.Driver) (bool, error) {
element, err = driver.FindElement(guia2.BySelector{UiAutomator: "new UiSelector().text(\"关注\");"})
if err == nil {
return true, nil
}
return false, nil
}
err = driver.Wait(exists)
checkErr(err)
err = elem.SendKeys("test " + time.Now().Format("2006-01-02 15:04:05"))
element, err = driver.FindElement(guia2.BySelector{UiAutomator: "new UiSelector().textContains(\" 图像\");"})
checkErr(err)
time.Sleep(time.Second * 1)
element, err = driver.FindElement(guia2.BySelector{UiAutomator: "new UiSelector().text(\"取消\");"})
checkErr(err)
checkErr(element.Click())
err = driver.ScrollTo(guia2.BySelector{UiAutomator: "new UiSelector().textContains(\"全部评论\");"})
checkErr(err)
// element, err = driver.FindElement(guia2.BySelector{ResourceIdID: "cn.xuexi.android:id/TOP_LAYER_VIEW_ID"})
// checkErr(err)
// elemBack, err := element.FindElement(guia2.BySelector{ClassName: "android.widget.ImageView"})
Expand All @@ -134,10 +106,10 @@ func checkErr(err error, msg ...string) {
```
> 以上代码仅使用了 `网易MuMu` (`Android` 版本: `6.0.1`) 进行了测试。
> 以上代码仅使用了 `小米8` 进行了测试。
![example](https://raw.githubusercontent.com/electricbubble/ImageHosting/master/img/202008051855_guia2.gif)
![example](https://github.com/electricbubble/ImageHosting/blob/master/img/202008091517_guia2.gif)
## TODO
Expand Down
56 changes: 14 additions & 42 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"github.com/electricbubble/guia2"
"log"
"time"
)

func main() {
Expand Down Expand Up @@ -31,62 +30,35 @@ func main() {
err = driver.SwipePointF(startPoint, endPoint)
checkErr(err)

element, err := driver.FindElement(guia2.BySelector{UiAutomator: "new UiSelector().className(\"android.view.ViewGroup\");"})
element, err := driver.FindElement(guia2.BySelector{UiAutomator: "new UiSelector().textStartsWith(\"MIUI\");"})
checkErr(err)

elem, err := element.FindElement(guia2.BySelector{UiAutomator: "new UiSelector().className(\"android.widget.LinearLayout\").index(6);"})
checkErr(err)

rect, err := elem.Rect()
checkErr(err)

x := rect.X + int(float64(rect.Width)*2)
y := rect.Y + rect.Height/2
err = driver.Tap(x, y)
checkErr(err)

time.Sleep(time.Millisecond * 600)

element, err = driver.FindElement(guia2.BySelector{UiAutomator: "new UiSelector().text(\"科技\");"})
checkErr(err)

time.Sleep(time.Millisecond * 1200)

err = element.Click()
checkErr(err)

time.Sleep(time.Second * 1)

err = driver.Swipe(startX, startY, endX, endY/2)
element, err = driver.FindElement(guia2.BySelector{UiAutomator: "new UiSelector().textStartsWith(\"查看更多\");"})
checkErr(err)

elements, err := driver.FindElements(guia2.BySelector{ResourceIdID: "cn.xuexi.android:id/general_card_image_id"})
checkErr(err)
time.Sleep(time.Second * 1)

elem = elements[len(elements)-1]
checkErr(elem.Click())
time.Sleep(time.Second * 1)

err = driver.ScrollTo(guia2.BySelector{ContentDescription: "点赞"})
checkErr(err)

element, err = driver.FindElement(guia2.BySelector{ClassName: "android.widget.TextView"})
checkErr(err)
checkErr(element.Click())
time.Sleep(time.Millisecond * 500)

elem, err = driver.FindElement(guia2.BySelector{ClassName: "android.widget.EditText"})
exists := func(d *guia2.Driver) (bool, error) {
element, err = driver.FindElement(guia2.BySelector{UiAutomator: "new UiSelector().text(\"关注\");"})
if err == nil {
return true, nil
}
return false, nil
}
err = driver.Wait(exists)
checkErr(err)

err = elem.SendKeys("test " + time.Now().Format("2006-01-02 15:04:05"))
element, err = driver.FindElement(guia2.BySelector{UiAutomator: "new UiSelector().textContains(\" 图像\");"})
checkErr(err)
time.Sleep(time.Second * 1)

element, err = driver.FindElement(guia2.BySelector{UiAutomator: "new UiSelector().text(\"取消\");"})
checkErr(err)
checkErr(element.Click())

err = driver.ScrollTo(guia2.BySelector{UiAutomator: "new UiSelector().textContains(\"全部评论\");"})
checkErr(err)

// element, err = driver.FindElement(guia2.BySelector{ResourceIdID: "cn.xuexi.android:id/TOP_LAYER_VIEW_ID"})
// checkErr(err)
// elemBack, err := element.FindElement(guia2.BySelector{ClassName: "android.widget.ImageView"})
Expand Down

0 comments on commit 4534993

Please sign in to comment.