playwright-go/tests/selectors_test.go
Remington Arneson fcd06e1fa6
Some checks failed
Go / Lint (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, oldstable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, oldstable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, oldstable, windows-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, stable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, stable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, stable, windows-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, oldstable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, oldstable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, oldstable, windows-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, stable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, stable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, stable, windows-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, oldstable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, oldstable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, oldstable, windows-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, stable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, stable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, stable, windows-latest) (push) Has been cancelled
Go / test-examples (push) Has been cancelled
Docs / Deploy docs (push) Has been cancelled
Verify Types / verify (push) Has been cancelled
Go / finish (push) Has been cancelled
chore: roll to Playwright v1.57.0 (#578)
* chore: roll to Playwright v1.57.0

Fix playwright submodule commit

Rerun gofumpt

Change ConsoleMessages to use newConsoleMessage() to properly deserialize event objects

Add test coverage

Update page_test.go

Fix failing end-to-end test

Update README.md

Fix README

Update README.md

* Fix unit tests
2026-01-16 23:06:27 +01:00

112 lines
3.3 KiB
Go

//nolint:staticcheck
package playwright_test
import (
"fmt"
"testing"
"time"
"github.com/playwright-community/playwright-go"
"github.com/stretchr/testify/require"
)
func TestSelectorsRegisterShouldWork(t *testing.T) {
tagSelector := `(() => ({
query(root, selector) {
return root.querySelector(selector);
},
queryAll(root, selector) {
return Array.from(root.querySelectorAll(selector));
}
}))()`
// Use unique names to avoid conflicts when running tests multiple times
uniqueSuffix := fmt.Sprintf("%s_%d", t.Name(), time.Now().UnixNano())
selectorName := "tag_" + browserName + "_" + uniqueSuffix
selector2Name := "tag2_" + browserName + "_" + uniqueSuffix
// Register one engine before creating context.
err := pw.Selectors.Register(selectorName, playwright.Script{
Content: &tagSelector,
})
require.NoError(t, err)
BeforeEach(t)
// Register another engine after creating context.
err = pw.Selectors.Register(selector2Name, playwright.Script{
Content: &tagSelector,
})
require.NoError(t, err)
require.NoError(t, page.SetContent(`<div><span></span></div><div></div>`))
ret, err := page.EvalOnSelector(selectorName+"=DIV", `e => e.nodeName`, nil)
require.NoError(t, err)
require.Equal(t, "DIV", ret)
ret, err = page.EvalOnSelector(selectorName+"=SPAN", `e => e.nodeName`, nil)
require.NoError(t, err)
require.Equal(t, "SPAN", ret)
ret, err = page.EvalOnSelectorAll(selectorName+"=DIV", `es => es.length`)
require.NoError(t, err)
require.Equal(t, 2, ret)
ret, err = page.EvalOnSelector(selector2Name+"=DIV", `e => e.nodeName`, nil)
require.NoError(t, err)
require.Equal(t, "DIV", ret)
ret, err = page.EvalOnSelector(selector2Name+"=SPAN", `e => e.nodeName`, nil)
require.NoError(t, err)
require.Equal(t, "SPAN", ret)
ret, err = page.EvalOnSelectorAll(selector2Name+"=DIV", `es => es.length`)
require.NoError(t, err)
require.Equal(t, 2, ret)
// Selector names are case-sensitive.
_, err = page.Locator("tAG=DIV").All()
require.ErrorContains(t, err, `Unknown engine "tAG" while parsing selector tAG=DIV`)
require.NoError(t, context.Close())
}
func TestSelectorsShouldUseDataTestIdInStrictErrors(t *testing.T) {
BeforeEach(t)
pw.Selectors.SetTestIdAttribute("data-custom-id")
require.NoError(t, page.SetContent(`
<div>
<div></div>
<div>
<div></div>
<div></div>
</div>
</div>
<div>
<div class='foo bar:0' data-custom-id='One'>
</div>
<div class='foo bar:1' data-custom-id='Two'>
</div>
</div>`))
err := page.Locator(".foo").Hover(playwright.LocatorHoverOptions{
Timeout: playwright.Float(500),
})
require.ErrorContains(t, err, "strict mode violation")
require.ErrorContains(t, err, `<div class="foo bar:0`)
require.ErrorContains(t, err, `<div class="foo bar:1`)
require.ErrorContains(t, err, `aka getByTestId('One')`)
}
func TestSelectorsShouldWorkWithPath(t *testing.T) {
BeforeEach(t)
// Use unique name to avoid conflicts when running tests multiple times
selectorName := fmt.Sprintf("foo_%s_%d", t.Name(), time.Now().UnixNano())
require.NoError(t, pw.Selectors.Register(selectorName, playwright.Script{
Path: playwright.String(Asset("sectionselectorengine.js")),
}))
require.NoError(t, page.SetContent(`<section></section>`))
ret, err := page.EvalOnSelector(selectorName+"=whatever", `e => e.nodeName`, nil)
require.NoError(t, err)
require.Equal(t, "SECTION", ret)
}