由于 Appium 的内容过于丰富。。我又不太想安装那么多我用不到的部分。。

而且 Appium 提供的 客户端库 并没有 Golang 的实现版本。

于是我写了使用 Golang 实现 appium/WebDriverAgent 的客户端库。

这些天重构了部分,相对来说使用会更接近 tebeka/selenium

灵感来自 Python 版的 facebook-wda : https://github.com/openatx/facebook-wda

扩展库

Android

安装

go get github.com/electricbubble/gwda

连接设备

package main

import (
    "github.com/electricbubble/gwda"
    "log"
)

func main() {
    // var urlPrefix = "http://localhost:8100"
    // 该函数或许还需要 `iproxy 8100 8100` 先进行设备端口转发
    // driver, _ := gwda.NewDriver(nil, urlPrefix)

    // 通过 USB 直连设备
    driver, _ := gwda.NewUSBDriver(nil)

    log.Println(driver.IsWdaHealthy())
}

手势操作

package main

import (
    "github.com/electricbubble/gwda"
)

func main() {
    driver, _ := gwda.NewUSBDriver(nil)

    x, y := 50, 256

    driver.Tap(x, y)

    driver.DoubleTap(x, y)

    driver.TouchAndHold(x, y)

    fromX, fromY, toX, toY := 50, 256, 100, 256

    driver.Drag(fromX, fromY, toX, toY)

    driver.Swipe(fromX, fromY, toX, toY)

    // 需要 3D Touch 硬件支持
    // driver.ForceTouch(x, y, 0.8)
}
driver.PerformW3CActionsdriver.PerformAppiumTouchActions

App 操作

package main

import (
    "github.com/electricbubble/gwda"
)

func main() {
    driver, _ := gwda.NewUSBDriver(nil)

    var bundleId = "com.apple.Preferences"

    driver.AppLaunchUnattached(bundleId)

    driver.AppDeactivate(2)

    driver.AppTerminate(bundleId)

    driver.AppActivate(bundleId)

    // 重置当前 App 的 相机  权限
    // driver.AppAuthReset(gwda.ProtectedResourceCamera)
}

键盘输入

package main

import (
    "github.com/electricbubble/gwda"
)

func main() {
    driver, _ := gwda.NewUSBDriver(nil)

    driver.SendKeys("hello")
}

Siri 操作

package main

import (
    "github.com/electricbubble/gwda"
)

func main() {
    driver, _ := gwda.NewUSBDriver(nil)

    driver.SiriActivate("What's the weather like today")
}

弹窗操作

package main

import (
    "github.com/electricbubble/gwda"
    "log"
)

func main() {
    driver, _ := gwda.NewUSBDriver(nil)

    text, _ := driver.AlertText()
    log.Println(text)

    alertButtons, _ := driver.AlertButtons()
    log.Println(alertButtons)

    driver.AlertAccept()
    // driver.AlertDismiss()

    // driver.SendKeys("ah")
}

设备的基本信息

package main

import (
    "github.com/electricbubble/gwda"
    "log"
)

func main() {
    driver, _ := gwda.NewUSBDriver(nil)

    deviceInfo, _ := driver.DeviceInfo()
    log.Println(deviceInfo.Name)

    batteryInfo, _ := driver.BatteryInfo()
    log.Println(batteryInfo.State)

    windowSize, _ := driver.WindowSize()
    log.Println(windowSize)

    // screen, _ := driver.Screen()
    // log.Println(screen)
}

按键操作

package main

import (
    "github.com/electricbubble/gwda"
)

func main() {
    driver, _ := gwda.NewUSBDriver(nil)

    // driver.Homescreen()

    driver.PressButton(gwda.DeviceButtonHome)
    driver.PressButton(gwda.DeviceButtonVolumeUp)
    driver.PressButton(gwda.DeviceButtonVolumeDown)
}

截图

package main

import (
    "github.com/electricbubble/gwda"
    "image"
)

func main() {
    driver, _ := gwda.NewUSBDriver(nil)

    screenshot, _ := driver.Screenshot()

    img, format, _ := image.Decode(screenshot)
    _, _ = img, format
}

调试函数

package main

import (
    "fmt"
    "github.com/electricbubble/gwda"
)

func main() {
    driver, _ := gwda.NewUSBDriver(nil)

    source, _ := driver.Source()
    fmt.Println(source)

    // fmt.Println(driver.AccessibleSource())

    // gwda.SetDebug(true)
}