Skip to content

Optional boolean fields with default true have unintended omission from CDP commands #1196

@LQR471814

Description

@LQR471814

Rod Version: v0.116.2

The code to demonstrate your question

package rod_test

import (
	"testing"

	"github.com/go-rod/rod/lib/proto"
)

// This is the template to demonstrate how to test Rod.
func TestRod(t *testing.T) {
	g := setup(t)
	g.cancelTimeout()

	page := g.page

	err := proto.AccessibilityEnable{}.Call(page)
	if err != nil {
		panic(err)
	}

	wait := page.EachEvent(
		func(e *proto.AccessibilityLoadComplete) bool {
			res, err := proto.AccessibilityGetPartialAXTree{
				BackendNodeID:  e.Root.BackendDOMNodeID,
				FetchRelatives: false,
			}.Call(page)
			if err != nil {
				panic(err)
			}
			g.Len(res.Nodes, 1)
			return true
		},
	)

	page.MustNavigate(g.html(doc)).MustWaitLoad()

	wait()
}

const doc = `
<html>
  <body>ok</body>
</html>
`

What you got

parallel test 12
=== RUN   TestRod
=== PAUSE TestRod
=== CONT  TestRod
    rod_test.go:30:  ⦗expect len⦘ 3 ⦗to be⦘ 1
--- FAIL: TestRod (0.73s)
FAIL
exit status 1
FAIL    github.com/go-rod/rod   1.164s

What you expect to see

The CDP command Accessibility.getPartialAXTree, should only return one node (the node with the given BackendDOMNodeID) and not its relatives when fetchRelatives is set to false. This is not the case currently.

As of now, there is effectively no way to send the Accessibility.getPartialAXTree (and other similar commands with optional boolean fields) with fetchRelatives set to false.

What have you tried to solve the question

Since the default value for fetchRelatives if it is missing is true and setting the field to false will cause the field to be deleted because of json:..,omitempty, the behavior of the command is incorrect.

The remedy for this specific case is to turn the type of the field from bool to *bool, similar to what you have already done with optional fields that have an int type with a non-zero default value in other commands.

A PR that addresses this is linked here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestions related to rod

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions