1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-19 21:28:32 +02:00

Added support to open blank page

This commit is contained in:
Tim Voronov 2018-10-04 21:37:28 -04:00
parent 71ee1f3dfe
commit c650d28b29
7 changed files with 55 additions and 27 deletions

9
Gopkg.lock generated
View File

@ -137,6 +137,14 @@
revision = "a96e63847dc3c67d17befa69c303767e2f84e54f"
version = "v2.1"
[[projects]]
branch = "master"
digest = "1:f7aa53146bf79462509d4ce136826ebbd64907e4679e1b04e62758da6b68e589"
name = "github.com/orcaman/concurrent-map"
packages = ["."]
pruneopts = "UT"
revision = "b28018939af9022337862b94a463abb18abb3e0e"
[[projects]]
digest = "1:40e195917a951a8bf867cd05de2a46aaf1806c50cf92eebf4c16f78cd196f747"
name = "github.com/pkg/errors"
@ -228,6 +236,7 @@
"github.com/mafredri/cdp/rpcc",
"github.com/mafredri/cdp/session",
"github.com/natefinch/lumberjack",
"github.com/orcaman/concurrent-map",
"github.com/pkg/errors",
"github.com/rs/zerolog",
"github.com/sethgrid/pester",

View File

@ -0,0 +1,3 @@
LET doc = DOCUMENT("about:blank", true)
NAVIGATE(doc, "https://www.google.com/")
RETURN doc.url

View File

@ -1,13 +1,12 @@
LET doc = DOCUMENT("https://github.com/", true)
LET main = ELEMENT(doc, '.application-main')
LOG('innerText:start')
LET mainTxt = main.innerText
LOG('innerText:end')
NAVIGATE(doc, "https://github.com/features")
LET features = ELEMENT(doc, '.application-main')
LET featuresTxt = features.innerText
LOG("featuresTxt:", featuresTxt)
RETURN mainTxt == featuresTxt

View File

@ -1775,23 +1775,28 @@ func TestParam(t *testing.T) {
}
func TestHtml(t *testing.T) {
// Convey("Should load a document", t, func() {
// c := compiler.New()
//
// out, err := c.MustCompile(`
//LET doc = DOCUMENT("https://github.com/", true)
//LET btn = ELEMENT(doc, ".HeaderMenu a")
//
//CLICK(btn)
//WAIT_NAVIGATION(doc)
//WAIT_ELEMENT(doc, '.IconNav')
//
//RETURN INNER_HTML_ALL(doc, '.IconNav a')
//
// `).Run(context.Background())
//
// So(err, ShouldBeNil)
//
// So(string(out), ShouldEqual, `"int"`)
// })
Convey("Should load a document", t, func() {
c := compiler.New()
out, err := c.MustCompile(`
LET doc = DOCUMENT("https://github.com/", true)
LET main = ELEMENT(doc, '.application-main')
LET mainTxt = main.innerText
NAVIGATE(doc, "https://github.com/features")
LET features = ELEMENT(doc, '.application-main')
LET featuresTxt = features.innerText
LOG("featuresTxt:", featuresTxt)
RETURN mainTxt == featuresTxt
`).Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `"int"`)
})
}

View File

@ -21,6 +21,8 @@ import (
"time"
)
const BlankPageUrl = "about:blank"
type HtmlDocument struct {
sync.Mutex
logger *zerolog.Logger
@ -78,10 +80,12 @@ func LoadHtmlDocument(
return nil, err
}
err = waitForLoadEvent(ctx, client)
if url != BlankPageUrl {
err = waitForLoadEvent(ctx, client)
if err != nil {
return nil, err
if err != nil {
return nil, err
}
}
root, innerHtml, err := getRootElement(client)
@ -637,6 +641,10 @@ func (doc *HtmlDocument) WaitForNavigation(timeout values.Int) error {
}
func (doc *HtmlDocument) Navigate(url values.String) error {
if url == "" {
url = BlankPageUrl
}
ctx := context.Background()
repl, err := doc.client.Page.Navigate(ctx, page.NewNavigateArgs(url.String()))
@ -648,5 +656,5 @@ func (doc *HtmlDocument) Navigate(url values.String) error {
return errors.New(*repl.ErrorText)
}
return waitForLoadEvent(ctx, doc.client)
return doc.WaitForNavigation(5000)
}

View File

@ -38,6 +38,10 @@ func (drv *Driver) GetDocument(ctx context.Context, url string) (values.HtmlNode
ctx, cancel := context.WithTimeout(ctx, DefaultTimeout)
defer cancel()
if url == "" {
url = BlankPageUrl
}
// Create a new target belonging to the browser context, similar
// to opening a new tab in an incognito window.
createTargetArgs := target.NewCreateTargetArgs(url).SetBrowserContextID(drv.contextID)

View File

@ -297,7 +297,7 @@ func (el *HtmlElement) GetChildNode(idx values.Int) core.Value {
func (el *HtmlElement) QuerySelector(selector values.String) core.Value {
if !el.IsConnected() {
return values.NewArray(0)
return values.None
}
ctx := context.Background()