Skip to content

Commit

Permalink
Feature/MontFerret#250 wait style (MontFerret#257)
Browse files Browse the repository at this point in the history
* Added WAIT_ATTR functions
  • Loading branch information
ziflex authored and Владимир Фетисов committed Apr 10, 2019
1 parent 47c4b5b commit ba17f02
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 48 deletions.
113 changes: 65 additions & 48 deletions pkg/drivers/cdp/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/drivers/cdp/eval"
"github.com/MontFerret/ferret/pkg/drivers/cdp/events"
"github.com/MontFerret/ferret/pkg/drivers/cdp/templates"
"github.com/MontFerret/ferret/pkg/drivers/common"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/logging"
Expand Down Expand Up @@ -527,6 +528,13 @@ func (doc *HTMLDocument) MoveMouseByXY(ctx context.Context, x, y values.Float) e
}

func (doc *HTMLDocument) WaitForElement(ctx context.Context, selector values.String, when drivers.WaitEvent) error {
var operator string

if when == drivers.WaitEventPresence {
operator = "!="
} else {
operator = "=="
}

task := events.NewEvalWaitTask(
doc.client,
Expand All @@ -542,7 +550,7 @@ func (doc *HTMLDocument) WaitForElement(ctx context.Context, selector values.Str
return null;
`,
eval.ParamString(selector.String()),
waitEventToEqOperator(when),
operator,
),
events.DefaultPolling,
)
Expand All @@ -555,26 +563,11 @@ func (doc *HTMLDocument) WaitForElement(ctx context.Context, selector values.Str
func (doc *HTMLDocument) WaitForClassBySelector(ctx context.Context, selector, class values.String, when drivers.WaitEvent) error {
task := events.NewEvalWaitTask(
doc.client,
fmt.Sprintf(`
var el = document.querySelector(%s);
if (el == null) {
return false;
}
var className = %s;
var found = el.className.split(' ').find(i => i === className);
if (found %s null) {
return true;
}
// null means we need to repeat
return null;
`,
eval.ParamString(selector.String()),
eval.ParamString(class.String()),
waitEventToEqOperator(when),
templates.WaitBySelector(
selector,
when,
class,
fmt.Sprintf("el.className.split(' ').find(i => i === %s)", eval.ParamString(class.String())),
),
events.DefaultPolling,
)
Expand All @@ -587,33 +580,11 @@ func (doc *HTMLDocument) WaitForClassBySelector(ctx context.Context, selector, c
func (doc *HTMLDocument) WaitForClassBySelectorAll(ctx context.Context, selector, class values.String, when drivers.WaitEvent) error {
task := events.NewEvalWaitTask(
doc.client,
fmt.Sprintf(`
var elements = document.querySelectorAll(%s);
if (elements == null || elements.length === 0) {
return false;
}
var className = %s;
var foundCount = 0;
elements.forEach((el) => {
var found = el.className.split(' ').find(i => i === className);
if (found %s null) {
foundCount++;
}
});
if (foundCount === elements.length) {
return true;
}
// null means we need to repeat
return null;
`,
eval.ParamString(selector.String()),
eval.ParamString(class.String()),
waitEventToEqOperator(when),
templates.WaitBySelectorAll(
selector,
when,
class,
fmt.Sprintf("el.className.split(' ').find(i => i === %s)", eval.ParamString(class.String())),
),
events.DefaultPolling,
)
Expand Down Expand Up @@ -641,6 +612,52 @@ func (doc *HTMLDocument) WaitForNavigation(ctx context.Context) error {
}
}

func (doc *HTMLDocument) WaitForAttributeBySelector(
ctx context.Context,
selector,
name values.String,
value core.Value,
when drivers.WaitEvent,
) error {
task := events.NewEvalWaitTask(
doc.client,
templates.WaitBySelector(
selector,
when,
value,
templates.AttributeRead(name),
),
events.DefaultPolling,
)

_, err := task.Run(ctx)

return err
}

func (doc *HTMLDocument) WaitForAttributeBySelectorAll(
ctx context.Context,
selector,
name values.String,
value core.Value,
when drivers.WaitEvent,
) error {
task := events.NewEvalWaitTask(
doc.client,
templates.WaitBySelectorAll(
selector,
when,
value,
templates.AttributeRead(name),
),
events.DefaultPolling,
)

_, err := task.Run(ctx)

return err
}

func (doc *HTMLDocument) Navigate(ctx context.Context, url values.String) error {
if url == "" {
url = BlankPageURL
Expand Down
8 changes: 8 additions & 0 deletions pkg/drivers/http/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ func (doc *HTMLDocument) WaitForClassBySelectorAll(_ context.Context, _, _ value
return core.ErrNotSupported
}

func (doc *HTMLDocument) WaitForAttributeBySelector(_ context.Context, _, _ values.String, _ core.Value, _ drivers.WaitEvent) error {
return core.ErrNotSupported
}

func (doc *HTMLDocument) WaitForAttributeBySelectorAll(_ context.Context, _, _ values.String, _ core.Value, _ drivers.WaitEvent) error {
return core.ErrNotSupported
}

func (doc *HTMLDocument) Close() error {
return nil
}
4 changes: 4 additions & 0 deletions pkg/drivers/http/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ func (el *HTMLElement) WaitForClass(_ context.Context, _ values.String, _ driver
return core.ErrNotSupported
}

func (el *HTMLElement) WaitForAttribute(_ context.Context, _ values.String, _ core.Value, _ drivers.WaitEvent) error {
return core.ErrNotSupported
}

func (el *HTMLElement) ensureStyles(ctx context.Context) error {
if el.styles == nil {
styles, err := el.parseStyles(ctx)
Expand Down

0 comments on commit ba17f02

Please sign in to comment.