Skip to content

Commit

Permalink
Feature/#265 dom manipulations (#329)
Browse files Browse the repository at this point in the history
* Added SetInnerHTML method

* Added E2E tests

* Refactored GetInnerText* methods

* Updated e2e tests

* Moved related E2E tests to folders

* Added error message

* Added E2E tests

* Added E2E for static driver
  • Loading branch information
ziflex authored Jul 11, 2019
1 parent 8afa3b6 commit 347bae2
Show file tree
Hide file tree
Showing 53 changed files with 739 additions and 269 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ cover:

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

bench:
go test -run=XXX -bench=. ${DIR_PKG}/...
Expand Down
8 changes: 4 additions & 4 deletions e2e/pages/static/assets/analytics.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion e2e/pages/static/assets/jquery-3.3.1.slim.min.js

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions e2e/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"

"github.com/MontFerret/ferret/pkg/compiler"
Expand Down Expand Up @@ -185,6 +186,8 @@ func (r *Runner) runQuery(ctx context.Context, c *compiler.FqlCompiler, name, sc
}
}

mustFail := r.mustFail(name)

out, err := p.Run(
ctx,
runtime.WithLog(zerolog.ConsoleWriter{Out: os.Stdout}),
Expand All @@ -195,13 +198,28 @@ func (r *Runner) runQuery(ctx context.Context, c *compiler.FqlCompiler, name, sc
duration := time.Since(start)

if err != nil {
if mustFail {
return Result{
name: name,
duration: duration,
}
}

return Result{
name: name,
duration: duration,
err: errors.Wrap(err, "failed to execute query"),
}
}

if mustFail {
return Result{
name: name,
duration: duration,
err: errors.New("expected to fail"),
}
}

var result string

if err := json.Unmarshal(out, &result); err != nil {
Expand Down Expand Up @@ -281,3 +299,7 @@ func (r *Runner) traverseDir(ctx context.Context, dir string, iteratee func(name

return nil
}

func (r *Runner) mustFail(name string) bool {
return strings.HasSuffix(name, ".fail.fql")
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions e2e/tests/dynamic/doc/inner_html/set.fql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
LET url = @dynamic
LET doc = DOCUMENT(url, true)

LET expected = `<span>Hello</span>`

INNER_HTML_SET(doc, "body", "<span>Hello</span>")

LET actual = INNER_HTML(doc, "body")

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

RETURN EXPECT(REGEXP_REPLACE(expected, r1, ''), REGEXP_REPLACE(TRIM(actual), r2, ''))
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions e2e/tests/dynamic/doc/inner_text/set.fql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
LET url = @dynamic
LET doc = DOCUMENT(url, true)

LET expected = `Hello`

INNER_TEXT_SET(doc, "body", expected)

LET actual = INNER_TEXT(doc, "body")

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

RETURN EXPECT(REGEXP_REPLACE(expected, r1, ''), REGEXP_REPLACE(TRIM(actual), r2, ''))
File renamed without changes.
11 changes: 11 additions & 0 deletions e2e/tests/dynamic/element/inner_html/get_by_selector.fql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
LET url = @dynamic
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 r1 = '(\n|\s)'
LET r2 = '(\n|\s)'

RETURN EXPECT(REGEXP_REPLACE(expected, r1, ''), REGEXP_REPLACE(TRIM(actual), r2, ''))
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
LET url = @dynamic
LET doc = DOCUMENT(url, true)
LET el = ELEMENT(doc, ".jumbotron")

RETURN INNER_HTML(el, "h5")
14 changes: 14 additions & 0 deletions e2e/tests/dynamic/element/inner_html/set.fql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
LET url = @dynamic
LET doc = DOCUMENT(url, true)
LET el = ELEMENT(doc, "#index")

LET expected = "<span>This node was injected by Ferret</span>"
INNER_HTML_SET(el, "<span>This node was injected by Ferret</span>")
LET actual = INNER_HTML(el)

WAIT(100)

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

RETURN EXPECT(REGEXP_REPLACE(expected, r1, ''), REGEXP_REPLACE(TRIM(actual), r2, ''))
File renamed without changes.
11 changes: 11 additions & 0 deletions e2e/tests/dynamic/element/inner_text/get_by_selector.fql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
LET url = @dynamic
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 r1 = '(\n|\s)'
LET r2 = '(\n|\s)'

RETURN EXPECT(REGEXP_REPLACE(expected, r1, ''), REGEXP_REPLACE(TRIM(actual), r2, ''))
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
LET url = @dynamic
LET doc = DOCUMENT(url, true)
LET el = ELEMENT(doc, ".jumbotron")

RETURN INNER_TEXT(el, "h5")
13 changes: 13 additions & 0 deletions e2e/tests/dynamic/element/inner_text/set.fql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
LET url = @dynamic
LET doc = DOCUMENT(url, true)
LET el = ELEMENT(doc, ".jumbotron")

LET expected = `Foobar`
INNER_TEXT_SET(el, expected)
WAIT(100)
LET actual = INNER_TEXT(el)

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

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

RETURN INNER_TEXT_SET(el, "h4", "foobar")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions e2e/tests/static/element/inner_html/set.fql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
LET url = @static + '/simple.html'
LET doc = DOCUMENT(url)
LET el = ELEMENT(doc, "body")

LET expected = `<div><h1>Injected by Ferret</h1></div>`

INNER_HTML_SET(el, expected)

LET actual = INNER_HTML(el)

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

RETURN EXPECT(REGEXP_REPLACE(expected, r1, ''), REGEXP_REPLACE(TRIM(actual), r2, ''))
File renamed without changes.
14 changes: 14 additions & 0 deletions e2e/tests/static/element/inner_text/set.fql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
LET url = @static + '/simple.html'
LET doc = DOCUMENT(url)
LET el = ELEMENT(doc, "body")

LET expected = `Injected by Ferret`

INNER_TEXT_SET(el, expected)

LET actual = INNER_TEXT(el)

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

RETURN EXPECT(REGEXP_REPLACE(expected, r1, ''), REGEXP_REPLACE(TRIM(actual), r2, ''))
2 changes: 0 additions & 2 deletions pkg/drivers/cdp/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"github.com/MontFerret/ferret/pkg/runtime/values"
)

const BlankPageURL = "about:blank"

type HTMLDocument struct {
logger *zerolog.Logger
client *cdp.Client
Expand Down
3 changes: 3 additions & 0 deletions pkg/drivers/cdp/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cdp
import (
"context"
"sync"
"time"

"github.com/mafredri/cdp"
"github.com/mafredri/cdp/devtool"
Expand All @@ -16,6 +17,8 @@ import (
)

const DriverName = "cdp"
const BlankPageURL = "about:blank"
const DefaultTimeout = 5000 * time.Millisecond

type Driver struct {
mu sync.Mutex
Expand Down
Loading

0 comments on commit 347bae2

Please sign in to comment.