1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-11-29 22:17:29 +02:00

Added Context to HTML methods (#235)

* Added Context to HTML methods

* Fixed unit tests

* Updated timeout

* Fixed WAIT_CLASS timeout
This commit is contained in:
Tim Voronov
2019-02-20 21:24:05 -05:00
committed by GitHub
parent 34c8c02258
commit 6e15846d0f
45 changed files with 415 additions and 446 deletions

View File

@@ -10,7 +10,7 @@ import (
// Click dispatches click event on a given element
// @param source (Document | Element) - Event source.
// @param selector (String, optional) - Optional selector. Only used when a document instance is passed.
func Click(_ context.Context, args ...core.Value) (core.Value, error) {
func Click(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 2)
if err != nil {
@@ -25,7 +25,7 @@ func Click(_ context.Context, args ...core.Value) (core.Value, error) {
return values.False, err
}
return el.Click()
return el.Click(ctx)
}
// CLICK(doc, selector)
@@ -37,5 +37,5 @@ func Click(_ context.Context, args ...core.Value) (core.Value, error) {
selector := args[1].String()
return doc.ClickBySelector(values.NewString(selector))
return doc.ClickBySelector(ctx, values.NewString(selector))
}

View File

@@ -12,7 +12,7 @@ import (
// @param source (Document) - Document.
// @param selector (String) - Selector.
// @returns (Boolean) - Returns true if matched at least one element.
func ClickAll(_ context.Context, args ...core.Value) (core.Value, error) {
func ClickAll(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, 2)
if err != nil {
@@ -34,5 +34,5 @@ func ClickAll(_ context.Context, args ...core.Value) (core.Value, error) {
return values.None, err
}
return doc.ClickBySelectorAll(values.NewString(selector))
return doc.ClickBySelectorAll(ctx, values.NewString(selector))
}

View File

@@ -14,14 +14,14 @@ import (
// @param docOrEl (HTMLDocument|HTMLElement) - Parent document or element.
// @param selector (String) - CSS selector.
// @returns (HTMLElement | None) - Returns an HTMLElement if found, otherwise NONE.
func Element(_ context.Context, args ...core.Value) (core.Value, error) {
func Element(ctx context.Context, args ...core.Value) (core.Value, error) {
el, selector, err := queryArgs(args)
if err != nil {
return values.None, err
}
return el.QuerySelector(selector), nil
return el.QuerySelector(ctx, selector), nil
}
func queryArgs(args []core.Value) (drivers.HTMLNode, values.String, error) {

View File

@@ -11,12 +11,12 @@ import (
// @param docOrEl (HTMLDocument|HTMLNode) - Parent document or element.
// @param selector (String) - CSS selector.
// @returns (Boolean) - A boolean value indicating whether there is an element matched by selector.
func ElementExists(_ context.Context, args ...core.Value) (core.Value, error) {
func ElementExists(ctx context.Context, args ...core.Value) (core.Value, error) {
el, selector, err := queryArgs(args)
if err != nil {
return values.None, err
}
return el.ExistsBySelector(selector), nil
return el.ExistsBySelector(ctx, selector), nil
}

View File

@@ -12,12 +12,12 @@ import (
// @param docOrEl (HTMLDocument|HTMLNode) - Parent document or element.
// @param selector (String) - CSS selector.
// @returns (Array) - Returns an array of found HTML element.
func Elements(_ context.Context, args ...core.Value) (core.Value, error) {
func Elements(ctx context.Context, args ...core.Value) (core.Value, error) {
el, selector, err := queryArgs(args)
if err != nil {
return values.None, err
}
return el.QuerySelectorAll(selector), nil
return el.QuerySelectorAll(ctx, selector), nil
}

View File

@@ -12,12 +12,12 @@ import (
// @param docOrEl (HTMLDocument|HTMLNode) - Parent document or element.
// @param selector (String) - CSS selector.
// @returns (Int) - A number of found HTML elements by a given CSS selector.
func ElementsCount(_ context.Context, args ...core.Value) (core.Value, error) {
func ElementsCount(ctx context.Context, args ...core.Value) (core.Value, error) {
el, selector, err := queryArgs(args)
if err != nil {
return values.None, err
}
return el.CountBySelector(selector), nil
return el.CountBySelector(ctx, selector), nil
}

View File

@@ -13,7 +13,7 @@ import (
// If there's no element matching selector, the method returns an error.
// @param docOrEl (HTMLDocument|HTMLElement) - Target document or element.
// @param selector (String, options) - If document is passed, this param must represent an element selector.
func Hover(_ context.Context, args ...core.Value) (core.Value, error) {
func Hover(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 2)
if err != nil {
@@ -38,11 +38,11 @@ func Hover(_ context.Context, args ...core.Value) (core.Value, error) {
doc := args[0].(drivers.HTMLDocument)
selector := args[1].(values.String)
return values.None, doc.HoverBySelector(selector)
return values.None, doc.HoverBySelector(ctx, selector)
}
// Element
el := args[0].(drivers.HTMLElement)
return values.None, el.Hover()
return values.None, el.Hover(ctx)
}

View File

@@ -13,7 +13,7 @@ import (
// @param doc (Document|Element) - Parent document or element.
// @param selector (String, optional) - String of CSS selector.
// @returns (String) - Inner HTML string if an element found, otherwise empty string.
func InnerHTML(_ context.Context, args ...core.Value) (core.Value, error) {
func InnerHTML(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 2)
if err != nil {
@@ -33,7 +33,7 @@ func InnerHTML(_ context.Context, args ...core.Value) (core.Value, error) {
}
if len(args) == 1 {
return el.InnerHTML(), nil
return el.InnerHTML(ctx), nil
}
err = core.ValidateType(args[1], types.String)
@@ -44,5 +44,5 @@ func InnerHTML(_ context.Context, args ...core.Value) (core.Value, error) {
selector := args[1].(values.String)
return el.InnerHTMLBySelector(selector), nil
return el.InnerHTMLBySelector(ctx, selector), nil
}

View File

@@ -13,7 +13,7 @@ import (
// @param doc (HTMLDocument|HTMLElement) - Parent document or element.
// @param selector (String) - String of CSS selector.
// @returns (String) - An array of inner HTML strings if any element found, otherwise empty array.
func InnerHTMLAll(_ context.Context, args ...core.Value) (core.Value, error) {
func InnerHTMLAll(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, 2)
if err != nil {
@@ -40,5 +40,5 @@ func InnerHTMLAll(_ context.Context, args ...core.Value) (core.Value, error) {
selector := args[1].(values.String)
return el.InnerHTMLBySelectorAll(selector), nil
return el.InnerHTMLBySelectorAll(ctx, selector), nil
}

View File

@@ -12,7 +12,7 @@ import (
// @param doc (HTMLDocument|HTMLElement) - Parent document or element.
// @param selector (String, optional) - String of CSS selector.
// @returns (String) - Inner text if an element found, otherwise empty string.
func InnerText(_ context.Context, args ...core.Value) (core.Value, error) {
func InnerText(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 2)
if err != nil {
@@ -26,7 +26,7 @@ func InnerText(_ context.Context, args ...core.Value) (core.Value, error) {
}
if len(args) == 1 {
return el.InnerText(), nil
return el.InnerText(ctx), nil
}
err = core.ValidateType(args[1], types.String)
@@ -37,5 +37,5 @@ func InnerText(_ context.Context, args ...core.Value) (core.Value, error) {
selector := args[1].(values.String)
return el.InnerTextBySelector(selector), nil
return el.InnerTextBySelector(ctx, selector), nil
}

View File

@@ -13,7 +13,7 @@ import (
// @param doc (HTMLDocument|HTMLElement) - Parent document or element.
// @param selector (String) - String of CSS selector.
// @returns (String) - An array of inner text if any element found, otherwise empty array.
func InnerTextAll(_ context.Context, args ...core.Value) (core.Value, error) {
func InnerTextAll(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, 2)
if err != nil {
@@ -40,5 +40,5 @@ func InnerTextAll(_ context.Context, args ...core.Value) (core.Value, error) {
selector := args[1].(values.String)
return el.InnerTextBySelectorAll(selector), nil
return el.InnerTextBySelectorAll(ctx, selector), nil
}

View File

@@ -15,7 +15,7 @@ import (
// @param value (String) - Target value.
// @param delay (Int, optional) - Waits delay milliseconds between keystrokes
// @returns (Boolean) - Returns true if an element was found.
func Input(_ context.Context, args ...core.Value) (core.Value, error) {
func Input(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, 4)
if err != nil {
@@ -54,7 +54,7 @@ func Input(_ context.Context, args ...core.Value) (core.Value, error) {
delay = arg4.(values.Int)
}
return doc.InputBySelector(arg2.(values.String), args[2], delay)
return doc.InputBySelector(ctx, arg2.(values.String), args[2], delay)
}
el := arg1.(drivers.HTMLElement)
@@ -72,7 +72,7 @@ func Input(_ context.Context, args ...core.Value) (core.Value, error) {
delay = arg3.(values.Int)
}
err = el.Input(args[1], delay)
err = el.Input(ctx, args[1], delay)
if err != nil {
return values.False, err

View File

@@ -2,11 +2,11 @@ package html
import (
"context"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
"time"
)
const defaultTimeout = 5000
@@ -67,6 +67,13 @@ func ValidateDocument(ctx context.Context, value core.Value) (core.Value, error)
return doc, nil
}
func waitTimeout(ctx context.Context, value values.Int) (context.Context, context.CancelFunc) {
return context.WithTimeout(
ctx,
time.Duration(value)*time.Millisecond,
)
}
func resolveElement(value core.Value) (drivers.HTMLElement, error) {
vt := value.Type()

View File

@@ -2,7 +2,6 @@ package html
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
@@ -14,7 +13,7 @@ import (
// @param doc (Document) - Target document.
// @param url (String) - Target url to navigate.
// @param timeout (Int, optional) - Optional timeout. Default is 5000.
func Navigate(_ context.Context, args ...core.Value) (core.Value, error) {
func Navigate(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, 3)
if err != nil {
@@ -39,5 +38,8 @@ func Navigate(_ context.Context, args ...core.Value) (core.Value, error) {
timeout = args[2].(values.Int)
}
return values.None, doc.Navigate(args[1].(values.String), timeout)
ctx, fn := waitTimeout(ctx, timeout)
defer fn()
return values.None, doc.Navigate(ctx, args[1].(values.String))
}

View File

@@ -2,7 +2,6 @@ package html
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
@@ -15,7 +14,7 @@ import (
// @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) {
func NavigateBack(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 3)
if err != nil {
@@ -51,5 +50,8 @@ func NavigateBack(_ context.Context, args ...core.Value) (core.Value, error) {
timeout = args[2].(values.Int)
}
return doc.NavigateBack(skip, timeout)
ctx, fn := waitTimeout(ctx, timeout)
defer fn()
return doc.NavigateBack(ctx, skip)
}

View File

@@ -2,7 +2,6 @@ package html
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
@@ -15,7 +14,7 @@ import (
// @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) {
func NavigateForward(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 3)
if err != nil {
@@ -51,5 +50,8 @@ func NavigateForward(_ context.Context, args ...core.Value) (core.Value, error)
timeout = args[2].(values.Int)
}
return doc.NavigateForward(skip, timeout)
ctx, fn := waitTimeout(ctx, timeout)
defer fn()
return doc.NavigateForward(ctx, skip)
}

View File

@@ -85,14 +85,14 @@ func (p *Paging) Iterate(_ context.Context) (core.Iterator, error) {
return &PagingIterator{p.document, p.selector, -1}, nil
}
func (i *PagingIterator) Next(_ context.Context) (core.Value, core.Value, error) {
func (i *PagingIterator) Next(ctx context.Context) (core.Value, core.Value, error) {
i.pos++
if i.pos == 0 {
return values.ZeroInt, values.ZeroInt, nil
}
clicked, err := i.document.ClickBySelector(i.selector)
clicked, err := i.document.ClickBySelector(ctx, i.selector)
if err != nil {
return values.None, values.None, err

View File

@@ -292,7 +292,7 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) {
}
}
pdf, err := doc.PrintToPDF(pdfParams)
pdf, err := doc.PrintToPDF(ctx, pdfParams)
if err != nil {
return values.None, err

View File

@@ -155,7 +155,7 @@ func Screenshot(ctx context.Context, args ...core.Value) (core.Value, error) {
}
}
scr, err := doc.CaptureScreenshot(screenshotParams)
scr, err := doc.CaptureScreenshot(ctx, screenshotParams)
if err != nil {
return values.None, err

View File

@@ -10,7 +10,7 @@ import (
// ScrollTop scrolls the document's window to its bottom.
// @param doc (HTMLDocument) - Target document.
func ScrollBottom(_ context.Context, args ...core.Value) (core.Value, error) {
func ScrollBottom(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
@@ -29,5 +29,5 @@ func ScrollBottom(_ context.Context, args ...core.Value) (core.Value, error) {
return values.None, err
}
return values.None, doc.ScrollBottom()
return values.None, doc.ScrollBottom(ctx)
}

View File

@@ -12,7 +12,7 @@ import (
// ScrollInto scrolls an element on.
// @param docOrEl (HTMLDocument|HTMLElement) - Target document or element.
// @param selector (String, options) - If document is passed, this param must represent an element selector.
func ScrollInto(_ context.Context, args ...core.Value) (core.Value, error) {
func ScrollInto(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 2)
if err != nil {
@@ -36,7 +36,7 @@ func ScrollInto(_ context.Context, args ...core.Value) (core.Value, error) {
doc := args[0].(drivers.HTMLDocument)
selector := args[1].(values.String)
return values.None, doc.ScrollBySelector(selector)
return values.None, doc.ScrollBySelector(ctx, selector)
}
err = core.ValidateType(args[0], drivers.HTMLElementType)
@@ -48,5 +48,5 @@ func ScrollInto(_ context.Context, args ...core.Value) (core.Value, error) {
// Element
el := args[0].(drivers.HTMLElement)
return values.None, el.ScrollIntoView()
return values.None, el.ScrollIntoView(ctx)
}

View File

@@ -10,7 +10,7 @@ import (
// ScrollTop scrolls the document's window to its top.
// @param doc (HTMLDocument) - Target document.
func ScrollTop(_ context.Context, args ...core.Value) (core.Value, error) {
func ScrollTop(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
@@ -29,5 +29,5 @@ func ScrollTop(_ context.Context, args ...core.Value) (core.Value, error) {
return values.None, err
}
return values.None, doc.ScrollTop()
return values.None, doc.ScrollTop(ctx)
}

View File

@@ -14,7 +14,7 @@ import (
// @param valueOrSelector (String | Array<String>) - Selector or a an array of strings as a value.
// @param value (Array<String) - Target value. Optional.
// @returns (Array<String>) - Returns an array of selected values.
func Select(_ context.Context, args ...core.Value) (core.Value, error) {
func Select(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, 4)
if err != nil {
@@ -46,7 +46,7 @@ func Select(_ context.Context, args ...core.Value) (core.Value, error) {
return values.False, err
}
return doc.SelectBySelector(arg2.(values.String), arg3.(*values.Array))
return doc.SelectBySelector(ctx, arg2.(values.String), arg3.(*values.Array))
}
el := arg1.(drivers.HTMLElement)
@@ -58,5 +58,5 @@ func Select(_ context.Context, args ...core.Value) (core.Value, error) {
return values.False, err
}
return el.Select(arg2.(*values.Array))
return el.Select(ctx, arg2.(*values.Array))
}

View File

@@ -2,7 +2,6 @@ package html
import (
"context"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
@@ -18,7 +17,7 @@ import (
// Otherwise timeout.
// @param timeout (Int, optional) - If document is passed, this param must represent timeout.
// Otherwise not passed.
func WaitClass(_ context.Context, args ...core.Value) (core.Value, error) {
func WaitClass(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, 4)
if err != nil {
@@ -72,7 +71,10 @@ func WaitClass(_ context.Context, args ...core.Value) (core.Value, error) {
timeout = args[3].(values.Int)
}
return values.None, doc.WaitForClassBySelector(selector, class, timeout)
ctx, fn := waitTimeout(ctx, timeout)
defer fn()
return values.None, doc.WaitForClassBySelector(ctx, selector, class)
}
el := arg1.(drivers.HTMLElement)
@@ -88,5 +90,8 @@ func WaitClass(_ context.Context, args ...core.Value) (core.Value, error) {
timeout = args[2].(values.Int)
}
return values.None, el.WaitForClass(class, timeout)
ctx, fn := waitTimeout(ctx, timeout)
defer fn()
return values.None, el.WaitForClass(ctx, class)
}

View File

@@ -2,7 +2,6 @@ package html
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
@@ -14,7 +13,7 @@ import (
// @param selector (String) - String of CSS selector.
// @param class (String) - String of target CSS class.
// @param timeout (Int, optional) - Optional timeout.
func WaitClassAll(_ context.Context, args ...core.Value) (core.Value, error) {
func WaitClassAll(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 3, 4)
if err != nil {
@@ -55,5 +54,8 @@ func WaitClassAll(_ context.Context, args ...core.Value) (core.Value, error) {
timeout = args[3].(values.Int)
}
return values.None, doc.WaitForClassBySelectorAll(selector, class, timeout)
ctx, fn := waitTimeout(ctx, timeout)
defer fn()
return values.None, doc.WaitForClassBySelectorAll(ctx, selector, class)
}

View File

@@ -2,7 +2,6 @@ package html
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
@@ -13,7 +12,7 @@ import (
// @param doc (HTMLDocument) - Driver HTMLDocument.
// @param selector (String) - Target element's selector.
// @param timeout (Int, optional) - Optional timeout. Default 5000 ms.
func WaitElement(_ context.Context, args ...core.Value) (core.Value, error) {
func WaitElement(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, 3)
if err != nil {
@@ -39,5 +38,8 @@ func WaitElement(_ context.Context, args ...core.Value) (core.Value, error) {
timeout = args[2].(values.Int)
}
return values.None, doc.WaitForSelector(values.NewString(selector), timeout)
ctx, fn := waitTimeout(ctx, timeout)
defer fn()
return values.None, doc.WaitForSelector(ctx, values.NewString(selector))
}

View File

@@ -2,7 +2,6 @@ package html
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
@@ -12,7 +11,7 @@ import (
// Stops the execution until the navigation ends or operation times out.
// @param doc (HTMLDocument) - Driver HTMLDocument.
// @param timeout (Int, optional) - Optional timeout. Default 5000 ms.
func WaitNavigation(_ context.Context, args ...core.Value) (core.Value, error) {
func WaitNavigation(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 2)
if err != nil {
@@ -37,5 +36,8 @@ func WaitNavigation(_ context.Context, args ...core.Value) (core.Value, error) {
timeout = args[1].(values.Int)
}
return values.None, doc.WaitForNavigation(timeout)
ctx, fn := waitTimeout(ctx, timeout)
defer fn()
return values.None, doc.WaitForNavigation(ctx)
}