Skip to content

Commit

Permalink
add Element.Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Feb 12, 2020
1 parent 2cebc7d commit ad6ffb4
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 11 deletions.
11 changes: 9 additions & 2 deletions browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,21 @@ func (b *Browser) PageE(url string) (*Page, error) {
target, err := b.Call(&cdp.Message{
Method: "Target.createTarget",
Params: cdp.Object{
"url": url,
"url": "about:blank",
},
})
if err != nil {
return nil, err
}

return b.page(target.Get("targetId").String())
page, err := b.page(target.Get("targetId").String())
if err != nil {
return nil, err
}

page.Navigate(url)

return page, nil
}

// Page creates a new page
Expand Down
56 changes: 50 additions & 6 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rod

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"path/filepath"
Expand Down Expand Up @@ -45,7 +46,7 @@ func (el *Element) CancelTimeout() {

// DescribeE ...
func (el *Element) DescribeE() (kit.JSONResult, error) {
node, err := el.page.Ctx(el.ctx).Call(
val, err := el.page.Ctx(el.ctx).Call(
"DOM.describeNode",
cdp.Object{
"objectId": el.ObjectID,
Expand All @@ -54,16 +55,16 @@ func (el *Element) DescribeE() (kit.JSONResult, error) {
if err != nil {
return nil, err
}
return node, nil
node := val.Get("node")
return &node, nil
}

// Describe returns the element info
// Returned json: https://chromedevtools.github.io/devtools-protocol/tot/DOM#type-Node
func (el *Element) Describe() kit.JSONResult {
info, err := el.DescribeE()
node, err := el.DescribeE()
kit.E(err)
val := info.Get("node")
return &val
return node
}

// FrameE ...
Expand All @@ -74,7 +75,7 @@ func (el *Element) FrameE() (*Page, error) {
}

newPage := *el.page
newPage.FrameID = node.Get("node.frameId").String()
newPage.FrameID = node.Get("frameId").String()
newPage.element = el

return &newPage, newPage.initIsolatedWorld()
Expand Down Expand Up @@ -399,6 +400,49 @@ func (el *Element) Box() kit.JSONResult {
return box
}

// ResourceE ...
func (el *Element) ResourceE() ([]byte, error) {
src, err := el.EvalE(true, `() => new Promise((resolve, reject) => {
if (this.complete) {
return resolve(this.src)
}
this.addEventListener('onload', () => resolve(this.src))
this.addEventListener('onerror', (e) => reject(e))
})`)
if err != nil {
return nil, err
}

res, err := el.page.Call("Page.getResourceContent", cdp.Object{
"frameId": el.page.FrameID,
"url": src.String(),
})
if err != nil {
return nil, err
}

data := res.Get("content").String()

var bin []byte
if res.Get("base64Encoded").Bool() {
bin, err = base64.StdEncoding.DecodeString(data)
if err != nil {
return nil, err
}
} else {
bin = []byte(data)
}

return bin, nil
}

// Resource returns the binary of the "src" properly, such as the image or audio file.
func (el *Element) Resource() []byte {
bin, err := el.ResourceE()
kit.E(err)
return bin
}

// EvalE ...
func (el *Element) EvalE(byValue bool, js string, params ...interface{}) (kit.JSONResult, error) {
return el.page.EvalE(byValue, el.ObjectID, js, params)
Expand Down
5 changes: 5 additions & 0 deletions element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ func (s *S) TestWaitStable() {
p.Has("[event=click]")
}

func (s *S) TestResource() {
p := s.page.Navigate(s.htmlFile("fixtures/resource.html"))
s.Equal(15148, len(p.Element("img").Resource()))
}

func (s *S) TestFnErr() {
p := s.page.Navigate(s.htmlFile("fixtures/click.html"))
el := p.Element("button")
Expand Down
2 changes: 2 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const (
ErrExpectElements = "expect js to return an array of elements"
// ErrElementNotFound error code
ErrElementNotFound = "cannot find element"
// ErrSrcNotFound error code
ErrSrcNotFound = "element doesn't have src attribute"
)

// Error ...
Expand Down
5 changes: 5 additions & 0 deletions fixtures/resource.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<img src="./rod.png" alt="img">
</body>
</html>
Binary file added fixtures/rod.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 4 additions & 3 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ type Page struct {
TargetID string
SessionID string
ContextID int64
FrameID string

// devices
Mouse *Mouse
Keyboard *Keyboard

// iframe only
FrameID string
element *Element

timeoutCancel func()
Expand Down Expand Up @@ -57,9 +57,10 @@ func (p *Page) CancelTimeout() {

// NavigateE ...
func (p *Page) NavigateE(url string) error {
_, err := p.Call("Page.navigate", cdp.Object{
res, err := p.Call("Page.navigate", cdp.Object{
"url": url,
})
p.FrameID = res.Get("frameId").String()
return err
}

Expand Down Expand Up @@ -447,7 +448,7 @@ func (p *Page) initSession() error {
}

func (p *Page) isIframe() bool {
return p.FrameID != ""
return p.element != nil
}

func (p *Page) rootFrame() *Page {
Expand Down

0 comments on commit ad6ffb4

Please sign in to comment.