Skip to content

Commit

Permalink
Element.ShadowRoot returns error when ShadowRoots is empty (#788)
Browse files Browse the repository at this point in the history
* Element.ShadowRoot returns an error when ShadowRoots slice is empty

* comment exported ErrNoShadowRoot

* unit test shadow root error

* test shadow root error contains correct error message
  • Loading branch information
pcen committed Jan 14, 2023
1 parent 499a17c commit cc9664f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions element.go
Expand Up @@ -402,6 +402,9 @@ func (el *Element) ShadowRoot() (*Element, error) {
}

// though now it's an array, w3c changed the spec of it to be a single.
if len(node.ShadowRoots) == 0 {
return nil, &ErrNoShadowRoot{el}
}
id := node.ShadowRoots[0].BackendNodeID

shadowNode, err := proto.DOMResolveNode{BackendNodeID: id}.Call(el)
Expand Down
5 changes: 5 additions & 0 deletions element_test.go
Expand Up @@ -284,6 +284,11 @@ func TestShadowDOM(t *testing.T) {
g.mc.stubErr(1, proto.DOMResolveNode{})
el.MustShadowRoot()
})

elNoShadow := p.MustElement("script")
_, err := elNoShadow.ShadowRoot()
g.True((&rod.ErrNoShadowRoot{}).Is(err))
g.Has(err.Error(), "element has no shadow root:")
}

func TestInputTime(t *testing.T) {
Expand Down
13 changes: 13 additions & 0 deletions error.go
Expand Up @@ -181,3 +181,16 @@ type ErrPageNotFound struct {
func (e *ErrPageNotFound) Error() string {
return "cannot find page"
}

// ErrNoShadowRoot error
type ErrNoShadowRoot struct {
*Element
}

// Error ...
func (e *ErrNoShadowRoot) Error() string {
return fmt.Sprintf("element has no shadow root: %s", e.String())
}

// Is interface
func (e *ErrNoShadowRoot) Is(err error) bool { _, ok := err.(*ErrNoShadowRoot); return ok }

0 comments on commit cc9664f

Please sign in to comment.