-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (90 loc) · 2.88 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
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/tebeka/selenium"
)
const envUsername = "WDSHOT_USERNAME"
const envPassword = "WDSHOT_PASSWORD"
func printUsage() {
_, err := fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS]\n", os.Args[0])
if err != nil {
panic(err)
}
flag.PrintDefaults()
fmt.Println("Note you will need to provide the following environment variables:")
fmt.Println("- " + envUsername)
fmt.Println("- " + envPassword)
}
func main() {
flag.Usage = printUsage
wantDebug := flag.Bool("debug", false, "whether to turn selenium debugging on")
theURL := flag.String("url", "https://www.example.com/", "the URL to hit")
theWDURL := flag.String("wdurl", "hub-cloud.browserstack.com/wd/hub", "the host/path to use to hit the remote Selenium WebDriver")
theSelector := flag.String("css", ".srow.rt-3c", "the CSS selector to use")
theScroll := flag.Int("scroll", -200, "how much to scroll after hitting the selector")
thePNG := flag.String("png", "foo.png", "the output PNG file")
theBrowser := flag.String("browser", "Firefox", "the browser to use")
theBrowserVersion := flag.String("browserversion", "69.0 beta", "the browser version to use")
theOS := flag.String("os", "Windows", "the OS to use")
theOSVersion := flag.String("osversion", "10", "the OS version to use")
theResolution := flag.String("resolution", "1280x1024", "the resolution to use")
theTestName := flag.String("test", "BStack Sample Test", "the test name to use")
theSleep := flag.Int64("sleep", 1, "the seconds to sleep before moving to the element (seconds)")
flag.Parse()
// pass sensitive info through the environment!
WDUsername := os.Getenv(envUsername)
if len(WDUsername) == 0 {
panic("Need " + envUsername)
}
WDKey := os.Getenv(envPassword)
if len(WDKey) == 0 {
panic("Need " + envPassword)
}
f, err := os.Create(*thePNG)
if err != nil {
panic(err)
}
defer f.Close()
selenium.SetDebug(*wantDebug)
caps := selenium.Capabilities{
"browser": *theBrowser,
"browser_version": *theBrowserVersion,
"os": *theOS,
"os_version": *theOSVersion,
"resolution": *theResolution,
"name": *theTestName,
// TODO more options via flags/command line
}
wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://%s:%s@%s", WDUsername, WDKey, *theWDURL))
if err != nil {
panic(err)
}
defer wd.Quit()
if err = wd.Get(*theURL); err != nil {
panic(err)
}
time.Sleep(time.Duration(*theSleep) * time.Second)
el, err := wd.FindElement(selenium.ByCSSSelector, *theSelector)
if err != nil {
panic(err)
}
_, err = wd.ExecuteScript("arguments[0].scrollIntoView(true);", []interface{}{el})
if err != nil {
panic(err)
}
_, err = wd.ExecuteScript("window.scrollBy(0,arguments[0])", []interface{}{*theScroll})
if err != nil {
panic(err)
}
data, err := wd.Screenshot()
if err != nil {
panic(err)
}
_, err = f.Write(data)
if err != nil {
panic(err)
}
}