Skip to content

Commit

Permalink
Add new WebDriver methods, better arg merging and error logging (#414)
Browse files Browse the repository at this point in the history
* Add ElementGetText and ElementSendKeys.

* Accommodate single-hyphen args, too.

args in moz:firefoxOptions use the "-" prefix, compared to args in
"goog:chromeOptions" using the "--" prefix.

* Detail error origin when launching a driver.
  • Loading branch information
mtrea committed Oct 16, 2020
1 parent 83283f5 commit 06023bb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
7 changes: 5 additions & 2 deletions go/metadata/capabilities/capabilities.go
Expand Up @@ -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{}

Expand All @@ -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
}
Expand All @@ -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]] {
Expand Down
29 changes: 27 additions & 2 deletions go/metadata/capabilities/capabilities_test.go
Expand Up @@ -435,7 +435,6 @@ func TestMerge(t *testing.T) {
"an option",
"--anOption",
"--anOption=true",
"-anotherOption",
map[string]interface{}{
"some": "map",
},
Expand All @@ -450,7 +449,7 @@ func TestMerge(t *testing.T) {
},
},
{
name: "args -- redefines",
name: "overrides '--' prefixed args",
input1: map[string]interface{}{
"args": []interface{}{
"an option",
Expand Down Expand Up @@ -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{}{
Expand Down Expand Up @@ -1600,3 +1624,4 @@ func TestMergeUnder(t *testing.T) {
})
}
}

21 changes: 21 additions & 0 deletions go/webdriver/webdriver.go
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go/wsl/hub/hub.go
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 06023bb

Please sign in to comment.