-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (158 loc) · 4.74 KB
/
main.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Command screenshot is a chromedp example demonstrating how to take a
// screenshot of a specific element and of the entire browser viewport.
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/chromedp/cdproto/emulation"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
)
func RunWithTimeOut(ctx *context.Context, timeout time.Duration, tasks chromedp.Tasks) chromedp.ActionFunc {
return func(ctx context.Context) error {
timeoutContext, cancel := context.WithTimeout(ctx, timeout*time.Second)
defer cancel()
return tasks.Do(timeoutContext)
}
}
func main() {
// get flags
pathPtr := flag.String("path", "http://localhost:9980/", "url")
elementPtr := flag.String("element", ".main-body", "class to wait for and then capture")
filePtr := flag.String("file", "screenshot.png", "save as filename")
flag.Parse()
// ignore unsigned certs
opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.Flag("ignore-certificate-errors", "1"),
chromedp.Flag("headless", true),
)
// create context
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf))
defer cancel()
var buf []byte
// capture entire browser viewport, returning png with quality=90
if err := chromedp.Run(ctx, RunWithTimeOut(&ctx, 30, fullScreenshot(*pathPtr, *elementPtr, 90, &buf))); err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile(*filePtr, buf, 0644); err != nil {
log.Fatal(err)
}
}
// fullScreenshot takes a screenshot of the entire browser viewport.
//
// Liberally copied from puppeteer's source.
//
// Note: this will override the viewport emulation settings.
func fullScreenshot(urlstr string, sel string, quality int64, res *[]byte) chromedp.Tasks {
if strings.Contains(urlstr, "https") {
return chromedp.Tasks{
chromedp.Navigate(urlstr),
chromedp.WaitVisible(`input[type='text']`, chromedp.ByQuery),
chromedp.SendKeys(`input[type='text']`, "admin", chromedp.ByQuery),
chromedp.WaitVisible(`input[type='password']`, chromedp.ByQuery),
chromedp.SendKeys(`input[type='password']`, "adminpw\t\t\n", chromedp.ByQuery),
chromedp.WaitVisible(sel, chromedp.ByQuery),
chromedp.Sleep(2 * time.Second),
chromedp.ActionFunc(func(ctx context.Context) error {
// get layout metrics
_, _, contentSize, err := page.GetLayoutMetrics().Do(ctx)
if err != nil {
return err
}
width, height := int64(1280), int64(720)
// force viewport emulation
err = emulation.SetDeviceMetricsOverride(width, height, 1, false).
WithScreenOrientation(&emulation.ScreenOrientation{
Type: emulation.OrientationTypePortraitPrimary,
Angle: 0,
}).
Do(ctx)
if err != nil {
return err
}
// capture screenshot
*res, err = page.CaptureScreenshot().
WithQuality(quality).
WithClip(&page.Viewport{
X: contentSize.X,
Y: contentSize.Y,
Width: 1280,
Height: 720,
Scale: 1,
}).Do(ctx)
if err != nil {
return err
}
return nil
}),
}
} else {
return chromedp.Tasks{
chromedp.Navigate(urlstr),
chromedp.WaitVisible(sel, chromedp.ByQuery),
chromedp.Sleep(2 * time.Second),
chromedp.ActionFunc(func(ctx context.Context) error {
// get layout metrics
_, _, contentSize, err := page.GetLayoutMetrics().Do(ctx)
if err != nil {
return err
}
width, height := int64(1280), int64(720)
// force viewport emulation
err = emulation.SetDeviceMetricsOverride(width, height, 1, false).
WithScreenOrientation(&emulation.ScreenOrientation{
Type: emulation.OrientationTypePortraitPrimary,
Angle: 0,
}).
Do(ctx)
if err != nil {
return err
}
// capture screenshot
*res, err = page.CaptureScreenshot().
WithQuality(quality).
WithClip(&page.Viewport{
X: contentSize.X,
Y: contentSize.Y,
Width: 1280,
Height: 720,
Scale: 1,
}).Do(ctx)
if err != nil {
return err
}
return nil
}),
}
}
}
func getValues() string {
// Social struct which contains a
// list of links
type AnsibleOutput struct {
Bp_integration_tests_token string `json:"bp_integration_tests_token"`
}
// Open our jsonFile
jsonFile, err := os.Open("localhost")
// if we os.Open returns an error then handle it
if err != nil {
fmt.Println(err)
}
fmt.Println("Successfully Opened localhost")
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var result AnsibleOutput
// var result map[string]interface{}
json.Unmarshal([]byte(byteValue), &result)
fmt.Println(result.Bp_integration_tests_token)
return result.Bp_integration_tests_token
}