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.
Uh oh!
There was an error while loading. Please reload this page.
-
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
Timeoutfor 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
Mustis a bit ugly too.I'm considering writing wrappers around the core types like
PageandElementso I can do something like the following:The wrapper types would contain the contain the
*testing.Tand the*rod.Browserorrod.Pageand delegate calls to the non-Mustmethods. 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
PageandElement. 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