Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /print command to web driver #268

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ require (
github.com/google/go-github/v27 v27.0.4
github.com/mediabuyerbot/go-crx3 v1.3.1
google.golang.org/api v0.7.0
)
)
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.4 h1:87PNWwrRvUSnqS4dlcBU/ftvOIBep4sYuBLlh6rX2wk=
github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
Expand Down
35 changes: 35 additions & 0 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,41 @@ func (wd *remoteWD) Screenshot() ([]byte, error) {
return ioutil.ReadAll(decoder)
}

func (wd *remoteWD) Print(args PrintArgs) ([]byte, error) {
data, err := wd.stringPostCommand("/session/%s/print", args)
if err != nil {
return nil, err
}

// Selenium returns a base64 encoded image.
buf := []byte(data)
decoder := base64.NewDecoder(base64.StdEncoding, bytes.NewBuffer(buf))
return ioutil.ReadAll(decoder)
}

func (wd *remoteWD) stringPostCommand(urlTemplate string, params interface{}) (string, error) {
url := wd.requestURL(urlTemplate, wd.id)
bts, err := json.Marshal(params)
if err != nil {
return "", err
}
response, err := wd.execute("POST", url, bts)
if err != nil {
return "", err
}

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

if reply.Value == nil {
return "", fmt.Errorf("nil return value")
}

return *reply.Value, nil
}

// Condition is an alias for a type that is passed as an argument
// for selenium.Wait(cond Condition) (error) function.
type Condition func(wd WebDriver) (bool, error)
Expand Down
18 changes: 18 additions & 0 deletions selenium.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,22 @@ type PointerAction map[string]interface{}
// Actions stores KeyActions and PointerActions for later execution.
type Actions []map[string]interface{}

// PrintArgs specify the arguments that can
// be sent to the Print() command.
type PrintArgs struct {
Scale float64 `json:"scale,omitempty"`
PageRanges []string `json:"pageRanges,omitempty"`
Margin *Margin `json:"margin,omitempty"`
}

// Margin for the print command.
marwan-at-work marked this conversation as resolved.
Show resolved Hide resolved
marwan-at-work marked this conversation as resolved.
Show resolved Hide resolved
type Margin struct {
Bottom int `json:"bottom"`
Left int `json:"left"`
Right int `json:"right"`
Top int `json:"top"`
}

// WebDriver defines methods supported by WebDriver drivers.
type WebDriver interface {
// Status returns various pieces of information about the server environment.
Expand Down Expand Up @@ -396,6 +412,8 @@ type WebDriver interface {
KeyUp(keys string) error
// Screenshot takes a screenshot of the browser window.
Screenshot() ([]byte, error)
// Print takes PrintArgs and returns a PDF representation of the browser window.
Print(args PrintArgs) ([]byte, error)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will you please add a new test function in internal/seleniumtest/seleniumtest.go. It will be similar to this test and should be added just below it:

func testDismissAlert(t *testing.T, c Config) {

The function call should be added to commonTests, after this line:

t.Run("DismissAlert", runTest(testDismissAlert, c))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@minusnine done

// Log fetches the logs. Log types must be previously configured in the
// capabilities.
//
Expand Down