-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
158 lines (136 loc) · 3.79 KB
/
Copy pathexample_test.go
File metadata and controls
158 lines (136 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package devicebase_test
import (
"fmt"
"os"
devicebase "github.com/devicebase/go-devicebase"
)
func ExampleNewClient() {
client := devicebase.NewClient(
devicebase.WithAPIKey("your-api-key"),
devicebase.WithSerialno("db-mttul4i41di8"),
)
_ = client
}
// ExampleClient_ListDevices discovers a serialno — the first step for every
// platform, and the only method that works without a bound serialno.
func ExampleClient_ListDevices() {
client := devicebase.NewClient(
devicebase.WithAPIKey(os.Getenv("DEVICEBASE_API_KEY")),
)
browsers, err := client.ListDevices(devicebase.ListDevicesRequest{Type: "browser"})
if err != nil {
fmt.Println("error:", err)
return
}
for _, device := range browsers {
fmt.Println(device.Serialno, device.State)
}
}
func ExampleClient_GetDeviceInfo() {
client := devicebase.NewClient(
devicebase.WithAPIKey(os.Getenv("DEVICEBASE_API_KEY")),
devicebase.WithSerialno("device123"),
)
info, err := client.GetDeviceInfo()
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println("device:", info.Serialno)
}
func ExampleClient_Tap() {
client := devicebase.NewClient(
devicebase.WithAPIKey("your-api-key"),
devicebase.WithSerialno("device123"),
)
result, err := client.Tap(100, 200)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println("success:", result.Success)
}
func ExampleClient_LaunchApp() {
client := devicebase.NewClient(
devicebase.WithAPIKey("your-api-key"),
devicebase.WithSerialno("device123"),
)
result, err := client.LaunchApp("com.tencent.mm")
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println("success:", result.Success)
}
func ExampleClient_Swipe() {
client := devicebase.NewClient(
devicebase.WithAPIKey("your-api-key"),
devicebase.WithSerialno("device123"),
)
result, err := client.Swipe(0, 500, 500, 500)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println("success:", result.Success)
}
func ExampleClient_GetScreenshot() {
client := devicebase.NewClient(
devicebase.WithAPIKey("your-api-key"),
devicebase.WithSerialno("device123"),
)
screenshot, err := client.GetScreenshot()
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Printf("screenshot size: %d bytes\n", len(screenshot))
}
// ExampleClient_BrowserNavigate drives a registered browser over CDP. The
// serialno comes from ListDevices with Type "browser".
func ExampleClient_BrowserNavigate() {
client := devicebase.NewClient(
devicebase.WithAPIKey("your-api-key"),
)
serialno := "db-mtsi49bf0mqb"
if _, err := client.BrowserNavigate(serialno, "https://example.com"); err != nil {
fmt.Println("error:", err)
return
}
if _, err := client.BrowserFill(serialno, "#search", "devicebase"); err != nil {
fmt.Println("error:", err)
return
}
text, err := client.BrowserText(serialno, "#search")
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println("value:", text.Data)
}
// ExampleClient_ComputerBash runs a command on the host that owns the desktop
// device. The command's own exit status arrives in data.exitCode rather than as
// an error, because the API call itself succeeded.
func ExampleClient_ComputerBash() {
client := devicebase.NewClient(
devicebase.WithAPIKey("your-api-key"),
)
result, err := client.ComputerBash("db-mtthisv311f1", "echo hello", 30)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println("exit code:", result.Data["exitCode"])
}
// ExampleClient_ComputerWait takes milliseconds and widens the HTTP deadline to
// cover the wait, so a sleep longer than the default 30s is not aborted.
func ExampleClient_ComputerWait() {
client := devicebase.NewClient(
devicebase.WithAPIKey("your-api-key"),
)
if _, err := client.ComputerWait("db-mtthisv311f1", 2000); err != nil {
fmt.Println("error:", err)
return
}
fmt.Println("waited 2s")
}