diff --git a/example_test.go b/example_test.go index e041a1c..89f754b 100644 --- a/example_test.go +++ b/example_test.go @@ -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. @@ -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"), diff --git a/go.mod b/go.mod index cbb519e..80e4ad9 100644 --- a/go.mod +++ b/go.mod @@ -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 -) \ No newline at end of file +) diff --git a/go.sum b/go.sum index 28a9333..7c2d1e3 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/seleniumtest/seleniumtest.go b/internal/seleniumtest/seleniumtest.go index b84f731..300dbd5 100644 --- a/internal/seleniumtest/seleniumtest.go +++ b/internal/seleniumtest/seleniumtest.go @@ -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) { @@ -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 = ` diff --git a/remote.go b/remote.go index 653b5f4..b2ed816 100644 --- a/remote.go +++ b/remote.go @@ -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) diff --git a/selenium.go b/selenium.go index ecd8c91..4349c86 100644 --- a/selenium.go +++ b/selenium.go @@ -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 @@ -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. @@ -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) // Log fetches the logs. Log types must be previously configured in the // capabilities. //