Replies: 3 comments
-
Yes, you can. The API is package main
import (
"context"
"fmt"
"time"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/utils"
)
func main() {
page := rod.New().MustConnect().MustPage("http://example.com").Sleeper(mySleeper)
page.MustElement("a")
page.MustElement("a")
}
func mySleeper() utils.Sleeper {
total := time.After(10 * time.Second)
return func(ctx context.Context) error {
select {
case <-time.After(300 * time.Millisecond): // for each retry
return nil
case <-ctx.Done():
return ctx.Err()
case <-total:
return fmt.Errorf("timeout after 10s")
}
}
} |
Beta Was this translation helpful? Give feedback.
-
If you want to create a test framework based on rod, please check https://github.com/ysmood/got, rod also uses it to do testing. I think something like this is enough for you: package main_test
import (
"runtime"
"testing"
"github.com/go-rod/rod"
)
func TestLab(t *testing.T) {
b := rod.New().MustConnect().WithPanic(func(v interface{}) {
_, file, line, _ := runtime.Caller(3)
t.Logf("%v\n at %s:%d", v, file, line)
t.FailNow()
})
page := b.MustPage("http://example.com")
page.MustEval("a")
} |
Beta Was this translation helpful? Give feedback.
-
Oh nice! Those are both big helps. Thanks! |
Beta Was this translation helpful? Give feedback.
-
I'm using rod for browser integration testing its performance is excellent. With a little bit of wrapper logic I'm able to spin up multiple sets of database, application server, and rod controlled browser to run my tests in parallel. It runs much faster than what I used to do in Capybara.
However, I'm missing some of the conveniences that Capybara and Ruby provided.
Default timeouts per action. I set a timeout for the entire test but it doesn't appear there is any way to automatically set a short timeout for each action. e.g. I have a 30 second timeout for the entire test. But when an expected element is missing early in a test I still have to wait the full 30 seconds for it to fail. I would like to wait a few seconds then give up without having to manually call
Timeout
for eachMustElement
.Must...
methods cause a panic instead of a test failure. This means test output is a stack trace where it is difficult to quickly see on what line the failure occurred. I'd rather get a normal test failure with a message likecould not find element with selector ...
.Every method being prefixed by
Must
is a bit ugly too.I'm considering writing wrappers around the core types like
Page
andElement
so I can do something like the following:The wrapper types would contain the contain the
*testing.T
and the*rod.Browser
orrod.Page
and delegate calls to the non-Must
methods. If an error was returned they would callt.Fatal(err)
.It seems that it would be fairly straightforward to build though a bit laborious given how many methods are on
Page
andElement
. So I before I build anything I wanted to ask if this seems like a good or bad idea, if it has already been done, or if there is a better approach.Beta Was this translation helpful? Give feedback.
All reactions