-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (70 loc) · 2.06 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
package main
import (
"flag"
"fmt"
"github.com/sclevine/agouti"
"log"
"regexp"
)
const defaultSeleniumPort = 4444
const defaultSeleniumProto = "http"
const defaultSeleniumAddr = "127.0.0.1"
var selenium_url = flag.String("h", fmt.Sprintf("%s://%s:%d", defaultSeleniumProto, defaultSeleniumAddr, defaultSeleniumPort), "Selenium host URL. Http is used if no protocol is specified")
var screenshot_file = flag.String("s", "", "Take final screenshot and save it to file with this name")
func checkErrFatal(err error) {
if err != nil {
log.Panic(err)
}
}
func getSeleniumURL(url string) string {
return getValidUrl(url) + "/wd/hub"
}
func getValidUrl(url string) string {
matched, err := regexp.MatchString("^https?://", url)
checkErrFatal(err)
if !matched {
url = defaultSeleniumProto + "://" + url
}
matched, err = regexp.MatchString(`.*:\d+`, url)
checkErrFatal(err)
if !matched {
url = fmt.Sprintf("%s:%d", url, defaultSeleniumPort)
}
return url
}
func getValidScreenshotFile(name string) string {
matched, err := regexp.MatchString(`.*\.jpe?g$`, name)
checkErrFatal(err)
if !matched {
name = name + ".jpg"
}
return name
}
func main() {
flag.Parse()
seleniumURL := getValidUrl(*selenium_url)
capabilities := agouti.NewCapabilities().Browser("chrome").Platform("linux").With("javascriptEnabled")
log.Println("Connecting to selenium")
page, err := agouti.NewPage(seleniumURL+"/wd/hub", agouti.Desired(capabilities))
if err != nil {
log.Fatal("Failed to open page:", err)
}
log.Println("Navigating to URL")
if err := page.Navigate(seleniumURL + "/grid/console"); err != nil {
log.Fatal("Failed to navigate:", err)
}
log.Println("Getting header")
selection, err := page.FindByID("header").All("h2").Text()
if err != nil {
log.Fatal("Failed to get element:", err)
}
if *screenshot_file != "" {
screenshotFile := getValidScreenshotFile(*screenshot_file)
log.Printf("Getting %s screenshot\n", screenshotFile)
err = page.Screenshot(screenshotFile)
if err != nil {
log.Fatal("Failed to get screenshot:", err)
}
}
fmt.Println(selection)
}