-
-
Notifications
You must be signed in to change notification settings - Fork 303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added ELEMENT_EXISTS function #210
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,12 @@ package main | |
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/MontFerret/ferret/e2e/runner" | ||
"github.com/MontFerret/ferret/e2e/server" | ||
"github.com/rs/zerolog" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
) | ||
|
||
var ( | ||
|
@@ -29,6 +29,12 @@ var ( | |
"http://0.0.0.0:9222", | ||
"address of remote Chrome instance", | ||
) | ||
|
||
filter = flag.String( | ||
"filter", | ||
"", | ||
"regexp expression to filter out tests", | ||
) | ||
) | ||
|
||
func main() { | ||
|
@@ -48,6 +54,19 @@ func main() { | |
Dir: filepath.Join(*pagesDir, "dynamic"), | ||
}) | ||
|
||
var filterR *regexp.Regexp | ||
|
||
if *filter != "" { | ||
r, err := regexp.Compile(*filter) | ||
|
||
if err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think would be better to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would panic the app without a proper message. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will. |
||
fmt.Println(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
filterR = r | ||
} | ||
|
||
go func() { | ||
if err := static.Start(); err != nil { | ||
logger.Info().Timestamp().Msg("shutting down the static pages server") | ||
|
@@ -79,6 +98,7 @@ func main() { | |
DynamicServerAddress: fmt.Sprintf("http://0.0.0.0:%d", dynamicPort), | ||
CDPAddress: *cdp, | ||
Dir: *testsDir, | ||
Filter: filterR, | ||
}) | ||
|
||
err := r.Run() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
LET url = @static + '/overview.html' | ||
LET doc = PAGE(url) | ||
|
||
LET expectedP = TRUE | ||
LET actualP = ELEMENT_EXISTS(doc, '.section-nav') | ||
|
||
LET expectedN = FALSE | ||
LET actualN = ELEMENT_EXISTS(doc, '.foo-bar') | ||
|
||
RETURN EXPECT(expectedP + expectedN, actualP + expectedN) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
LET url = @dynamic | ||
LET doc = PAGE(url) | ||
|
||
LET expectedP = TRUE | ||
LET actualP = ELEMENT_EXISTS(doc, '.text-center') | ||
|
||
LET expectedN = FALSE | ||
LET actualN = ELEMENT_EXISTS(doc, '.foo-bar') | ||
|
||
RETURN EXPECT(expectedP + expectedN, actualP + expectedN) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
LET url = @static + '/value.html' | ||
LET doc = PAGE(url) | ||
|
||
LET el = ELEMENT(doc, "#listings_table") | ||
|
||
LET expectedP = TRUE | ||
LET actualP = ELEMENT_EXISTS(el, '.odd') | ||
|
||
LET expectedN = FALSE | ||
LET actualN = ELEMENT_EXISTS(el, '.foo-bar') | ||
|
||
RETURN EXPECT(expectedP + expectedN, actualP + expectedN) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
LET url = @dynamic | ||
LET doc = PAGE(url) | ||
|
||
LET el = ELEMENT(doc, "#root") | ||
|
||
LET expectedP = TRUE | ||
LET actualP = ELEMENT_EXISTS(el, '.jumbotron') | ||
|
||
LET expectedN = FALSE | ||
LET actualN = ELEMENT_EXISTS(el, '.foo-bar') | ||
|
||
RETURN EXPECT(expectedP + expectedN, actualP + expectedN) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -679,6 +679,33 @@ func (el *HTMLElement) CountBySelector(selector values.String) values.Int { | |
return values.NewInt(len(res.NodeIDs)) | ||
} | ||
|
||
func (el *HTMLElement) ExistsBySelector(selector values.String) values.Boolean { | ||
if !el.IsConnected() { | ||
return values.False | ||
} | ||
|
||
ctx, cancel := contextWithTimeout() | ||
defer cancel() | ||
|
||
// TODO: Can we use RemoteObjectID or BackendID instead of NodeId? | ||
selectorArgs := dom.NewQuerySelectorArgs(el.id.nodeID, selector.String()) | ||
res, err := el.client.DOM.QuerySelector(ctx, selectorArgs) | ||
|
||
if err != nil { | ||
el.logError(err). | ||
Str("selector", selector.String()). | ||
Msg("failed to retrieve nodes by selector") | ||
|
||
return values.False | ||
} | ||
|
||
if res.NodeID == 0 { | ||
return values.False | ||
} | ||
|
||
return values.True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @3timeslazy what about code readability? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ¯_(ツ)_/¯ |
||
} | ||
|
||
func (el *HTMLElement) WaitForClass(class values.String, timeout values.Int) error { | ||
task := events.NewWaitTask( | ||
func() (core.Value, error) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -248,6 +248,16 @@ func (el *HTMLElement) CountBySelector(selector values.String) values.Int { | |
return values.NewInt(selection.Size()) | ||
} | ||
|
||
func (el *HTMLElement) ExistsBySelector(selector values.String) values.Boolean { | ||
selection := el.selection.Closest(selector.String()) | ||
|
||
if selection == nil { | ||
return values.False | ||
} | ||
|
||
return values.True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same with this. |
||
} | ||
|
||
func (el *HTMLElement) parseAttrs() *values.Object { | ||
obj := values.NewObject() | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package html | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/MontFerret/ferret/pkg/runtime/core" | ||
"github.com/MontFerret/ferret/pkg/runtime/values" | ||
) | ||
|
||
// ElementExists returns a boolean value indicating whether there is an element matched by selector. | ||
// @param docOrEl (HTMLDocument|HTMLElement) - 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) { | ||
el, selector, err := queryArgs(args) | ||
|
||
if err != nil { | ||
return values.None, err | ||
} | ||
|
||
return el.ExistsBySelector(selector), nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you can use
goimports
util.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@3timeslazy Could you give me more details on that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/bradfitz/goimports