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) {
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"])
}
}

func RunChromeTests(t *testing.T, c Config) {
// Chrome-specific tests.
t.Run("Extension", runTest(testChromeExtension, c))
t.Run("CDP", runTest(testChromeCdp, c))
}
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)
func (wd *remoteWD) execCdpCommand(cmd string, params map[string]interface{}) (interface{}, error) {
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)
}
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
// 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