sourcegraph / go-selenium

Selenium WebDriver client for Go
https://sourcegraph.com/github.com/sourcegraph/go-selenium
Other
367 stars 73 forks source link

Add support for context.Context automatic Quit() #31

Closed pwaller closed 8 years ago

pwaller commented 8 years ago

It's annoying if you interrupt tests to have them leave a browser window open.

You might have to wait a while before you can run your tests again.

This adds support for go 1.7's context.Context. It instruments all requests, causing them to cancel the outgoing HTTP request to selenium if the context is canceled.

In addition, it checks on entry and exit to a HTTP request if the context is canceled, to ensure we notice rapidly.

If the context is canceled, wd.Quit() is called. For convenience, it's made legal to call quit more than once, so that code which does:

wd := makeWD()
defer wd.Quit()

Remains legal.

An example of use:

var cancel func()
ctx, cancel = context.WithCancel(context.Background())
go func() {
    interrupt := make(chan os.Signal)
    signal.Notify(interrupt, os.Interrupt)
    <-interrupt
    log.Printf("Received interrupt.")
    cancel()
}()

wd := makeWD()
wd.SetContext(ctx)

wd.Get(...)
// ... etc, etc.

The net effect is that interrupting the tests now give X failed: canceled, and the browser window quits.

sqs commented 8 years ago

Thanks!