Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #393 from aandryashin/master
Browse files Browse the repository at this point in the history
W3C protocol session creation
  • Loading branch information
vania-pooh authored Mar 25, 2018
2 parents fff0de2 + 9a11aeb commit 3225edd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
11 changes: 9 additions & 2 deletions selenoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ import (

"crypto/rand"
"encoding/hex"

"github.com/aerokube/selenoid/session"
"github.com/aerokube/util"
"github.com/docker/docker/api/types"
"golang.org/x/net/websocket"
"github.com/docker/docker/pkg/stdcopy"
"golang.org/x/net/websocket"
)

var (
Expand Down Expand Up @@ -126,7 +127,10 @@ func create(w http.ResponseWriter, r *http.Request) {
return
}
var browser struct {
Caps session.Caps `json:"desiredCapabilities"`
Caps session.Caps `json:"desiredCapabilities"`
W3CCaps struct {
Caps session.Caps `json:"alwaysMatch"`
} `json:"capabilities"`
}
err = json.Unmarshal(body, &browser)
if err != nil {
Expand All @@ -135,6 +139,9 @@ func create(w http.ResponseWriter, r *http.Request) {
queue.Drop()
return
}
if browser.W3CCaps.Caps.Name != "" {
browser.Caps = browser.W3CCaps.Caps
}
browser.Caps.ProcessExtensionCapabilities()
resolution, err := getScreenResolution(browser.Caps.ScreenResolution)
if err != nil {
Expand Down
24 changes: 22 additions & 2 deletions selenoid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"github.com/aerokube/selenoid/config"
"io/ioutil"
"log"
"net/http"
Expand All @@ -14,10 +13,13 @@ import (
"testing"
"time"

"github.com/aerokube/selenoid/config"

"encoding/json"
"path/filepath"

. "github.com/aandryashin/matchers"
. "github.com/aandryashin/matchers/httpresp"
"path/filepath"
)

var (
Expand Down Expand Up @@ -188,6 +190,24 @@ func TestSessionCreated(t *testing.T) {
queue.Release()
}

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

resp, err := http.Post(With(srv.URL).Path("/wd/hub/session"), "", bytes.NewReader([]byte(`{"capabilities":{"alwaysMatch":{"acceptInsecureCerts":true,"browserName":"firefox"}}}`)))
AssertThat(t, err, Is{nil})
var sess map[string]string
AssertThat(t, resp, AllOf{Code{http.StatusOK}, IsJson{&sess}})

resp, err = http.Get(With(srv.URL).Path("/status"))
AssertThat(t, err, Is{nil})
var state config.State
AssertThat(t, resp, AllOf{Code{http.StatusOK}, IsJson{&state}})
AssertThat(t, state.Used, EqualTo{1})
AssertThat(t, queue.Used(), EqualTo{1})
sessions.Remove(sess["sessionId"])
queue.Release()
}

func TestSessionCreatedWdHub(t *testing.T) {
root := http.NewServeMux()
root.Handle("/wd/hub/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
5 changes: 2 additions & 3 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ type Caps struct {
HostsEntries string `json:"hostsEntries"`
Labels string `json:"labels"`
ExtensionCapabilities map[string]interface{} `json:"selenoid:options"`
AlwaysMatch map[string]interface{} `json:"alwaysMatch"`
}

func (c *Caps) ProcessExtensionCapabilities() {
if c.W3CVersion != "" {
c.Version = c.W3CVersion
}
if len(c.ExtensionCapabilities) > 0 || len(c.AlwaysMatch) > 0 {
if len(c.ExtensionCapabilities) > 0 {
s := reflect.ValueOf(c).Elem()

tagToFieldMap := make(map[string]reflect.StructField)
Expand All @@ -43,7 +42,7 @@ func (c *Caps) ProcessExtensionCapabilities() {
}

//NOTE: entries from the first maps have less priority than then next ones
nestedMaps := []map[string]interface{}{c.ExtensionCapabilities, c.AlwaysMatch}
nestedMaps := []map[string]interface{}{c.ExtensionCapabilities}
for _, nm := range nestedMaps {
for k, v := range nm {
value := reflect.ValueOf(v)
Expand Down
14 changes: 7 additions & 7 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (

"time"

"testing"

. "github.com/aandryashin/matchers"
"github.com/aerokube/selenoid/service"
"github.com/aerokube/selenoid/session"
"github.com/pborman/uuid"
"testing"
)

type HTTPTest struct {
Expand Down Expand Up @@ -128,10 +129,8 @@ func Selenium() http.Handler {

func TestProcessExtensionCapabilities(t *testing.T) {
capsJson := `{
"browserVersion": "57.0",
"alwaysMatch": {
"browserName": "firefox"
},
"version": "57.0",
"browserName": "firefox",
"selenoid:options": {
"name": "ExampleTestName",
"enableVNC": true,
Expand All @@ -142,9 +141,10 @@ func TestProcessExtensionCapabilities(t *testing.T) {
var caps session.Caps
err := json.Unmarshal([]byte(capsJson), &caps)
AssertThat(t, err, Is{nil})
AssertThat(t, caps.Name, EqualTo{""})
AssertThat(t, caps.Version, EqualTo{""})
AssertThat(t, caps.Name, EqualTo{"firefox"})
AssertThat(t, caps.Version, EqualTo{"57.0"})
AssertThat(t, caps.TestName, EqualTo{""})

caps.ProcessExtensionCapabilities()
AssertThat(t, caps.Name, EqualTo{"firefox"})
AssertThat(t, caps.Version, EqualTo{"57.0"})
Expand Down

0 comments on commit 3225edd

Please sign in to comment.