From 9ff5a4d654dd9a4a4f7d38daa5254fb388585dfe Mon Sep 17 00:00:00 2001 From: Trey Sturman Date: Thu, 26 Jan 2023 15:59:40 -0500 Subject: [PATCH 1/4] #74-builds app, connects to browser in localhost --- testing/automation/GoWebBehavior_test.go | 67 ++++++++++++++++++------ 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/testing/automation/GoWebBehavior_test.go b/testing/automation/GoWebBehavior_test.go index 3c1975c..6e4a941 100644 --- a/testing/automation/GoWebBehavior_test.go +++ b/testing/automation/GoWebBehavior_test.go @@ -2,48 +2,83 @@ package testing import ( "fmt" + "log" + "os" "os/exec" + "runtime" "testing" - - models "github.com/SCCapstone/BitCrunch/models" - - "github.com/go-rod/rod" ) // https://github.com/go-rod/rod/blob/master/examples_test.go func TestMain(m *testing.M) { fmt.Println("Building and Activating GoWeb...") + fmt.Println("Making sure that we are in the correct directory...") + cherry := os.Chdir("../../") + if cherry != nil { + // Something happened when trying to change dir! + fmt.Println("chdir didn't work!", cherry) + return + } fmt.Println("Building an exe file from the Go code...") - cmd := exec.Command("go", "build -o GoWeb.exe") // purposely using specific name - err := cmd.Run() + cmd := exec.Command("go", "build", "-o", "GoWeb.exe") // purposely using specific name + output, err := cmd.CombinedOutput() if err != nil { // Something happened when trying to build the codebase! - fmt.Println("Something happened when trying to build the codebase! \n", err) + fmt.Println(fmt.Sprint(err) + ": " + string(output)) + //fmt.Println("Something happened when trying to build the codebase! \n", err) return } fmt.Println("Running built exe file...") cmd = exec.Command(".\\GoWeb.exe") // running the exe to produce a local copy of the webpage - err = cmd.Run() + output, err = cmd.CombinedOutput() if err != nil { // Something happened when trying to run the built command! - fmt.Println("Something happened when trying to run the built command! \n", err) + fmt.Println(fmt.Sprint(err) + ": " + string(output)) return } - browser := rod.New().MustConnect() // opens up the default browser - defer browser.MustClose() // makes sure the browser closes once Tests are complete - - fmt.Println("GoWeb activated! Begin Testing...") + /* + browser := rod.New().MustConnect() // opens up the default browser + fmt.Printf("browser") + defer browser.MustClose() // makes sure the browser closes once Tests are complete + page1 := browser.MustPage("localhost") // creates a page from browser + fmt.Printf("page1") + fmt.Println(page1) + fmt.Printf("defer close") + */ + openbrowser("localhost") + fmt.Println("GoWeb activated! Begin Testing...") // will be "connecting" using rod within the tests themselves m.Run() fmt.Println("Testing Complete!") + + //NOTE: GoWeb.exe isn't removed by the code (TODO), make sure you delete it before running tests! } -func TestBrowser(t *testing.T) { - newfloorList := models.GetAllFloors() - fmt.Println(newfloorList[0]) // placeholder +func TestPageRunning(t *testing.T) { } func TestLogin(t *testing.T) { // opens up the domain and attempts to login using user1 and pass1 } + +// Helper function to open correct browser for testee's machine +// thanks to https://gist.github.com/hyg/9c4afcd91fe24316cbf0 +func openbrowser(url string) { + var err error + + switch runtime.GOOS { + case "linux": + err = exec.Command("xdg-open", url).Start() + case "windows": + err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() + case "darwin": + err = exec.Command("open", url).Start() + default: + err = fmt.Errorf("unsupported platform") + } + if err != nil { + log.Fatal(err) + } + +} From 0a38c238aae7696199fdb9448d67d98f355b16df Mon Sep 17 00:00:00 2001 From: Trey Sturman Date: Fri, 27 Jan 2023 16:47:16 -0500 Subject: [PATCH 2/4] #74 - app on localhost, tests for connection --- README.md | 23 +++++++++++++-------- go.mod | 12 +++++------ go.sum | 2 -- testing/automation/GoWebBehavior_test.go | 26 +++++++++++++++++++----- 4 files changed, 42 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 6e48630..abd0c5e 100644 --- a/README.md +++ b/README.md @@ -29,22 +29,27 @@ Deployment will be done with AWS Hosting. After deploying to AWS, visiting the U # Testing -In 492 you will write automated tests. When you do you will need to add a -section that explains how to run them. +All tests are located under in /testing. They are seperated by Behaviorial tests (/automation), and Unit tests (/unit). -The unit tests are in `/test/unit`. -The behavioral tests are in `/test/automation/`. (Cypress) ## Testing Technology -Go compiler has built-in test functionality +Go compiler has built-in test functionality. In addition, we will be using (url link to go-rod) to facillitate behaviorial testing. ## Running Tests -go test <- to run all tests +To run tests, first navigate to the correct directory for the desired testing type. These are either /testing/automation, or /testing/unit. While in these directories, you may run the following commands to test. -go test -v -run test_name <- to run individual test +go test <- to run all tests in current directory + +go test -v -run test_name <- to run individual test within a test file + +go test file_name.go <- to run all tests in a specific file + +## Additional Testing Notes +While running behaviorial testing, if a browser is not detected on your machine, the code will automatically download one for testing. + +If on a Windows machine, the code will generate a temp file that is marked as suspiscous, and will be labeled as a Trojan, and subsequently quarentined. **DO NOT PANIC!** This file is used to control the testing browser in many different ways, and is crucial to get the tests to function correctly. You can safely release the file from quarentine, which will allow the behaviorial tests to correctly function. If on another machine, such as a Linux or Mac, the file will not raise any issues.. Please see (url to go-rod's issues page w the right issue) for more information. -go test file_name.go <- to run tests in a specific file # Authors Ethan Speer: jespeer@email.sc.edu @@ -54,3 +59,5 @@ Trey Sturman: rsturman@email.sc.edu Dan Rochester: rochesw@email.sc.edu Wilson Green: wtgreen@email.sc.edu (account email wilsontgreen@gmail.com) + + diff --git a/go.mod b/go.mod index 4f8b44a..b76e291 100644 --- a/go.mod +++ b/go.mod @@ -3,18 +3,16 @@ module github.com/SCCapstone/BitCrunch go 1.19 require ( - github.com/ysmood/goob v0.4.0 // indirect - github.com/ysmood/gson v0.7.3 // indirect - github.com/ysmood/leakless v0.8.0 // indirect + github.com/gin-gonic/gin v1.8.1 + github.com/go-rod/rod v0.112.3 + golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 ) require ( github.com/gin-contrib/sse v0.1.0 // indirect - github.com/gin-gonic/gin v1.8.1 github.com/go-playground/locales v0.14.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect github.com/go-playground/validator/v10 v10.11.0 // indirect - github.com/go-rod/rod v0.112.3 github.com/goccy/go-json v0.9.11 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/leodido/go-urn v1.2.1 // indirect @@ -23,7 +21,9 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.5 // indirect github.com/ugorji/go/codec v1.2.7 // indirect - golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 + github.com/ysmood/goob v0.4.0 // indirect + github.com/ysmood/gson v0.7.3 // indirect + github.com/ysmood/leakless v0.8.0 // indirect golang.org/x/net v0.0.0-20220909164309-bea034e7d591 // indirect golang.org/x/sys v0.0.0-20220913175220-63ea55921009 // indirect golang.org/x/text v0.3.7 // indirect diff --git a/go.sum b/go.sum index d10d3b8..a56bc56 100644 --- a/go.sum +++ b/go.sum @@ -62,9 +62,7 @@ github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0 github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= github.com/ysmood/goob v0.4.0 h1:HsxXhyLBeGzWXnqVKtmT9qM7EuVs/XOgkX7T6r1o1AQ= github.com/ysmood/goob v0.4.0/go.mod h1:u6yx7ZhS4Exf2MwciFr6nIM8knHQIE22lFpWHnfql18= -github.com/ysmood/got v0.32.0 h1:aAHdQgfgMb/lo4v+OekM+SSqEJYFI035h5YYvLXsVyU= github.com/ysmood/got v0.32.0/go.mod h1:pE1l4LOwOBhQg6A/8IAatkGp7uZjnalzrZolnlhhMgY= -github.com/ysmood/gotrace v0.6.0 h1:SyI1d4jclswLhg7SWTL6os3L1WOKeNn/ZtzVQF8QmdY= github.com/ysmood/gotrace v0.6.0/go.mod h1:TzhIG7nHDry5//eYZDYcTzuJLYQIkykJzCRIo4/dzQM= github.com/ysmood/gson v0.7.3 h1:QFkWbTH8MxyUTKPkVWAENJhxqdBa4lYTQWqZCiLG6kE= github.com/ysmood/gson v0.7.3/go.mod h1:3Kzs5zDl21g5F/BlLTNcuAGAYLKt2lV5G8D1zF3RNmg= diff --git a/testing/automation/GoWebBehavior_test.go b/testing/automation/GoWebBehavior_test.go index 6e4a941..db29fd2 100644 --- a/testing/automation/GoWebBehavior_test.go +++ b/testing/automation/GoWebBehavior_test.go @@ -7,11 +7,14 @@ import ( "os/exec" "runtime" "testing" + + "github.com/go-rod/rod" ) // https://github.com/go-rod/rod/blob/master/examples_test.go func TestMain(m *testing.M) { + fmt.Println("Please note: in order to properly test, please read the Readme!! Otherwise, the browser will NOT connect properly!") fmt.Println("Building and Activating GoWeb...") fmt.Println("Making sure that we are in the correct directory...") cherry := os.Chdir("../../") @@ -40,29 +43,42 @@ func TestMain(m *testing.M) { /* browser := rod.New().MustConnect() // opens up the default browser fmt.Printf("browser") - defer browser.MustClose() // makes sure the browser closes once Tests are complete - page1 := browser.MustPage("localhost") // creates a page from browser + defer browser.MustClose() // makes sure the browser closes once Tests are complete + page1 := browser.MustPage("http://localhost:80/") // creates a page from browser fmt.Printf("page1") fmt.Println(page1) fmt.Printf("defer close") + //openbrowser("localhost") */ - openbrowser("localhost") fmt.Println("GoWeb activated! Begin Testing...") // will be "connecting" using rod within the tests themselves m.Run() fmt.Println("Testing Complete!") - //NOTE: GoWeb.exe isn't removed by the code (TODO), make sure you delete it before running tests! + //NOTE: GoWeb.exe isn't stopped (TODO), make sure you delete it before running more tests! } func TestPageRunning(t *testing.T) { + browser := rod.New().MustConnect() // opens up the default browser + defer func() { + _, err := browser.Pages() + if err != nil { // check to see if the page was rendered at all + t.Errorf("There was an issue rendering the webapp!") + } + browser.MustClose() // On panic (and end), close the browser + }() + browser.MustPage("http://localhost:80/") // creates a page from browser, connects to localhost } func TestLogin(t *testing.T) { // opens up the domain and attempts to login using user1 and pass1 + t.Errorf("Auto fail this!") +} +func TestThetests(t *testing.T) { + t.Errorf("Used to test TestMain's functions, make sure to comment out once done!") } -// Helper function to open correct browser for testee's machine +// Helper function to open correct browser for testee's machine, but does not function properly woth localhost // thanks to https://gist.github.com/hyg/9c4afcd91fe24316cbf0 func openbrowser(url string) { var err error From d0d00f752a08d2fb0f0d3b2e1b53dd627c3f70d5 Mon Sep 17 00:00:00 2001 From: Trey Sturman <70028633+Twofishsticks@users.noreply.github.com> Date: Fri, 27 Jan 2023 16:52:44 -0500 Subject: [PATCH 3/4] Update Readme for testing --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index abd0c5e..7d4ebc8 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ All tests are located under in /testing. They are seperated by Behaviorial tests ## Testing Technology -Go compiler has built-in test functionality. In addition, we will be using (url link to go-rod) to facillitate behaviorial testing. +Go compiler has built-in test functionality. In addition, we will be using ([go-rod](https://github.com/go-rod/rod)) to facillitate behaviorial testing. ## Running Tests To run tests, first navigate to the correct directory for the desired testing type. These are either /testing/automation, or /testing/unit. While in these directories, you may run the following commands to test. @@ -48,7 +48,7 @@ go test file_name.go <- to run all tests in a specific file ## Additional Testing Notes While running behaviorial testing, if a browser is not detected on your machine, the code will automatically download one for testing. -If on a Windows machine, the code will generate a temp file that is marked as suspiscous, and will be labeled as a Trojan, and subsequently quarentined. **DO NOT PANIC!** This file is used to control the testing browser in many different ways, and is crucial to get the tests to function correctly. You can safely release the file from quarentine, which will allow the behaviorial tests to correctly function. If on another machine, such as a Linux or Mac, the file will not raise any issues.. Please see (url to go-rod's issues page w the right issue) for more information. +If on a Windows machine, the code will generate a temp file that is marked as suspiscous, and will be labeled as a Trojan, and subsequently quarentined. **DO NOT PANIC!** This file is used to control the testing browser in many different ways, and is crucial to get the tests to function correctly. You can safely release the file from quarentine, which will allow the behaviorial tests to correctly function. If on another machine, such as a Linux or Mac, the file will not raise any issues.. Please see [this issue in go-rod's github](https://github.com/go-rod/rod/issues/739) for more information. # Authors From 1902c49621d9104c44a086f9588f790ad7a55a05 Mon Sep 17 00:00:00 2001 From: Trey Sturman Date: Sat, 28 Jan 2023 13:41:08 -0500 Subject: [PATCH 4/4] moved a block of comments out --- testing/automation/GoWebBehavior_test.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/testing/automation/GoWebBehavior_test.go b/testing/automation/GoWebBehavior_test.go index db29fd2..25fa382 100644 --- a/testing/automation/GoWebBehavior_test.go +++ b/testing/automation/GoWebBehavior_test.go @@ -40,16 +40,6 @@ func TestMain(m *testing.M) { fmt.Println(fmt.Sprint(err) + ": " + string(output)) return } - /* - browser := rod.New().MustConnect() // opens up the default browser - fmt.Printf("browser") - defer browser.MustClose() // makes sure the browser closes once Tests are complete - page1 := browser.MustPage("http://localhost:80/") // creates a page from browser - fmt.Printf("page1") - fmt.Println(page1) - fmt.Printf("defer close") - //openbrowser("localhost") - */ fmt.Println("GoWeb activated! Begin Testing...") // will be "connecting" using rod within the tests themselves m.Run() fmt.Println("Testing Complete!")