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

#104 Added NAVIGATE_FORWARD function (#108)

This commit is contained in:
Tim Voronov 2018-10-12 16:30:17 -04:00 committed by GitHub
parent 93d5df5fe7
commit 495cc34d77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 142 additions and 18 deletions

View File

@ -0,0 +1,9 @@
LET origin = "https://github.com/"
LET target = "https://github.com/features"
LET doc = DOCUMENT(origin, true)
NAVIGATE(doc, target)
NAVIGATE_BACK(doc)
NAVIGATE_FORWARD(doc)
RETURN doc.url == target

View File

@ -0,0 +1,9 @@
LET doc = DOCUMENT("https://github.com/", true)
NAVIGATE(doc, "https://github.com/features")
NAVIGATE(doc, "https://github.com/business")
NAVIGATE(doc, "https://github.com/marketplace")
NAVIGATE_BACK(doc, 3)
NAVIGATE_FORWARD(doc, 2)
RETURN doc.url == "https://github.com/business"

View File

@ -643,6 +643,49 @@ func (doc *HTMLDocument) NavigateBack(skip values.Int, timeout values.Int) (valu
return values.True, nil
}
func (doc *HTMLDocument) NavigateForward(skip values.Int, timeout values.Int) (values.Boolean, error) {
ctx := context.Background()
history, err := doc.client.Page.GetNavigationHistory(ctx)
if err != nil {
return values.False, err
}
length := len(history.Entries)
lastIndex := length - 1
// nowhere to go forward
if history.CurrentIndex == lastIndex {
return values.False, nil
}
if skip < 1 {
skip = 1
}
to := int(skip) + history.CurrentIndex
if to > lastIndex {
// TODO: Return error?
return values.False, nil
}
next := history.Entries[to]
err = doc.client.Page.NavigateToHistoryEntry(ctx, page.NewNavigateToHistoryEntryArgs(next.ID))
if err != nil {
return values.False, err
}
err = doc.WaitForNavigation(timeout)
if err != nil {
return values.False, err
}
return values.True, nil
}
func (doc *HTMLDocument) CaptureScreenshot(params *ScreenshotArgs) (core.Value, error) {
ctx := context.Background()
metrics, err := doc.client.Page.GetLayoutMetrics(ctx)

View File

@ -13,23 +13,24 @@ var (
func NewLib() map[string]core.Function {
return map[string]core.Function{
"DOCUMENT": Document,
"DOCUMENT_PARSE": DocumentParse,
"ELEMENT": Element,
"ELEMENTS": Elements,
"WAIT_ELEMENT": WaitElement,
"WAIT_NAVIGATION": WaitNavigation,
"WAIT_CLASS": WaitClass,
"WAIT_CLASS_ALL": WaitClassAll,
"CLICK": Click,
"CLICK_ALL": ClickAll,
"NAVIGATE": Navigate,
"NAVIGATE_BACK": NavigateBack,
"INPUT": Input,
"INNER_HTML": InnerHTML,
"INNER_HTML_ALL": InnerHTMLAll,
"INNER_TEXT": InnerText,
"INNER_TEXT_ALL": InnerTextAll,
"SCREENSHOT": Screenshot,
"DOCUMENT": Document,
"DOCUMENT_PARSE": DocumentParse,
"ELEMENT": Element,
"ELEMENTS": Elements,
"WAIT_ELEMENT": WaitElement,
"WAIT_NAVIGATION": WaitNavigation,
"WAIT_CLASS": WaitClass,
"WAIT_CLASS_ALL": WaitClassAll,
"CLICK": Click,
"CLICK_ALL": ClickAll,
"NAVIGATE": Navigate,
"NAVIGATE_BACK": NavigateBack,
"NAVIGATE_FORWARD": NavigateForward,
"INPUT": Input,
"INNER_HTML": InnerHTML,
"INNER_HTML_ALL": InnerHTMLAll,
"INNER_TEXT": InnerText,
"INNER_TEXT_ALL": InnerTextAll,
"SCREENSHOT": Screenshot,
}
}

View File

@ -0,0 +1,62 @@
package html
import (
"context"
"github.com/MontFerret/ferret/pkg/html/dynamic"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
/*
* Navigates a document forward within its navigation history.
* The operation blocks the execution until the page gets loaded.
* If the history is empty, the function returns FALSE.
* @param doc (Document) - Target document.
* @param entry (Int, optional) - Optional value indicating how many pages to skip. Default 1.
* @param timeout (Int, optional) - Optional timeout. Default is 5000.
* @returns (Boolean) - Returns TRUE if history exists and the operation succeeded, otherwise FALSE.
*/
func NavigateForward(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 3)
if err != nil {
return values.False, err
}
err = core.ValidateType(args[0], core.HTMLDocumentType)
if err != nil {
return values.None, err
}
doc, ok := args[0].(*dynamic.HTMLDocument)
if !ok {
return values.False, core.Errors(core.ErrInvalidType, ErrNotDynamic)
}
skip := values.NewInt(1)
timeout := values.NewInt(defaultTimeout)
if len(args) > 1 {
err = core.ValidateType(args[1], core.IntType)
if err != nil {
return values.None, err
}
skip = args[1].(values.Int)
}
if len(args) > 2 {
err = core.ValidateType(args[2], core.IntType)
if err != nil {
return values.None, err
}
timeout = args[2].(values.Int)
}
return doc.NavigateForward(skip, timeout)
}