Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions internal/seleniumtest/seleniumtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -1700,7 +1700,29 @@ func testChromeExtension(t *testing.T, c Config) {
}
}

func testChromeCdp(t *testing.T, c Config) {
Comment thread
wanmail marked this conversation as resolved.
Outdated
caps := newTestCapabilities(t, c)

wd, err := NewRemote(t, caps, c.Addr)
if err != nil {
t.Fatalf("newRemote(_, _) returned error: %v", err)
}
defer wd.Quit()

res, err := wd.ExecuteCdpCommand("Browser.getVersion", nil)
if err != nil {
t.Fatalf("cdp execute error: %s", err.Error())
}

if data, ok := res.(map[string]interface{}); !ok {
t.Fatalf("cdp execute failed with result: %v", res)
} else {
t.Log(data["product"])
Comment thread
wanmail marked this conversation as resolved.
Outdated
}
}

func RunChromeTests(t *testing.T, c Config) {
// Chrome-specific tests.
t.Run("Extension", runTest(testChromeExtension, c))
t.Run("CDP", runTest(testChromeCdp, c))
Comment thread
wanmail marked this conversation as resolved.
Outdated
}
40 changes: 40 additions & 0 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,46 @@ func (wd *remoteWD) execScript(script string, args []interface{}, suffix string)
return reply.Value, nil
}

func (wd *remoteWD) execCdpCommandRaw(data []byte) ([]byte, error) {
return wd.execute("POST", wd.requestURL("/session/%s/goog/cdp/execute", wd.id), data)
}

// execCdpCommand execute cdp command
// this interface didn't define in official wiki(https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol)
// so I just refered to selenium in python (selenium.webdriver.chrome.remote_connection.ChromeRemoteConnection)
Comment thread
wanmail marked this conversation as resolved.
Outdated
func (wd *remoteWD) execCdpCommand(cmd string, params map[string]interface{}) (interface{}, error) {
Comment thread
wanmail marked this conversation as resolved.
Outdated
if params == nil {
params = make(map[string]interface{})
}

data, err := json.Marshal(map[string]interface{}{
"cmd": cmd,
"params": params,
})
if err != nil {
return nil, err
}

response, err := wd.execCdpCommandRaw(data)
if err != nil {
return nil, err
}

reply := new(struct{ Value interface{} })
if err = json.Unmarshal(response, reply); err != nil {
return nil, err
}

return reply.Value, nil
}

func (wd *remoteWD) ExecuteCdpCommand(cmd string, params map[string]interface{}) (interface{}, error) {
if wd.browser != "chrome" {
return nil, fmt.Errorf("cdp command must execute in chrome not %s", wd.browser)
Comment thread
wanmail marked this conversation as resolved.
Outdated
}
return wd.execCdpCommand(cmd, params)
}

func (wd *remoteWD) ExecuteScript(script string, args []interface{}) (interface{}, error) {
if !wd.w3cCompatible {
return wd.execScript(script, args, "")
Expand Down
4 changes: 4 additions & 0 deletions selenium.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ type WebDriver interface {
// perform JSON decoding.
ExecuteScriptAsyncRaw(script string, args []interface{}) ([]byte, error)

// ExecuteCdpCommand execute Chrome Devtools Protocol command and get returned result
Comment thread
wanmail marked this conversation as resolved.
Outdated
// refer to link https://chromedevtools.github.io/devtools-protocol/
ExecuteCdpCommand(cmd string, params map[string]interface{}) (interface{}, error)

// WaitWithTimeoutAndInterval waits for the condition to evaluate to true.
WaitWithTimeoutAndInterval(condition Condition, timeout, interval time.Duration) error

Expand Down