-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_test.go
106 lines (83 loc) · 1.89 KB
/
main_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package conditions_test
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"testing"
"time"
"github.com/tebeka/selenium"
)
var cmd *exec.Cmd
var wd selenium.WebDriver
func TestMain(m *testing.M) {
err := runSelenium()
if err != nil {
fmt.Printf("Error staring Selenium.\n")
panic(err)
}
fmt.Printf("Selenium started.\n")
go startServer()
err = runRemote()
if err != nil {
fmt.Printf("Error staring remote.\n")
panic(err)
}
retCode := m.Run()
wd.Quit()
err = stopSelenium()
if err != nil {
fmt.Printf("Error stopping Selenium.\n")
panic(err)
}
fmt.Printf("Selenium stopped.\n")
os.Exit(retCode)
}
func runRemote() error {
caps := selenium.Capabilities{"browserName": "firefox"}
driver, err := selenium.NewRemote(caps, "")
wd = driver
return err
}
func runSelenium() error {
// Running selenium-standalone.
cmd = exec.Command("java", "-jar", "testing/selenium-server-standalone-3.5.1.jar")
err := cmd.Start()
if err != nil {
return err
}
// Waiting for selenium-standalone server to run.
for i := 0; i < 30; i++ {
time.Sleep(time.Second)
resp, err := http.Get("http://localhost:4444/status")
if err == nil {
resp.Body.Close()
switch resp.StatusCode {
// Selenium <3 returned Forbidden and BadRequest. ChromeDriver and
// Selenium 3 return OK.
case http.StatusForbidden, http.StatusBadRequest, http.StatusOK:
return nil
}
}
}
return fmt.Errorf("server did not respond on port %d", 4444)
}
func handler(writer http.ResponseWriter, request *http.Request) {
pwd, _ := os.Getwd()
path := request.URL.Path
bytes, err := ioutil.ReadFile(pwd + "/testing/static" + path + ".html")
if err != nil {
http.NotFound(writer, request)
return
}
fmt.Fprintf(writer, string(bytes))
}
func startServer() {
http.HandleFunc("/", handler)
http.ListenAndServe(":3000", nil)
}
func stopSelenium() error {
err := cmd.Process.Kill()
return err
}