Skip to content

Commit

Permalink
Ability to set custom screen resolution. Fix #35.
Browse files Browse the repository at this point in the history
  • Loading branch information
aandryashin committed Apr 7, 2017
1 parent bce7aa0 commit 202f915
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 19 deletions.
2 changes: 1 addition & 1 deletion doc.go
@@ -1,4 +1,4 @@
/*
Selenoid is a powerful implementation of Selenium Hub using Docker or standalone web driver binaries to start and launch browsers. Documentation has moved to: http://github.com/aandryashin/selenoid
Selenoid is a powerful implementation of Selenium Hub using Docker or standalone web driver binaries to start and launch browsers. Documentation has moved to: http://github.com/aandryashin/selenoid
*/
package main
16 changes: 13 additions & 3 deletions selenoid.go
Expand Up @@ -10,6 +10,7 @@ import (
"net"
"net/http"
"net/http/httputil"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -79,8 +80,9 @@ func create(w http.ResponseWriter, r *http.Request) {
}
var browser struct {
Caps struct {
Name string `json:"browserName"`
Version string `json:"version"`
Name string `json:"browserName"`
Version string `json:"version"`
ScreenResolution string `json:"screenResolution"`
} `json:"desiredCapabilities"`
}
err = json.Unmarshal(body, &browser)
Expand All @@ -90,7 +92,15 @@ func create(w http.ResponseWriter, r *http.Request) {
queue.Drop()
return
}
starter, ok := manager.Find(browser.Caps.Name, &browser.Caps.Version)
if browser.Caps.ScreenResolution != "" {
exp := regexp.MustCompile(`^[0-9]+x[0-9]+x(8|16|24)$`)
if !exp.MatchString(browser.Caps.ScreenResolution) {
http.Error(w, "Malformed screenResolution capability.", http.StatusBadRequest)
queue.Drop()
return
}
}
starter, ok := manager.Find(browser.Caps.Name, &browser.Caps.Version, browser.Caps.ScreenResolution)
if !ok {
log.Printf("[ENVIRONMENT_NOT_AVAILABLE] [%s-%s]\n", browser.Caps.Name, browser.Caps.Version)
http.Error(w, "Requested environment is not available", http.StatusBadRequest)
Expand Down
10 changes: 10 additions & 0 deletions selenoid_test.go
Expand Up @@ -62,6 +62,16 @@ func TestBrowserNotFound(t *testing.T) {
AssertThat(t, queue.Used(), EqualTo{0})
}

func TestMalformedScreeResolutionCapability(t *testing.T) {
manager = &BrowserNotFound{}

rsp, err := http.Post(With(srv.URL).Path("/wd/hub/session"), "", bytes.NewReader([]byte(`{"desiredCapabilities":{"screenResolution":"1024x768"}}`)))
AssertThat(t, err, Is{nil})
AssertThat(t, rsp, Code{http.StatusBadRequest})

AssertThat(t, queue.Used(), EqualTo{0})
}

func TestNewSessionNotFound(t *testing.T) {
manager = &HTTPTest{Handler: Selenium()}

Expand Down
14 changes: 9 additions & 5 deletions service/docker.go
Expand Up @@ -20,10 +20,11 @@ import (

// Docker - docker container manager
type Docker struct {
IP string
Client *client.Client
Service *config.Browser
LogConfig *container.LogConfig
IP string
Client *client.Client
Service *config.Browser
LogConfig *container.LogConfig
ScreenResolution string
}

// StartWithCancel - Starter interface implementation
Expand All @@ -34,7 +35,10 @@ func (docker *Docker) StartWithCancel() (*url.URL, func(), error) {
}
ctx := context.Background()
log.Println("Creating Docker container", docker.Service.Image, "...")
env := []string{fmt.Sprintf("TZ=%s", time.Local)}
env := []string{
fmt.Sprintf("TZ=%s", time.Local),
fmt.Sprintf("SCREEN_RESOLUTION=%s", docker.ScreenResolution),
}
resp, err := docker.Client.ContainerCreate(ctx,
&container.Config{
Hostname: "localhost",
Expand Down
12 changes: 6 additions & 6 deletions service/service.go
Expand Up @@ -18,18 +18,18 @@ type Starter interface {

// Manager - interface to choose appropriate starter
type Manager interface {
Find(s string, v *string) (Starter, bool)
Find(s string, v *string, sr string) (Starter, bool)
}

// DefaultManager - struct for default implementation
type DefaultManager struct {
IP string
Client *client.Client
Config *config.Config
IP string
Client *client.Client
Config *config.Config
}

// Find - default implementation Manager interface
func (m *DefaultManager) Find(s string, v *string) (Starter, bool) {
func (m *DefaultManager) Find(s string, v *string, sr string) (Starter, bool) {
log.Printf("Locating the service for %s %s\n", s, *v)
service, ok := m.Config.Find(s, v)
if !ok {
Expand All @@ -41,7 +41,7 @@ func (m *DefaultManager) Find(s string, v *string) (Starter, bool) {
return nil, false
}
log.Printf("Using docker service for %s %s\n", s, *v)
return &Docker{m.IP, m.Client, service, m.Config.ContainerLogs}, true
return &Docker{m.IP, m.Client, service, m.Config.ContainerLogs, sr}, true
case []interface{}:
log.Printf("Using driver service for %s %s\n", s, *v)
return &Driver{service}, true
Expand Down
9 changes: 5 additions & 4 deletions utils_test.go
Expand Up @@ -11,9 +11,10 @@ import (
"strings"
"sync"

"time"

"github.com/aandryashin/selenoid/service"
"github.com/pborman/uuid"
"time"
)

type HTTPTest struct {
Expand Down Expand Up @@ -52,7 +53,7 @@ func (m *HTTPTest) StartWithCancel() (*url.URL, func(), error) {
}, nil
}

func (m *HTTPTest) Find(s string, v *string) (service.Starter, bool) {
func (m *HTTPTest) Find(s string, v *string, sr string) (service.Starter, bool) {
return m, true
}

Expand All @@ -64,13 +65,13 @@ func (m *StartupError) StartWithCancel() (*url.URL, func(), error) {
return nil, nil, errors.New("Failed to start Service")
}

func (m *StartupError) Find(s string, v *string) (service.Starter, bool) {
func (m *StartupError) Find(s string, v *string, sr string) (service.Starter, bool) {
return m, true
}

type BrowserNotFound struct{}

func (m *BrowserNotFound) Find(s string, v *string) (service.Starter, bool) {
func (m *BrowserNotFound) Find(s string, v *string, sr string) (service.Starter, bool) {
return nil, false
}

Expand Down

0 comments on commit 202f915

Please sign in to comment.