Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hupe1980 committed Feb 21, 2023
1 parent cb98b18 commit 52f5aff
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/

chromedriver
70 changes: 40 additions & 30 deletions capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,8 @@ package webdriver

type Capabilities map[string]interface{}

// NewCapabilities returns a Capabilities instance with any provided features enabled.
func NewCapabilities(features ...string) Capabilities {
c := Capabilities{}

for _, feature := range features {
c.With(feature)
}

return c
}

// BrowserName sets the desired browser name.
func (c Capabilities) BrowserName(name string) Capabilities {
// SetBrowserName sets the desired browser name.
func (c Capabilities) SetBrowserName(name string) Capabilities {
c["browserName"] = name
return c
}
Expand All @@ -36,32 +25,25 @@ type ProxyConfig struct {
}

// Proxy sets the desired proxy configuration.
func (c Capabilities) Proxy(p ProxyConfig) Capabilities {
func (c Capabilities) SetProxy(p ProxyConfig) Capabilities {
c["proxy"] = p
return c
}

// BrowserVersion sets the desired browser version.
func (c Capabilities) BrowserVersion(version string) Capabilities {
// SetBrowserVersion sets the desired browser version.
func (c Capabilities) SetBrowserVersion(version string) Capabilities {
c["browserVersion"] = version
return c
}

// PlatformName sets the desired browser platform.
func (c Capabilities) PlatformName(platform string) Capabilities {
// SetPlatformName sets the desired browser platform.
func (c Capabilities) SetPlatformName(platform string) Capabilities {
c["platformName"] = platform
return c
}

// With enables the provided feature (ex. "acceptInsecureCerts").
func (c Capabilities) With(feature string) Capabilities {
c[feature] = true
return c
}

// Without disables the provided feature (ex. "acceptInsecureCerts").
func (c Capabilities) Without(feature string) Capabilities {
c[feature] = false
func (c Capabilities) SetAcceptInsecureCerts(acceptInsecureCerts bool) Capabilities {
c["acceptInsecureCerts"] = acceptInsecureCerts
return c
}

Expand All @@ -71,6 +53,10 @@ func (c Capabilities) Set(key string, value string) Capabilities {
return c
}

/****************************************************************************************************************
* Chrome Options *
****************************************************************************************************************/

type ChromeOptions map[string]interface{}

func (co ChromeOptions) AddArg(arg string) ChromeOptions {
Expand All @@ -83,12 +69,36 @@ func (co ChromeOptions) AddArg(arg string) ChromeOptions {
return co
}

func (co ChromeOptions) Binary(binaryLocation string) ChromeOptions {
co["binary"] = binaryLocation
func (co ChromeOptions) SetBinary(binary string) ChromeOptions {
co["binary"] = binary
return co
}

func (c Capabilities) ChromeOptions(co ChromeOptions) Capabilities {
func (co ChromeOptions) Binary() string {
if val, ok := co["binary"]; ok {
return val.(string)
}

return ""
}

func (co ChromeOptions) DebuggerAddress() string {
if val, ok := co["debuggerAddress"]; ok {
return val.(string)
}

return ""
}

func (c Capabilities) SetChromeOptions(co ChromeOptions) Capabilities {
c["goog:chromeOptions"] = co
return c
}

func (c Capabilities) ChromeOptions() ChromeOptions {
if opts, ok := c["goog:chromeOptions"]; ok {
return opts.(map[string]interface{})
}

return nil
}
4 changes: 2 additions & 2 deletions capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

func TestCapabilities(t *testing.T) {
caps := NewCapabilities()
caps.BrowserName("chrome")
caps := Capabilities{}
caps.SetBrowserName("chrome")
assert.Equal(t, "chrome", caps["browserName"])
}

Expand Down
4 changes: 2 additions & 2 deletions chromedriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func (d *ChromeDriver) NewSession(optFns ...func(o *SessionOptions)) (*Session,
}

func newDefaultChromeDriverCapabilities() Capabilities {
caps := NewCapabilities()
caps.BrowserName("chrome")
caps := Capabilities{}
caps.SetBrowserName("chrome")

return caps
}

0 comments on commit 52f5aff

Please sign in to comment.