Skip to content

Commit

Permalink
Added Context to HTML methods (MontFerret#235)
Browse files Browse the repository at this point in the history
* Added Context to HTML methods

* Fixed unit tests

* Updated timeout

* Fixed WAIT_CLASS timeout
  • Loading branch information
ziflex authored and Владимир Фетисов committed Apr 10, 2019
1 parent 63db1d6 commit 4a040fd
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 124 deletions.
118 changes: 51 additions & 67 deletions pkg/drivers/cdp/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,32 +273,32 @@ func (doc *HTMLDocument) Length() values.Int {
return doc.element.Length()
}

func (doc *HTMLDocument) GetChildNodes() core.Value {
func (doc *HTMLDocument) GetChildNodes(ctx context.Context) core.Value {
doc.Lock()
defer doc.Unlock()

return doc.element.GetChildNodes()
return doc.element.GetChildNodes(ctx)
}

func (doc *HTMLDocument) GetChildNode(idx values.Int) core.Value {
func (doc *HTMLDocument) GetChildNode(ctx context.Context, idx values.Int) core.Value {
doc.Lock()
defer doc.Unlock()

return doc.element.GetChildNode(idx)
return doc.element.GetChildNode(ctx, idx)
}

func (doc *HTMLDocument) QuerySelector(selector values.String) core.Value {
func (doc *HTMLDocument) QuerySelector(ctx context.Context, selector values.String) core.Value {
doc.Lock()
defer doc.Unlock()

return doc.element.QuerySelector(selector)
return doc.element.QuerySelector(ctx, selector)
}

func (doc *HTMLDocument) QuerySelectorAll(selector values.String) core.Value {
func (doc *HTMLDocument) QuerySelectorAll(ctx context.Context, selector values.String) core.Value {
doc.Lock()
defer doc.Unlock()

return doc.element.QuerySelectorAll(selector)
return doc.element.QuerySelectorAll(ctx, selector)
}

func (doc *HTMLDocument) DocumentElement() drivers.HTMLElement {
Expand All @@ -315,26 +315,27 @@ func (doc *HTMLDocument) GetURL() core.Value {
return doc.url
}

func (doc *HTMLDocument) SetURL(url values.String) error {
return doc.Navigate(url, values.Int(DefaultTimeout))
func (doc *HTMLDocument) SetURL(ctx context.Context, url values.String) error {
return doc.Navigate(ctx, url)
}

func (doc *HTMLDocument) CountBySelector(selector values.String) values.Int {
func (doc *HTMLDocument) CountBySelector(ctx context.Context, selector values.String) values.Int {
doc.Lock()
defer doc.Unlock()

return doc.element.CountBySelector(selector)
return doc.element.CountBySelector(ctx, selector)
}

func (doc *HTMLDocument) ExistsBySelector(selector values.String) values.Boolean {
func (doc *HTMLDocument) ExistsBySelector(ctx context.Context, selector values.String) values.Boolean {
doc.Lock()
defer doc.Unlock()

return doc.element.ExistsBySelector(selector)
return doc.element.ExistsBySelector(ctx, selector)
}

func (doc *HTMLDocument) ClickBySelector(selector values.String) (values.Boolean, error) {
func (doc *HTMLDocument) ClickBySelector(ctx context.Context, selector values.String) (values.Boolean, error) {
res, err := eval.Eval(
ctx,
doc.client,
fmt.Sprintf(`
var el = document.querySelector(%s);
Expand All @@ -360,8 +361,9 @@ func (doc *HTMLDocument) ClickBySelector(selector values.String) (values.Boolean
return values.False, nil
}

func (doc *HTMLDocument) ClickBySelectorAll(selector values.String) (values.Boolean, error) {
func (doc *HTMLDocument) ClickBySelectorAll(ctx context.Context, selector values.String) (values.Boolean, error) {
res, err := eval.Eval(
ctx,
doc.client,
fmt.Sprintf(`
var elements = document.querySelectorAll(%s);
Expand Down Expand Up @@ -389,12 +391,11 @@ func (doc *HTMLDocument) ClickBySelectorAll(selector values.String) (values.Bool
return values.False, nil
}

func (doc *HTMLDocument) InputBySelector(selector values.String, value core.Value, delay values.Int) (values.Boolean, error) {
ctx := context.Background()

func (doc *HTMLDocument) InputBySelector(ctx context.Context, selector values.String, value core.Value, delay values.Int) (values.Boolean, error) {
valStr := value.String()

res, err := eval.Eval(
ctx,
doc.client,
fmt.Sprintf(`
var el = document.querySelector(%s);
Expand Down Expand Up @@ -423,18 +424,21 @@ func (doc *HTMLDocument) InputBySelector(selector values.String, value core.Valu
for _, ch := range valStr {
for _, ev := range []string{"keyDown", "keyUp"} {
ke := input.NewDispatchKeyEventArgs(ev).SetText(string(ch))

if err := doc.client.Input.DispatchKeyEvent(ctx, ke); err != nil {
return values.False, err
}

time.Sleep(delayMs * time.Millisecond)
}
}

return values.True, nil
}

func (doc *HTMLDocument) SelectBySelector(selector values.String, value *values.Array) (*values.Array, error) {
func (doc *HTMLDocument) SelectBySelector(ctx context.Context, selector values.String, value *values.Array) (*values.Array, error) {
res, err := eval.Eval(
ctx,
doc.client,
fmt.Sprintf(`
var element = document.querySelector(%s);
Expand Down Expand Up @@ -479,11 +483,8 @@ func (doc *HTMLDocument) SelectBySelector(selector values.String, value *values.
return nil, core.TypeError(types.Array, res.Type())
}

func (doc *HTMLDocument) HoverBySelector(selector values.String) error {
ctx, cancel := contextWithTimeout()
defer cancel()

err := doc.ScrollBySelector(selector)
func (doc *HTMLDocument) HoverBySelector(ctx context.Context, selector values.String) error {
err := doc.ScrollBySelector(ctx, selector)

if err != nil {
return err
Expand Down Expand Up @@ -518,7 +519,7 @@ func (doc *HTMLDocument) HoverBySelector(selector values.String) error {
)
}

func (doc *HTMLDocument) WaitForSelector(selector values.String, timeout values.Int) error {
func (doc *HTMLDocument) WaitForSelector(ctx context.Context, selector values.String) error {
task := events.NewEvalWaitTask(
doc.client,
fmt.Sprintf(`
Expand All @@ -529,16 +530,15 @@ func (doc *HTMLDocument) WaitForSelector(selector values.String, timeout values.
// null means we need to repeat
return null;
`, eval.ParamString(selector.String())),
time.Millisecond*time.Duration(timeout),
events.DefaultPolling,
)

_, err := task.Run()
_, err := task.Run(ctx)

return err
}

func (doc *HTMLDocument) WaitForClassBySelector(selector, class values.String, timeout values.Int) error {
func (doc *HTMLDocument) WaitForClassBySelector(ctx context.Context, selector, class values.String) error {
task := events.NewEvalWaitTask(
doc.client,
fmt.Sprintf(`
Expand All @@ -558,16 +558,15 @@ func (doc *HTMLDocument) WaitForClassBySelector(selector, class values.String, t
eval.ParamString(selector.String()),
eval.ParamString(class.String()),
),
time.Millisecond*time.Duration(timeout),
events.DefaultPolling,
)

_, err := task.Run()
_, err := task.Run(ctx)

return err
}

func (doc *HTMLDocument) WaitForClassBySelectorAll(selector, class values.String, timeout values.Int) error {
func (doc *HTMLDocument) WaitForClassBySelectorAll(ctx context.Context, selector, class values.String) error {
task := events.NewEvalWaitTask(
doc.client,
fmt.Sprintf(`
Expand All @@ -593,23 +592,17 @@ func (doc *HTMLDocument) WaitForClassBySelectorAll(selector, class values.String
eval.ParamString(selector.String()),
eval.ParamString(class.String()),
),
time.Millisecond*time.Duration(timeout),
events.DefaultPolling,
)

_, err := task.Run()
_, err := task.Run(ctx)

return err
}

func (doc *HTMLDocument) WaitForNavigation(timeout values.Int) error {
// do not wait
if timeout == 0 {
return nil
}

func (doc *HTMLDocument) WaitForNavigation(ctx context.Context) error {
onEvent := make(chan struct{})
listener := func(_ interface{}) {
listener := func(_ context.Context, _ interface{}) {
close(onEvent)
}

Expand All @@ -620,17 +613,16 @@ func (doc *HTMLDocument) WaitForNavigation(timeout values.Int) error {
select {
case <-onEvent:
return nil
case <-time.After(time.Millisecond * time.Duration(timeout)):
case <-ctx.Done():
return core.ErrTimeout
}
}

func (doc *HTMLDocument) Navigate(url values.String, timeout values.Int) error {
func (doc *HTMLDocument) Navigate(ctx context.Context, url values.String) error {
if url == "" {
url = BlankPageURL
}

ctx := context.Background()
repl, err := doc.client.Page.Navigate(ctx, page.NewNavigateArgs(url.String()))

if err != nil {
Expand All @@ -641,11 +633,10 @@ func (doc *HTMLDocument) Navigate(url values.String, timeout values.Int) error {
return errors.New(*repl.ErrorText)
}

return doc.WaitForNavigation(timeout)
return doc.WaitForNavigation(ctx)
}

func (doc *HTMLDocument) NavigateBack(skip values.Int, timeout values.Int) (values.Boolean, error) {
ctx := context.Background()
func (doc *HTMLDocument) NavigateBack(ctx context.Context, skip values.Int) (values.Boolean, error) {
history, err := doc.client.Page.GetNavigationHistory(ctx)

if err != nil {
Expand Down Expand Up @@ -675,7 +666,7 @@ func (doc *HTMLDocument) NavigateBack(skip values.Int, timeout values.Int) (valu
return values.False, err
}

err = doc.WaitForNavigation(timeout)
err = doc.WaitForNavigation(ctx)

if err != nil {
return values.False, err
Expand All @@ -684,8 +675,7 @@ 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()
func (doc *HTMLDocument) NavigateForward(ctx context.Context, skip values.Int) (values.Boolean, error) {
history, err := doc.client.Page.GetNavigationHistory(ctx)

if err != nil {
Expand Down Expand Up @@ -718,7 +708,7 @@ func (doc *HTMLDocument) NavigateForward(skip values.Int, timeout values.Int) (v
return values.False, err
}

err = doc.WaitForNavigation(timeout)
err = doc.WaitForNavigation(ctx)

if err != nil {
return values.False, err
Expand All @@ -727,9 +717,7 @@ func (doc *HTMLDocument) NavigateForward(skip values.Int, timeout values.Int) (v
return values.True, nil
}

func (doc *HTMLDocument) PrintToPDF(params drivers.PDFParams) (values.Binary, error) {
ctx := context.Background()

func (doc *HTMLDocument) PrintToPDF(ctx context.Context, params drivers.PDFParams) (values.Binary, error) {
args := page.NewPrintToPDFArgs()
args.
SetLandscape(bool(params.Landscape)).
Expand Down Expand Up @@ -787,8 +775,7 @@ func (doc *HTMLDocument) PrintToPDF(params drivers.PDFParams) (values.Binary, er
return values.NewBinary(reply.Data), nil
}

func (doc *HTMLDocument) CaptureScreenshot(params drivers.ScreenshotParams) (values.Binary, error) {
ctx := context.Background()
func (doc *HTMLDocument) CaptureScreenshot(ctx context.Context, params drivers.ScreenshotParams) (values.Binary, error) {
metrics, err := doc.client.Page.GetLayoutMetrics(ctx)

if params.Format == drivers.ScreenshotFormatJPEG && params.Quality < 0 && params.Quality > 100 {
Expand Down Expand Up @@ -836,8 +823,8 @@ func (doc *HTMLDocument) CaptureScreenshot(params drivers.ScreenshotParams) (val
return values.NewBinary(reply.Data), nil
}

func (doc *HTMLDocument) ScrollTop() error {
_, err := eval.Eval(doc.client, `
func (doc *HTMLDocument) ScrollTop(ctx context.Context) error {
_, err := eval.Eval(ctx, doc.client, `
window.scrollTo({
left: 0,
top: 0,
Expand All @@ -848,8 +835,8 @@ func (doc *HTMLDocument) ScrollTop() error {
return err
}

func (doc *HTMLDocument) ScrollBottom() error {
_, err := eval.Eval(doc.client, `
func (doc *HTMLDocument) ScrollBottom(ctx context.Context) error {
_, err := eval.Eval(ctx, doc.client, `
window.scrollTo({
left: 0,
top: window.document.body.scrollHeight,
Expand All @@ -860,8 +847,8 @@ func (doc *HTMLDocument) ScrollBottom() error {
return err
}

func (doc *HTMLDocument) ScrollBySelector(selector values.String) error {
_, err := eval.Eval(doc.client, fmt.Sprintf(`
func (doc *HTMLDocument) ScrollBySelector(ctx context.Context, selector values.String) error {
_, err := eval.Eval(ctx, doc.client, fmt.Sprintf(`
var el = document.querySelector(%s);
if (el == null) {
throw new Error("element not found");
Expand All @@ -876,13 +863,10 @@ func (doc *HTMLDocument) ScrollBySelector(selector values.String) error {
return err
}

func (doc *HTMLDocument) handlePageLoad(_ interface{}) {
func (doc *HTMLDocument) handlePageLoad(ctx context.Context, _ interface{}) {
doc.Lock()
defer doc.Unlock()

ctx, cancel := contextWithTimeout()
defer cancel()

node, err := getRootElement(ctx, doc.client)

if err != nil {
Expand Down Expand Up @@ -924,7 +908,7 @@ func (doc *HTMLDocument) handlePageLoad(_ interface{}) {
}
}

func (doc *HTMLDocument) handleError(val interface{}) {
func (doc *HTMLDocument) handleError(_ context.Context, val interface{}) {
err, ok := val.(error)

if !ok {
Expand Down
Loading

0 comments on commit 4a040fd

Please sign in to comment.