mirror of
https://github.com/MontFerret/ferret.git
synced 2025-03-03 15:02:32 +02:00
Feature/#103 navigate back (#107)
* #103 Added NAVIGATE_BACK function * Commented out debugging test * Updated implementation
This commit is contained in:
parent
f91fbf6f8c
commit
93d5df5fe7
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()
|
||||
//
|
||||
// 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)
|
||||
//CLICK(google, 'input[name="btnK"]')
|
||||
//NAVIGATE(doc, "https://github.com/features")
|
||||
//
|
||||
//WAIT_NAVIGATION(google)
|
||||
//WAIT_ELEMENT(google, '.g', 5000)
|
||||
//LOG("NAVIGATE", doc.url)
|
||||
//
|
||||
//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())
|
||||
//
|
||||
// So(err, ShouldBeNil)
|
||||
|
@ -603,6 +603,46 @@ func (doc *HTMLDocument) Navigate(url values.String, timeout values.Int) error {
|
||||
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) {
|
||||
ctx := context.Background()
|
||||
metrics, err := doc.client.Page.GetLayoutMetrics(ctx)
|
||||
|
@ -24,6 +24,7 @@ func NewLib() map[string]core.Function {
|
||||
"CLICK": Click,
|
||||
"CLICK_ALL": ClickAll,
|
||||
"NAVIGATE": Navigate,
|
||||
"NAVIGATE_BACK": NavigateBack,
|
||||
"INPUT": Input,
|
||||
"INNER_HTML": InnerHTML,
|
||||
"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)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user