Skip to content

Commit

Permalink
chore: code review, remove FromElement and Locator functions from Sel…
Browse files Browse the repository at this point in the history
…ection class
  • Loading branch information
Azhovan committed Jan 11, 2024
1 parent 50d21c8 commit 139495f
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 54 deletions.
2 changes: 1 addition & 1 deletion js/modules/k6/html/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 0 additions & 32 deletions js/modules/k6/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down
21 changes: 0 additions & 21 deletions js/modules/k6/html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 139495f

Please sign in to comment.