From d2c380459e3dcdc4d442a90ffdfcffc007df9981 Mon Sep 17 00:00:00 2001 From: Michael Rea Date: Thu, 15 Oct 2020 16:27:52 -0700 Subject: [PATCH 1/3] Add ElementGetText and ElementSendKeys. --- go/webdriver/webdriver.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/go/webdriver/webdriver.go b/go/webdriver/webdriver.go index 0a91cfd4..a1d9be58 100644 --- a/go/webdriver/webdriver.go +++ b/go/webdriver/webdriver.go @@ -72,6 +72,10 @@ type WebDriver interface { Screenshot(context.Context) (image.Image, error) // ElementScreenshot takes a screenshot of the visible region encompassed by the bounding rectangle of element. ElementScreenshot(ctx context.Context, el WebElement) (image.Image, error) + // ElementGetText gets the text of an element. + ElementGetText(ctx context.Context, el WebElement) (string, error) + // ElementSendKeys sends keys to the element. + ElementSendKeys(ctx context.Context, el WebElement, keys string) error // WindowHandles returns a slice of the current window handles. WindowHandles(context.Context) ([]string, error) // CurrentWindowHandle returns the handle of the active window. @@ -393,6 +397,23 @@ func (d *webDriver) ElementScreenshot(ctx context.Context, el WebElement) (image return png.Decode(base64.NewDecoder(base64.StdEncoding, strings.NewReader(value))) } +// ElementGetText gets the text of an element. +func (d *webDriver) ElementGetText(ctx context.Context, el WebElement) (string, error) { + var value string + if err := d.get(ctx, fmt.Sprintf("element/%s/text", el.ID()), &value); err != nil { + return "", err + } + return value, nil +} + +// ElementSendKeys sends keys to an element. +func (d *webDriver) ElementSendKeys(ctx context.Context, el WebElement, keys string) error { + if err := d.post(ctx, fmt.Sprintf("element/%s/value", el.ID()), map[string]string{"text": keys}, nil); err != nil { + return err + } + return nil +} + // WindowHandles returns a slice of the current window handles. func (d *webDriver) WindowHandles(ctx context.Context) ([]string, error) { var value []string From 0a303806f3a2268e9dc0ebe4b6eb7b9484693282 Mon Sep 17 00:00:00 2001 From: Michael Rea Date: Thu, 15 Oct 2020 16:44:52 -0700 Subject: [PATCH 2/3] Accommodate single-hyphen args, too. args in moz:firefoxOptions use the "-" prefix, compared to args in "goog:chromeOptions" using the "--" prefix. --- go/metadata/capabilities/capabilities.go | 7 +++-- go/metadata/capabilities/capabilities_test.go | 29 +++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/go/metadata/capabilities/capabilities.go b/go/metadata/capabilities/capabilities.go index 63c9d17e..a24d991d 100644 --- a/go/metadata/capabilities/capabilities.go +++ b/go/metadata/capabilities/capabilities.go @@ -720,6 +720,9 @@ func mergeLists(m1, m2 []interface{}) []interface{} { return nl } +// The mergeArgs function merges m1 and m2. For same argument exists in m1 and m2 and has +// prefix "--" or "-", the argument in m2 will override m1; If the same argument does not +// have prefix "--" or "-", then both same arguments in m1 and m2 will remain. func mergeArgs(m1, m2 []interface{}) []interface{} { m2Opts := map[string]bool{} @@ -730,7 +733,7 @@ func mergeArgs(m1, m2 []interface{}) []interface{} { m2Opts[arg[7:]] = true continue // And leave arg out of m2Copy } - if strings.HasPrefix(arg, "--") { + if strings.HasPrefix(arg, "-") { tokens := strings.Split(arg, "=") m2Opts[tokens[0]] = true } @@ -742,7 +745,7 @@ func mergeArgs(m1, m2 []interface{}) []interface{} { for _, a := range m1 { if arg, ok := a.(string); ok { - if strings.HasPrefix(arg, "--") { + if strings.HasPrefix(arg, "-") { tokens := strings.Split(arg, "=") // Skip options from m1 that are redefined in m2 if m2Opts[tokens[0]] { diff --git a/go/metadata/capabilities/capabilities_test.go b/go/metadata/capabilities/capabilities_test.go index 32677a54..e5322153 100644 --- a/go/metadata/capabilities/capabilities_test.go +++ b/go/metadata/capabilities/capabilities_test.go @@ -435,7 +435,6 @@ func TestMerge(t *testing.T) { "an option", "--anOption", "--anOption=true", - "-anotherOption", map[string]interface{}{ "some": "map", }, @@ -450,7 +449,7 @@ func TestMerge(t *testing.T) { }, }, { - name: "args -- redefines", + name: "overrides '--' prefixed args", input1: map[string]interface{}{ "args": []interface{}{ "an option", @@ -531,6 +530,31 @@ func TestMerge(t *testing.T) { }, }, }, + { + name: "overrides '-' prefixed args", + input1: map[string]interface{}{ + "args": []interface{}{ + "option", + "--anOption", + "-width=1024", + }, + }, + input2: map[string]interface{}{ + "args": []interface{}{ + "option", + "--anOption", + "-width=2048", + }, + }, + result: map[string]interface{}{ + "args": []interface{}{ + "option", + "option", + "--anOption", + "-width=2048", + }, + }, + }, { name: "chromeOptions to goog:chromeOptions", input1: map[string]interface{}{ @@ -1600,3 +1624,4 @@ func TestMergeUnder(t *testing.T) { }) } } + From 6408d74273c36145174d15ec1953900ab98f58fa Mon Sep 17 00:00:00 2001 From: Michael Rea Date: Thu, 15 Oct 2020 17:00:40 -0700 Subject: [PATCH 3/3] Detail error origin when launching a driver. --- go/wsl/hub/hub.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go/wsl/hub/hub.go b/go/wsl/hub/hub.go index 55c4f225..1764ec62 100644 --- a/go/wsl/hub/hub.go +++ b/go/wsl/hub/hub.go @@ -155,7 +155,7 @@ func (h *Hub) newSessionFromCaps(ctx context.Context, caps *capabilities.Capabil if err := resolver.RecyclePorts(); err != nil { log.Printf("Error recycling ports: %v", err) } - return "", nil, err + return "", nil, fmt.Errorf("Error launching driver binary: %w", err) } s, err := d.NewSession(ctx, caps, w) @@ -165,7 +165,7 @@ func (h *Hub) newSessionFromCaps(ctx context.Context, caps *capabilities.Capabil if err := d.Shutdown(ctx); err != nil { log.Printf("Error shutting down driver: %v", err) } - return "", nil, err + return "", nil, fmt.Errorf("Error requesting new session: %w", err) } return s, d, nil