mirror of
https://github.com/MontFerret/ferret.git
synced 2025-06-23 00:28:10 +02:00
Feature/#103 navigate back (#107)
* #103 Added NAVIGATE_BACK function * Commented out debugging test * Updated implementation
This commit is contained in:
7
examples/navigate_back.fql
Normal file
7
examples/navigate_back.fql
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
LET origin = "https://github.com/"
|
||||||
|
LET doc = DOCUMENT(origin, true)
|
||||||
|
|
||||||
|
NAVIGATE(doc, "https://github.com/features")
|
||||||
|
NAVIGATE_BACK(doc)
|
||||||
|
|
||||||
|
RETURN doc.url == origin
|
9
examples/navigate_back_2.fql
Normal file
9
examples/navigate_back_2.fql
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
LET origin = "https://github.com/"
|
||||||
|
LET doc = DOCUMENT(origin, true)
|
||||||
|
|
||||||
|
NAVIGATE(doc, "https://github.com/features")
|
||||||
|
NAVIGATE(doc, "https://github.com/business")
|
||||||
|
NAVIGATE(doc, "https://github.com/marketplace")
|
||||||
|
NAVIGATE_BACK(doc, 3)
|
||||||
|
|
||||||
|
RETURN doc.url == origin
|
@ -2104,22 +2104,19 @@ func TestParam(t *testing.T) {
|
|||||||
// c := compiler.New()
|
// c := compiler.New()
|
||||||
//
|
//
|
||||||
// out, err := c.MustCompile(`
|
// out, err := c.MustCompile(`
|
||||||
//LET google = DOCUMENT("https://www.google.com/", true)
|
//LET origin = "https://github.com/"
|
||||||
|
//LET doc = DOCUMENT(origin, true)
|
||||||
//
|
//
|
||||||
//INPUT(google, 'input[name="q"]', "ferret", 25)
|
//NAVIGATE(doc, "https://github.com/features")
|
||||||
//CLICK(google, 'input[name="btnK"]')
|
|
||||||
//
|
//
|
||||||
//WAIT_NAVIGATION(google)
|
//LOG("NAVIGATE", doc.url)
|
||||||
//WAIT_ELEMENT(google, '.g', 5000)
|
//
|
||||||
|
//NAVIGATE_BACK(doc)
|
||||||
|
//
|
||||||
|
//LOG("NAVIGATE_BACK", doc.url)
|
||||||
|
//
|
||||||
|
//RETURN doc.url == origin
|
||||||
//
|
//
|
||||||
//FOR result IN ELEMENTS(google, '.g')
|
|
||||||
// // filter out extra elements like videos and 'People also ask'
|
|
||||||
// FILTER TRIM(result.attributes.class) == 'g'
|
|
||||||
// RETURN {
|
|
||||||
// title: INNER_TEXT(result, 'h3'),
|
|
||||||
// description: INNER_TEXT(result, '.st'),
|
|
||||||
// url: INNER_TEXT(result, 'cite')
|
|
||||||
// }
|
|
||||||
// `).Run(context.Background())
|
// `).Run(context.Background())
|
||||||
//
|
//
|
||||||
// So(err, ShouldBeNil)
|
// So(err, ShouldBeNil)
|
||||||
|
@ -603,6 +603,46 @@ func (doc *HTMLDocument) Navigate(url values.String, timeout values.Int) error {
|
|||||||
return doc.WaitForNavigation(timeout)
|
return doc.WaitForNavigation(timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (doc *HTMLDocument) NavigateBack(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
|
||||||
|
}
|
||||||
|
|
||||||
|
// we are in the beginning
|
||||||
|
if history.CurrentIndex == 0 {
|
||||||
|
return values.False, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if skip < 1 {
|
||||||
|
skip = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
to := history.CurrentIndex - int(skip)
|
||||||
|
|
||||||
|
if to < 0 {
|
||||||
|
// TODO: Return error?
|
||||||
|
return values.False, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
prev := history.Entries[to]
|
||||||
|
err = doc.client.Page.NavigateToHistoryEntry(ctx, page.NewNavigateToHistoryEntryArgs(prev.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) {
|
func (doc *HTMLDocument) CaptureScreenshot(params *ScreenshotArgs) (core.Value, error) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
metrics, err := doc.client.Page.GetLayoutMetrics(ctx)
|
metrics, err := doc.client.Page.GetLayoutMetrics(ctx)
|
||||||
|
@ -24,6 +24,7 @@ func NewLib() map[string]core.Function {
|
|||||||
"CLICK": Click,
|
"CLICK": Click,
|
||||||
"CLICK_ALL": ClickAll,
|
"CLICK_ALL": ClickAll,
|
||||||
"NAVIGATE": Navigate,
|
"NAVIGATE": Navigate,
|
||||||
|
"NAVIGATE_BACK": NavigateBack,
|
||||||
"INPUT": Input,
|
"INPUT": Input,
|
||||||
"INNER_HTML": InnerHTML,
|
"INNER_HTML": InnerHTML,
|
||||||
"INNER_HTML_ALL": InnerHTMLAll,
|
"INNER_HTML_ALL": InnerHTMLAll,
|
||||||
|
62
pkg/stdlib/html/navigate_back.go
Normal file
62
pkg/stdlib/html/navigate_back.go
Normal 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 back 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 NavigateBack(_ 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.NavigateBack(skip, timeout)
|
||||||
|
}
|
Reference in New Issue
Block a user