-
Notifications
You must be signed in to change notification settings - Fork 4
/
browsersteps_test.go
80 lines (69 loc) · 1.54 KB
/
browsersteps_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
package browsersteps
import (
"encoding/json"
"fmt"
"log"
"net"
"os"
"strconv"
"testing"
"time"
"net/http"
"net/http/httptest"
"net/url"
"github.com/DATA-DOG/godog"
"github.com/tebeka/selenium"
)
func iWaitFor(amount int, unit string) error {
u := time.Second
fmt.Printf("Waiting for %d %s", amount, unit)
time.Sleep(u * time.Duration(amount))
return nil
}
func FeatureContext(s *godog.Suite) {
s.Step(`^I wait for (\d+) (milliseconds|millisecond|seconds|second)$`, iWaitFor)
debug := os.Getenv("DEBUG")
if debug != "" {
val, err := strconv.ParseBool(debug)
if err == nil {
selenium.SetDebug(val)
}
}
capabilities := selenium.Capabilities{"browserName": "chrome"}
capEnv := os.Getenv("SELENIUM_CAPABILITIES")
if capEnv != "" {
err := json.Unmarshal([]byte(capEnv), &capabilities)
if err != nil {
log.Panic(err)
}
}
bs := NewBrowserSteps(s, capabilities, os.Getenv("SELENIUM_URL"))
var server *httptest.Server
s.BeforeSuite(func() {
server = httptest.NewUnstartedServer(http.FileServer(http.Dir("./public")))
listenAddress := os.Getenv("SERVER_LISTEN")
if listenAddress != "" {
var err error
server.Listener, err = net.Listen("tcp4", listenAddress)
if err != nil {
log.Fatal(err)
}
}
server.Start()
u, err := url.Parse(server.URL)
if err != nil {
log.Panic(err.Error())
}
bs.SetBaseURL(u)
})
s.AfterSuite(func() {
if server != nil {
server.Close()
server = nil
}
})
}
func TestMain(m *testing.M) {
status := godog.Run("browsersteps", FeatureContext)
os.Exit(status)
}