File size: 2,883 Bytes
530729e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package web

import (
	"fmt"
	"testing"
	"time"

	"github.com/mgutz/ansi"

	_ "github.com/GoAdminGroup/go-admin/adapter/gin"
	_ "github.com/GoAdminGroup/go-admin/modules/db/drivers/mysql"
	_ "github.com/GoAdminGroup/themes/adminlte"

	"github.com/sclevine/agouti"
)

type Testers func(t *testing.T, page *Page)
type ServerStarter func(quit chan struct{})

// UserAcceptanceTestSuit make sure the chromedriver version Is corresponding to the
// chrome version. Using the following link to get the latest version of Chrome and ChromeDriver.
// https://googlechromelabs.github.io/chrome-for-testing/
func UserAcceptanceTestSuit(t *testing.T, testers Testers, serverStarter ServerStarter, local bool, options ...string) {
	var quit = make(chan struct{})
	go serverStarter(quit)

	if len(options) == 0 {
		options = []string{
			"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36",
			"--window-size=1500,900",
			"--incognito",
			"--blink-settings=imagesEnabled=true",
			"--no-default-browser-check",
			"--ignore-ssl-errors=true",
			"--ssl-protocol=any",
			"--no-sandbox",
			"--disable-breakpad",
			"--disable-gpu",
			"--disable-logging",
			"--no-zygote",
			"--allow-running-insecure-content",
		}
		if !local {
			options = append(options, "--headless")
		}
	}

	driver := agouti.ChromeDriver(
		agouti.ChromeOptions("args", options),
		agouti.Desired(
			agouti.Capabilities{
				"loggingPrefs": map[string]string{
					"performance": "ALL",
				},
				"acceptSslCerts":      true,
				"acceptInsecureCerts": true,
			},
		))
	err := driver.Start()
	if err != nil {
		panic("failed to start driver, error: " + err.Error())
	}

	page, err := driver.NewPage()
	if err != nil {
		panic("failed to open page, error: " + err.Error())
	}

	fmt.Println()
	fmt.Println("============================================")
	printlnWithColor("User Acceptance Testing", "blue")
	fmt.Println("============================================")
	fmt.Println()

	testers(t, &Page{T: t, Page: page, Driver: driver, Quit: quit})

	wait(2)

	if !local {
		err = page.CloseWindow()
		if err != nil {
			fmt.Println("failed to close page, error: ", err)
		}

		err = page.Destroy()
		if err != nil {
			fmt.Println("failed to destroy page, error: ", err)
		}

		err = driver.Stop()
		if err != nil {
			fmt.Println("failed to stop driver, error: ", err)
		}
	}

	quit <- struct{}{}
}

func printlnWithColor(msg string, color string) {
	fmt.Println(ansi.Color(msg, color))
}

func printPart(part string) {
	printlnWithColor("> "+part, colorBlue)
}

func wait(t int) {
	time.Sleep(time.Duration(t) * time.Second)
}

const basePath = "http://localhost:9033"

func url(suffix string) string {
	if suffix == "/" {
		suffix = ""
	}
	return basePath + "/admin" + suffix
}

const (
	colorBlue  = "blue"
	colorGreen = "green"
)