Skip to content

Commit

Permalink
Bugfix/inner text html by selector (#347)
Browse files Browse the repository at this point in the history
* Fixed inner text

* Fixed inner html

* Updated set inner html and inner text

* Changed mechanism of reading and writing inner text and html

* updated makefile

* Added e2e tests

* Updated makefile

* Updated changelog

* Reverted dynamic page example
  • Loading branch information
ziflex authored Aug 5, 2019
1 parent 7a07278 commit 6ec50c5
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 80 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#### Fixed
- Scrolling position is not centered. [#343](https://github.com/MontFerret/ferret/pull/343)
- Unable to set custom logger fields. [#346](https://github.com/MontFerret/ferret/pull/346)
- Fixed ``INNER_HTML``, ``INNER_TEXT``, ``INNER_HTML_SET``, ``INNER_TEXT_SET`` functions. [#347](https://github.com/MontFerret/ferret/pull/347)
- Unable to set custom headers. [#348](https://github.com/MontFerret/ferret/pull/348)

### 0.8.1
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ cover:

e2e:
go run ${DIR_E2E}/main.go --tests ${DIR_E2E}/tests --pages ${DIR_E2E}/pages
# --filter=e2e/tests/**/xpath/*.fql
# --filter=e2e/tests/dynamic/element/inner_text/get_by_selector.fql

bench:
go test -run=XXX -bench=. ${DIR_PKG}/...
Expand Down
5 changes: 5 additions & 0 deletions e2e/pages/dynamic/components/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import EventsPage from './pages/events/index.js';
import IframePage from './pages/iframes/index.js';
import MediaPage from './pages/media/index.js';
import PaginationPage from './pages/pagination/index.js';
import ListsPage from './pages/lists/index.js';

const e = React.createElement;
const Router = ReactRouter.Router;
Expand Down Expand Up @@ -61,6 +62,10 @@ export default React.memo(function AppComponent(params = {}) {
path: '/pagination',
component: PaginationPage
}),
e(Route, {
path: '/lists',
component: ListsPage
}),
]),
redirectTo
])
Expand Down
20 changes: 20 additions & 0 deletions e2e/pages/dynamic/components/pages/lists/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const e = React.createElement;

const ITEMS = [{"artist":"Lil Tecca","track":"Ransom"},{"artist":"NLE Choppa","track":"Shotta Flow (Feat. Blueface) [Remix]"},{"artist":"Baby Jesus (DaBaby)","track":"Suge"},{"artist":"NLE Choppa","track":"Shotta Flow 3"},{"artist":"Lil Tecca ","track":"Lil Tecca - Did It Again"},{"artist":"NLE Choppa","track":"Shotta Flow"},{"artist":"Ynw Melly","track":"Dangerously In Love (772 Love Pt. 2)"},{"artist":"POLO G","track":"Polo G feat. Lil TJay - Pop Out"},{"artist":"MUSTARD","track":"Ballin' (feat. Roddy Ricch)"},{"artist":"Lil Nas X","track":"Panini"},{"artist":"Juice WRLD","track":"Juice Wrld - RUN"},{"artist":"Shordie Shordie","track":"Betchua (Bitchuary)"},{"artist":"Post Malone","track":"Goodbyes (feat. Young Thug)"},{"artist":"LIL UZI VERT","track":"Sanguine Paradise"},{"artist":"Calboy","track":"Envy Me"},{"artist":"Ambjaay","track":"Uno"},{"artist":"Lil Tecca","track":"Lil Tecca - Bossanova"},{"artist":"Lil Baby","track":"Baby"},{"artist":"Lil Tjay","track":"Lil Tjay - Brothers (Prod by JDONTHATRACK & Protegebeatz)"},{"artist":"YK Osiris","track":"Worth It"}];

export default class ListsPage extends React.Component {
render() {
const items = ITEMS.map((i) => {
return e("li", { className: "list-group-item track"}, [
e("div", { className: "track-details"}, [
e("h5", { className: "track-artist"}, i.artist),
e("small", { className: "track-name"}, i.track)
])
])
});

return e("div", { id: "tracks" }, [
e("ul", { className: "list-group track-list" }, items)
])
}
}
37 changes: 30 additions & 7 deletions e2e/tests/dynamic/element/inner_html/get_by_selector.fql
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
LET url = @dynamic
LET url = @dynamic + "/#/lists"
LET doc = DOCUMENT(url, true)
LET el = ELEMENT(doc, ".jumbotron")

LET expected = `Welcome to Ferret E2E test page!`
LET actual = INNER_HTML(el, "h1")
LET expected = [
{"details":'<h5 class="track-artist">Lil Tecca</h5><small class="track-name">Ransom</small>'},
{"details":'<h5 class="track-artist">NLE Choppa</h5><small class="track-name">Shotta Flow (Feat. Blueface) [Remix]</small>'},
{"details":'<h5 class="track-artist">Baby Jesus (DaBaby)</h5><small class="track-name">Suge</small>'},
{"details":'<h5 class="track-artist">NLE Choppa</h5><small class="track-name">Shotta Flow 3</small>'},
{"details":'<h5 class="track-artist">Lil Tecca </h5><small class="track-name">Lil Tecca - Did It Again</small>'},
{"details":'<h5 class="track-artist">NLE Choppa</h5><small class="track-name">Shotta Flow</small>'},
{"details":'<h5 class="track-artist">Ynw Melly</h5><small class="track-name">Dangerously In Love (772 Love Pt. 2)</small>'},
{"details":'<h5 class="track-artist">POLO G</h5><small class="track-name">Polo G feat. Lil TJay - Pop Out</small>'},
{"details":`<h5 class="track-artist">MUSTARD</h5><small class="track-name">Ballin' (feat. Roddy Ricch)</small>`},
{"details":'<h5 class="track-artist">Lil Nas X</h5><small class="track-name">Panini</small>'},
{"details":'<h5 class="track-artist">Juice WRLD</h5><small class="track-name">Juice Wrld - RUN</small>'},
{"details":'<h5 class="track-artist">Shordie Shordie</h5><small class="track-name">Betchua (Bitchuary)</small>'},
{"details":'<h5 class="track-artist">Post Malone</h5><small class="track-name">Goodbyes (feat. Young Thug)</small>'},
{"details":'<h5 class="track-artist">LIL UZI VERT</h5><small class="track-name">Sanguine Paradise</small>'},
{"details":'<h5 class="track-artist">Calboy</h5><small class="track-name">Envy Me</small>'},
{"details":'<h5 class="track-artist">Ambjaay</h5><small class="track-name">Uno</small>'},
{"details":'<h5 class="track-artist">Lil Tecca</h5><small class="track-name">Lil Tecca - Bossanova</small>'},
{"details":'<h5 class="track-artist">Lil Baby</h5><small class="track-name">Baby</small>'},
{"details":'<h5 class="track-artist">Lil Tjay</h5><small class="track-name">Lil Tjay - Brothers (Prod by JDONTHATRACK &amp; Protegebeatz)</small>'},
{"details":'<h5 class="track-artist">YK Osiris</h5><small class="track-name">Worth It</small>'}
]

LET r1 = '(\n|\s)'
LET r2 = '(\n|\s)'
LET actual = (
FOR item IN ELEMENTS(doc, '.track-list li')
RETURN {
details: INNER_HTML(item, '.track-details'),
}
)

RETURN EXPECT(REGEXP_REPLACE(expected, r1, ''), REGEXP_REPLACE(TRIM(actual), r2, ''))
RETURN EXPECT(expected, actual)
25 changes: 25 additions & 0 deletions e2e/tests/dynamic/element/inner_html/set_by_selector.fql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
LET url = @dynamic + "/#/lists"
LET doc = DOCUMENT(url, true)

LET expected = [
{"details":'<h5 class="track-artist">MEDUZA</h5><small class="track-name">Piece Of Your Heart (feat. Goodboys)</small>'},
{"details":'<h5 class="track-artist">Metanoia Music</h5><small class="track-name">Che Crozz x Orbis - Lift Me Up</small>'}
]

LET html = (
FOR t IN expected
RETURN '<li><div class="track-details">' + t.details + '</div></li>'
)

INNER_HTML_SET(doc, '.track-list', CONCAT_SEPARATOR('\n', html))

LET list = ELEMENT(doc, '.track-list')

LET actual = (
FOR item IN ELEMENTS(doc, '.track-list li')
RETURN {
details: INNER_HTML(item, '.track-details'),
}
)

RETURN EXPECT(expected, actual)
39 changes: 31 additions & 8 deletions e2e/tests/dynamic/element/inner_text/get_by_selector.fql
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
LET url = @dynamic
LET url = @dynamic + "/#/lists"
LET doc = DOCUMENT(url, true)
LET el = ELEMENT(doc, ".jumbotron")

LET expected = `Welcome to Ferret E2E test page!`
LET actual = INNER_TEXT(el, "h1")
LET expected = [
{"artist":"Lil Tecca","track":"Ransom"},
{"artist":"NLE Choppa","track":"Shotta Flow (Feat. Blueface) [Remix]"},
{"artist":"Baby Jesus (DaBaby)","track":"Suge"},
{"artist":"NLE Choppa","track":"Shotta Flow 3"},
{"artist":"Lil Tecca","track":"Lil Tecca - Did It Again"},
{"artist":"NLE Choppa","track":"Shotta Flow"},
{"artist":"Ynw Melly","track":"Dangerously In Love (772 Love Pt. 2)"},
{"artist":"POLO G","track":"Polo G feat. Lil TJay - Pop Out"},
{"artist":"MUSTARD","track":"Ballin' (feat. Roddy Ricch)"},
{"artist":"Lil Nas X","track":"Panini"},
{"artist":"Juice WRLD","track":"Juice Wrld - RUN"},
{"artist":"Shordie Shordie","track":"Betchua (Bitchuary)"},
{"artist":"Post Malone","track":"Goodbyes (feat. Young Thug)"},
{"artist":"LIL UZI VERT","track":"Sanguine Paradise"},
{"artist":"Calboy","track":"Envy Me"},
{"artist":"Ambjaay","track":"Uno"},
{"artist":"Lil Tecca","track":"Lil Tecca - Bossanova"},
{"artist":"Lil Baby","track":"Baby"},
{"artist":"Lil Tjay","track":"Lil Tjay - Brothers (Prod by JDONTHATRACK & Protegebeatz)"},
{"artist":"YK Osiris","track":"Worth It"}
]
LET actual = (
FOR item IN ELEMENTS(doc, '.track-list li')
RETURN {
artist: TRIM(INNER_TEXT(item, '.track-artist')),
track: TRIM(INNER_TEXT(item, '.track-name'))
}
)

LET r1 = '(\n|\s)'
LET r2 = '(\n|\s)'

RETURN EXPECT(REGEXP_REPLACE(expected, r1, ''), REGEXP_REPLACE(TRIM(actual), r2, ''))
RETURN EXPECT(expected, actual)
37 changes: 37 additions & 0 deletions e2e/tests/dynamic/element/inner_text/set_by_selector.fql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
LET url = @dynamic + "/#/lists"
LET doc = DOCUMENT(url, true)

LET expected = [
{
"artist":'MEDUZA',
"track": 'Piece Of Your Heart (feat. Goodboys)'
},
{
"artist": 'Metanoia Music',
"track": 'Che Crozz x Orbis - Lift Me Up'
}
]

LET f = (
FOR item, idx IN ELEMENTS(doc, '.track-list li')
LIMIT 2
LET value = expected[idx]

INNER_HTML_SET(item, '.track-artist', value.artist)
INNER_HTML_SET(item, '.track-name', value.track)

RETURN NONE
)

LET list = ELEMENT(doc, '.track-list')

LET actual = (
FOR item IN ELEMENTS(doc, '.track-list li')
LIMIT 2
RETURN {
artist: TRIM(INNER_TEXT(item, '.track-artist')),
track: TRIM(INNER_TEXT(item, '.track-name'))
}
)

RETURN EXPECT(expected, actual)
4 changes: 2 additions & 2 deletions examples/dynamic-page.fql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ LET tracks = ELEMENTS(doc, '.chartTrack__details')

FOR track IN tracks
RETURN {
artist: TRIM(INNER_TEXT(track, '.chartTrack__username')),
track: TRIM(INNER_TEXT(track, '.chartTrack__title'))
artist: TRIM(INNER_TEXT(track, '.chartTrack__username')),
track: TRIM(INNER_TEXT(track, '.chartTrack__title'))
}
120 changes: 114 additions & 6 deletions pkg/drivers/cdp/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,22 @@ func (el *HTMLElement) GetInnerTextBySelector(ctx context.Context, selector valu
return values.EmptyString, drivers.ErrDetached
}

out, err := el.exec.EvalWithReturnValue(ctx, templates.GetInnerTextBySelector(selector.String()))
sel, err := selector.MarshalJSON()

if err != nil {
return values.EmptyString, err
}

out, err := el.exec.EvalWithArgumentsAndReturnValue(
ctx,
templates.GetInnerTextBySelector(),
runtime.CallArgument{
ObjectID: &el.id.objectID,
},
runtime.CallArgument{
Value: sel,
},
)

if err != nil {
return values.EmptyString, err
Expand All @@ -741,15 +756,54 @@ func (el *HTMLElement) SetInnerTextBySelector(ctx context.Context, selector, inn
return drivers.ErrDetached
}

return el.exec.Eval(ctx, templates.SetInnerTextBySelector(selector.String(), innerText.String()))
sel, err := selector.MarshalJSON()

if err != nil {
return err
}

val, err := innerText.MarshalJSON()

if err != nil {
return err
}

return el.exec.EvalWithArguments(
ctx,
templates.SetInnerTextBySelector(),
runtime.CallArgument{
ObjectID: &el.id.objectID,
},
runtime.CallArgument{
Value: sel,
},
runtime.CallArgument{
Value: val,
},
)
}

func (el *HTMLElement) GetInnerTextBySelectorAll(ctx context.Context, selector values.String) (*values.Array, error) {
if el.IsDetached() {
return values.NewArray(0), drivers.ErrDetached
}

out, err := el.exec.EvalWithReturnValue(ctx, templates.GetInnerTextBySelectorAll(selector.String()))
sel, err := selector.MarshalJSON()

if err != nil {
return nil, err
}

out, err := el.exec.EvalWithArgumentsAndReturnValue(
ctx,
templates.GetInnerTextBySelectorAll(),
runtime.CallArgument{
ObjectID: &el.id.objectID,
},
runtime.CallArgument{
Value: sel,
},
)

if err != nil {
return values.NewArray(0), err
Expand Down Expand Up @@ -793,7 +847,22 @@ func (el *HTMLElement) GetInnerHTMLBySelector(ctx context.Context, selector valu
return values.EmptyString, drivers.ErrDetached
}

out, err := el.exec.EvalWithReturnValue(ctx, templates.GetInnerHTMLBySelector(selector.String()))
sel, err := selector.MarshalJSON()

if err != nil {
return values.EmptyString, err
}

out, err := el.exec.EvalWithArgumentsAndReturnValue(
ctx,
templates.GetInnerHTMLBySelector(),
runtime.CallArgument{
ObjectID: &el.id.objectID,
},
runtime.CallArgument{
Value: sel,
},
)

if err != nil {
return values.EmptyString, err
Expand All @@ -807,15 +876,54 @@ func (el *HTMLElement) SetInnerHTMLBySelector(ctx context.Context, selector, inn
return drivers.ErrDetached
}

return el.exec.Eval(ctx, templates.SetInnerHTMLBySelector(selector.String(), innerHTML.String()))
sel, err := selector.MarshalJSON()

if err != nil {
return err
}

val, err := innerHTML.MarshalJSON()

if err != nil {
return err
}

return el.exec.EvalWithArguments(
ctx,
templates.SetInnerHTMLBySelector(),
runtime.CallArgument{
ObjectID: &el.id.objectID,
},
runtime.CallArgument{
Value: sel,
},
runtime.CallArgument{
Value: val,
},
)
}

func (el *HTMLElement) GetInnerHTMLBySelectorAll(ctx context.Context, selector values.String) (*values.Array, error) {
if el.IsDetached() {
return values.NewArray(0), drivers.ErrDetached
}

out, err := el.exec.EvalWithReturnValue(ctx, templates.GetInnerHTMLBySelectorAll(selector.String()))
sel, err := selector.MarshalJSON()

if err != nil {
return values.NewArray(0), err
}

out, err := el.exec.EvalWithArgumentsAndReturnValue(
ctx,
templates.GetInnerHTMLBySelectorAll(),
runtime.CallArgument{
ObjectID: &el.id.objectID,
},
runtime.CallArgument{
Value: sel,
},
)

if err != nil {
return values.NewArray(0), err
Expand Down
Loading

0 comments on commit 6ec50c5

Please sign in to comment.