-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_screenshot_test.go
85 lines (70 loc) · 1.54 KB
/
example_screenshot_test.go
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
package cri_test
import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"strconv"
"github.com/SKatiyar/cri"
"github.com/SKatiyar/cri/emulation"
"github.com/SKatiyar/cri/page"
)
// Example shows steps to take screenshot of a page.
func Example() {
conn, connErr := cri.NewConnection()
if connErr != nil {
panic(connErr)
}
pi := page.New(conn)
ei := emulation.New(conn)
if enableErr := pi.Enable(); enableErr != nil {
panic(enableErr)
}
if overideErr := ei.SetDeviceMetricsOverride(&emulation.SetDeviceMetricsOverrideRequest{
Width: 1280,
Height: 800,
}); overideErr != nil {
panic(overideErr)
}
urls := []string{
"https://www.google.com",
"https://www.chromestatus.com",
"https://www.facebook.com",
"https://www.example.com",
}
for i := 0; i < len(urls); i++ {
doneChn := make(chan struct{})
pi.LoadEventFired(func(params *page.LoadEventFiredParams, err error) bool {
close(doneChn)
return false
})
_, navErr := pi.Navigate(&page.NavigateRequest{
Url: urls[i],
})
if navErr != nil {
panic(navErr)
}
<-doneChn
pic, picErr := pi.CaptureScreenshot(nil)
if picErr != nil {
panic(picErr)
}
img, imgErr := base64.StdEncoding.DecodeString(pic.Data)
if imgErr != nil {
panic(imgErr)
}
fileName := strconv.Itoa(i) + ".png"
if writeErr := ioutil.WriteFile(fileName, img, 0700); writeErr != nil {
panic(writeErr)
}
fmt.Println(fileName)
if removeErr := os.Remove(fileName); removeErr != nil {
panic(removeErr)
}
}
// Unordered output:
// 0.png
// 1.png
// 2.png
// 3.png
}