[MISC] Refactor webdriver port initialization (#1491)

This change aims to factorize code introduced in #1467 for webdriver port customisation within the suites.

Co-authored-by: James Elliott <james-d-elliott@users.noreply.github.com>
This commit is contained in:
Amir Zarrinkafsh 2020-11-28 11:06:42 +11:00 committed by GitHub
parent ba04d1072b
commit b786b2e1f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 25 deletions

View File

@ -3,8 +3,6 @@ package suites
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"strconv"
"testing" "testing"
"time" "time"
@ -41,13 +39,7 @@ func (s *NetworkACLSuite) TestShouldAccessSecretUpon1FA() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel() defer cancel()
driverPort := os.Getenv("CHROMEDRIVER_PORT") wds, err := StartWebDriverWithProxy("http://proxy-client1.example.com:3128", GetWebDriverPort())
if driverPort == "" {
driverPort = defaultChromeDriverPort
}
p, _ := strconv.Atoi(driverPort)
wds, err := StartWebDriverWithProxy("http://proxy-client1.example.com:3128", p)
s.Require().NoError(err) s.Require().NoError(err)
defer wds.Stop() //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting. defer wds.Stop() //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
@ -66,13 +58,7 @@ func (s *NetworkACLSuite) TestShouldAccessSecretUpon0FA() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel() defer cancel()
driverPort := os.Getenv("CHROMEDRIVER_PORT") wds, err := StartWebDriverWithProxy("http://proxy-client2.example.com:3128", GetWebDriverPort())
if driverPort == "" {
driverPort = defaultChromeDriverPort
}
p, _ := strconv.Atoi(driverPort)
wds, err := StartWebDriverWithProxy("http://proxy-client2.example.com:3128", p)
s.Require().NoError(err) s.Require().NoError(err)
defer wds.Stop() //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting. defer wds.Stop() //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.

View File

@ -1,5 +1,10 @@
package suites package suites
import (
"os"
"strconv"
)
// GetLoginBaseURL returns the URL of the login portal and the path prefix if specified. // GetLoginBaseURL returns the URL of the login portal and the path prefix if specified.
func GetLoginBaseURL() string { func GetLoginBaseURL() string {
if PathPrefix != "" { if PathPrefix != "" {
@ -8,3 +13,15 @@ func GetLoginBaseURL() string {
return LoginBaseURL return LoginBaseURL
} }
// GetWebDriverPort returns the port to initialize the webdriver with.
func GetWebDriverPort() int {
driverPort := os.Getenv("CHROMEDRIVER_PORT")
if driverPort == "" {
driverPort = defaultChromeDriverPort
}
p, _ := strconv.Atoi(driverPort)
return p
}

View File

@ -7,7 +7,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"strconv"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -73,14 +72,7 @@ func StartWebDriverWithProxy(proxy string, port int) (*WebDriverSession, error)
// StartWebDriver create a selenium session. // StartWebDriver create a selenium session.
func StartWebDriver() (*WebDriverSession, error) { func StartWebDriver() (*WebDriverSession, error) {
driverPort := os.Getenv("CHROMEDRIVER_PORT") return StartWebDriverWithProxy("", GetWebDriverPort())
if driverPort == "" {
driverPort = defaultChromeDriverPort
}
p, _ := strconv.Atoi(driverPort)
return StartWebDriverWithProxy("", p)
} }
// Stop stop the selenium session. // Stop stop the selenium session.