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 all commits
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
4 changes: 2 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func Example() {
// selenium.MousePointer is used to identify the type of the pointer.
// The stored action chain will move the pointer and click on the code
// editor text box on the page.
selenium.StorePointerActions("mouse1",
wd.StorePointerActions("mouse1",
selenium.MousePointer,
// using selenium.FromViewport as the move origin
// which calculates the offset from 0,0.
Expand All @@ -143,7 +143,7 @@ func Example() {
// "keyboard1" is used as a unique virtual device identifier
// for this and future actions.
// The stored action chain will send keyboard inputs to the browser.
selenium.StoreKeyActions("keyboard1",
wd.StoreKeyActions("keyboard1",
selenium.KeyDownAction(selenium.ControlKey),
selenium.KeyPauseAction(50),
selenium.KeyDownAction("a"),
Expand Down
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
16 changes: 16 additions & 0 deletions internal/seleniumtest/seleniumtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func RunCommonTests(t *testing.T, c Config) {
t.Run("ActiveElement", runTest(testActiveElement, c))
t.Run("AcceptAlert", runTest(testAcceptAlert, c))
t.Run("DismissAlert", runTest(testDismissAlert, c))
t.Run("Print", runTest(testPrint, c))
}

func testStatus(t *testing.T, c Config) {
Expand Down Expand Up @@ -1493,6 +1494,21 @@ func testDismissAlert(t *testing.T, c Config) {
}
}

func testPrint(t *testing.T, c Config) {
wd := newRemote(t, newTestCapabilities(t, c), c)
defer quitRemote(t, wd)

homePage := c.ServerURL + "/"

if err := wd.Get(homePage); err != nil {
t.Fatalf("wd.Get(%q) returned error: %v", homePage, err)
}

if _, err := wd.Print(selenium.PrintArgs{}); err != nil {
t.Fatalf("wd.Print() returned error: %v", err)
}
}

var homePage = `
<html>
<head>
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
33 changes: 31 additions & 2 deletions selenium.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ type PointerType string

const (
MousePointer PointerType = "mouse"
PenPointer = "pen"
TouchPointer = "touch"
PenPointer PointerType = "pen"
TouchPointer PointerType = "touch"
)

// PointerMoveOrigin controls how the offset for
Expand All @@ -255,6 +255,33 @@ 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"`
Orientation PrintOrientation `json:"orientation,omitempty"`
}

// PrintOrientation specifies the orientation for
// the print command
type PrintOrientation string

const (
PrintOrientationPortrait PrintOrientation = "portrait"
PrintOrientationLandscape PrintOrientation = "landscape"
)

// Margin for the Print command.
//
// All units are in centimeters.
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 +423,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