Skip to content

Commit e25447f

Browse files
committed
automated headless tests
1 parent f703c47 commit e25447f

File tree

4 files changed

+76
-23
lines changed

4 files changed

+76
-23
lines changed

host/main.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,30 @@ import (
1212

1313
//export main
1414
func main() {
15-
exitCode := roc.Main()
15+
options := roc.Options{
16+
SetupOnly: false,
17+
PrintBrowserVersionOnly: false,
18+
}
19+
20+
// os.Args is a slice of strings
21+
// os.Args[0] is the name of the program
22+
// os.Args[1:] contains the command-line arguments
23+
args := os.Args[1:]
24+
25+
for _, arg := range args {
26+
// fmt.Printf("arg: %s", arg)
27+
switch arg {
28+
case "--setup":
29+
options.SetupOnly = true
30+
case "--print-browser-version-only":
31+
options.PrintBrowserVersionOnly = true
32+
case "--headless":
33+
// TODO - this will be provided by the Roc app
34+
options.Headless = true
35+
}
36+
}
37+
38+
exitCode := roc.Main(options)
1639

1740
os.Exit(exitCode)
1841
}

host/roc/app.go

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import "C"
66
import (
77
"fmt"
88
"host/driversetup"
9+
"host/setup"
910
"host/utils"
1011
"host/webdriver"
1112
"os"
@@ -14,15 +15,36 @@ import (
1415
"unsafe"
1516
)
1617

17-
func Main() int {
18-
// fmt.Println(utils.FG_BLUE + "=============================" + utils.RESET)
19-
// fmt.Println(utils.FG_BLUE + "============SETUP============" + utils.RESET)
20-
err := setup()
18+
type Options struct {
19+
SetupOnly bool
20+
PrintBrowserVersionOnly bool
21+
Headless bool
22+
}
23+
24+
// TODO change when passing more than 1 value from Roc app is possible
25+
var headless = false
26+
27+
func Main(options Options) int {
28+
if options.PrintBrowserVersionOnly {
29+
fmt.Printf("%s", setup.BrowserVersion)
30+
return 0
31+
}
32+
33+
if options.Headless {
34+
headless = true
35+
}
36+
37+
err := driversetup.DownloadChromeAndDriver()
2138
if err != nil {
2239
fmt.Println(utils.FG_RED+"Setup failed with: "+utils.RESET, err)
2340
return 1
2441
}
2542

43+
if options.SetupOnly {
44+
fmt.Println("Browser and driver ready.")
45+
return 0
46+
}
47+
2648
cmd, err := driversetup.RunChromedriver()
2749
if err != nil {
2850
// todo
@@ -37,10 +59,6 @@ func Main() int {
3759
return 1
3860
}
3961

40-
// fmt.Println(utils.FG_BLUE + "Driver and Browser are ready." + utils.RESET)
41-
// fmt.Println(utils.FG_BLUE + "=============================" + utils.RESET)
42-
// fmt.Print("\n\n")
43-
4462
size := C.roc__mainForHost_1_exposed_size()
4563
capturePtr := roc_alloc(size, 0)
4664
defer roc_dealloc(capturePtr, 0)
@@ -88,18 +106,13 @@ func roc_fx_wait(timeout int64) C.struct_ResultVoidStr {
88106
return createRocResultStr(RocOk, "")
89107
}
90108

91-
func setup() error {
92-
err := driversetup.DownloadChromeAndDriver()
93-
if err != nil {
94-
return err
95-
}
96-
97-
return nil
98-
}
99-
100109
//export roc_fx_startSession
101110
func roc_fx_startSession() C.struct_ResultVoidStr {
102-
sessionId, err := webdriver.CreateSession()
111+
serverOptions := webdriver.SessionOptions{
112+
Headless: headless,
113+
}
114+
115+
sessionId, err := webdriver.CreateSession(serverOptions)
103116

104117
if err != nil {
105118
fmt.Println("error value ")

host/setup/setup.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@ type BrowserPaths struct {
1515
DriverDirPath string
1616
}
1717

18+
var (
19+
// TODO - this will be passed from the roc program
20+
BrowserVersion = fmt.Sprintf("Chrome-%s", ChromeVersion)
21+
ChromeVersion = "117.0.5846.0"
22+
)
23+
1824
func GetChromePaths() (*BrowserPaths, error) {
1925
os := fmt.Sprintf("%s-%s", runtime.GOOS, runtime.GOARCH)
2026

2127
browserFilesDir := "browser_files"
22-
chromeVersion := "117.0.5846.0"
28+
chromeVersion := ChromeVersion
2329
var osName string
2430

2531
switch os {

host/webdriver/webdriver.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,36 @@ type CreateSession_Response struct {
2121
Value CreateSession_ResponseValue `json:"value"`
2222
}
2323

24-
func CreateSession() (string, error) {
24+
type SessionOptions struct {
25+
// TODO - props like window-size, timeouts, etc passed down from the Roc app
26+
Headless bool
27+
}
28+
29+
func CreateSession(options SessionOptions) (string, error) {
2530
url := fmt.Sprintf("%s/session", baseUrl)
2631
paths, err := setup.GetChromePaths()
2732
if err != nil {
2833
return "", err
2934
}
3035

36+
headlessSwtich := ""
37+
if options.Headless {
38+
headlessSwtich = ", \"--headless\""
39+
}
40+
41+
// TODO parametrize this when passing more data from Roc app is possible
3142
jsonData := []byte(fmt.Sprintf(`{
3243
"capabilities": {
3344
"firstMatch": [
3445
{
3546
"goog:chromeOptions": {
3647
"binary": "%s",
37-
"args": ["--window-size=1920,1080"]
48+
"args": ["--window-size=1920,1080"%s]
3849
}
3950
}
4051
]
4152
}
42-
}`, paths.BrowserPath))
53+
}`, paths.BrowserPath, headlessSwtich))
4354

4455
var response CreateSession_Response
4556
err = makeHttpRequest("POST", url, bytes.NewBuffer(jsonData), &response)

0 commit comments

Comments
 (0)