Skip to content

Commit

Permalink
add Element.Screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Mar 9, 2020
1 parent 1cf4a66 commit 09ee7c4
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 3 deletions.
35 changes: 35 additions & 0 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,41 @@ func (el *Element) ResourceE() ([]byte, error) {
return bin, nil
}

// ScreenshotE of the area of the element
func (el *Element) ScreenshotE(format string, quality int) ([]byte, error) {
err := el.WaitVisibleE()
if err != nil {
return nil, err
}

err = el.ScrollIntoViewIfNeededE()
if err != nil {
return nil, err
}

box, err := el.BoxE()
if err != nil {
return nil, err
}

opts := cdp.Object{
"format": format,
"clip": cdp.Object{
"x": box.Left,
"y": box.Top,
"width": box.Width,
"height": box.Height,
"scale": 1,
},
}

if quality > -1 {
opts["quality"] = quality
}

return el.page.Root().ScreenshotE(opts)
}

// ReleaseE doc is the same as the method Release
func (el *Element) ReleaseE() error {
return el.page.Context(el.ctx).ReleaseE(el.ObjectID)
Expand Down
12 changes: 12 additions & 0 deletions element_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package rod_test

import (
"bytes"
"context"
"errors"
"image/png"
"path/filepath"
"time"

Expand Down Expand Up @@ -164,6 +166,16 @@ func (s *S) TestResource() {
s.Equal(15148, len(p.Element("img").Resource()))
}

func (s *S) TestElementScreenshot() {
p := s.page.Navigate(srcFile("fixtures/click.html"))

data := p.Element("h4").Screenshot()
img, err := png.Decode(bytes.NewBuffer(data))
kit.E(err)
s.EqualValues(200, img.Bounds().Dx())
s.EqualValues(30, img.Bounds().Dy())
}

func (s *S) TestUseReleasedElement() {
p := s.page.Navigate(srcFile("fixtures/click.html"))
btn := p.Element("button")
Expand Down
4 changes: 4 additions & 0 deletions fixtures/click.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<html>
<style>
h4 {
width: 200px;
height: 30px;
}
button {
margin: 100px;
}
Expand Down
5 changes: 2 additions & 3 deletions page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,12 @@ func (s *S) TestPagePause() {

func (s *S) TestPageScreenshot() {
p := s.page.Navigate(srcFile("fixtures/click.html"))
p.Viewport(400, 300, 1, false)
p.Element("button")
data := p.Screenshot()
img, err := png.Decode(bytes.NewBuffer(data))
kit.E(err)
s.Equal(400, img.Bounds().Dx())
s.Equal(300, img.Bounds().Dy())
s.Equal(800, img.Bounds().Dx())
s.Equal(600, img.Bounds().Dy())
}

func (s *S) TestPageTraceDir() {
Expand Down
1 change: 1 addition & 0 deletions rod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func Test(t *testing.T) {
defer s.browser.Close()

s.page = s.browser.Page("")
s.page.Viewport(800, 600, 1, false)

suite.Run(t, s)
}
7 changes: 7 additions & 0 deletions sugar.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,13 @@ func (el *Element) Resource() []byte {
return bin
}

// Screenshot of the area of the element
func (el *Element) Screenshot() []byte {
bin, err := el.ScreenshotE("png", -1)
kit.E(err)
return bin
}

// Release remote object on browser
func (el *Element) Release() {
kit.E(el.ReleaseE())
Expand Down

0 comments on commit 09ee7c4

Please sign in to comment.