diff --git a/js/modules/k6/html/element.go b/js/modules/k6/html/element.go index b8b655ff3a4..022531b9a39 100644 --- a/js/modules/k6/html/element.go +++ b/js/modules/k6/html/element.go @@ -255,7 +255,7 @@ func (e Element) IsSameNode(v goja.Value) bool { // Example: // sel := element.Selection() func (e Element) Selection() Selection { - return Selection{rt: e.sel.rt, sel: e.sel.sel, URL: e.sel.URL} + return *e.sel } func (e Element) GetElementsByClassName(name string) []goja.Value { diff --git a/js/modules/k6/html/html.go b/js/modules/k6/html/html.go index ac2204aaa74..180567a9c3a 100644 --- a/js/modules/k6/html/html.go +++ b/js/modules/k6/html/html.go @@ -280,38 +280,6 @@ func (s Selection) Last() Selection { return Selection{s.rt, s.sel.Last(), s.URL} } -// FromElement creates a new Selection based on a specified Element. -// -// This function is useful for creating a new Selection object that -// encapsulates the same HTML structure as the provided Element. It -// is particularly helpful when you need to perform further operations -// or manipulations on the HTML content within the scope of this Element. -// If the provided value is not an Element, an empty Selection is returned. -// -// Example: -// sel := existingSelection.FromElement(jsElement) -func (s Selection) FromElement(v goja.Value) Selection { - elm, ok := v.Export().(Element) - if !ok { - // Return an empty Selection if the value is not an Element - return s.emptySelection() - } - - return Selection{rt: s.rt, sel: elm.sel.sel, URL: s.URL} -} - -// Locator selects HTML elements based on a CSS selector or XPath. -// -// This method is designed to provide a consistent way of selecting elements, -// similar to how 'page.locator' works in the 'k6-browser'. It uses goquery -// to perform the selection based on the given selector string. -// -// Example: -// sel := mySelection.Locator(".myClass") -func (s Selection) Locator(selector string) Selection { - return Selection{s.rt, s.sel.Find(selector), s.URL} -} - func (s Selection) Contents() Selection { return Selection{s.rt, s.sel.Contents(), s.URL} } diff --git a/js/modules/k6/html/html_test.go b/js/modules/k6/html/html_test.go index 19b4efd87d1..19f3e912de9 100644 --- a/js/modules/k6/html/html_test.go +++ b/js/modules/k6/html/html_test.go @@ -269,27 +269,6 @@ func TestParseHTML(t *testing.T) { assert.Equal(t, "form1", v.Export()) } }) - t.Run("FromElement", func(t *testing.T) { - t.Parallel() - rt := getTestRuntimeWithDoc(t, testHTML) - - _, err := rt.RunString(`var body = doc.find("body").get(0)`) - assert.NoError(t, err) - - v, err := rt.RunString(`doc.fromElement(body).find('#top').text()`) - if assert.NoError(t, err) { - assert.Equal(t, "Lorem ipsum", v.String()) - } - }) - t.Run("Locator", func(t *testing.T) { - t.Parallel() - rt := getTestRuntimeWithDoc(t, testHTML) - - v, err := rt.RunString(`doc.locator("#top").text()`) - if assert.NoError(t, err) { - assert.Equal(t, "Lorem ipsum", v.String()) - } - }) t.Run("Contents", func(t *testing.T) { t.Parallel() rt := getTestRuntimeWithDoc(t, testHTML)