b977e305d2
We don't want to always set c.first, as that can change the field from true to false.
51 lines
1.0 KiB
Go
51 lines
1.0 KiB
Go
package chromedp
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
func TestTargets(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Start one browser with one tab.
|
|
ctx1, cancel1 := NewContext(context.Background())
|
|
defer cancel1()
|
|
if err := Run(ctx1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
wantTargets := func(ctx context.Context, want int) {
|
|
t.Helper()
|
|
infos, err := Targets(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := len(infos); want != got {
|
|
t.Fatalf("want %d targets, got %d", want, got)
|
|
}
|
|
}
|
|
wantTargets(ctx1, 1)
|
|
|
|
// Start a second tab on the same browser.
|
|
ctx2, cancel2 := NewContext(ctx1)
|
|
defer cancel2()
|
|
if err := Run(ctx2); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wantTargets(ctx2, 2)
|
|
|
|
// The first context should also see both targets.
|
|
wantTargets(ctx1, 2)
|
|
|
|
// Cancelling the second context should close the second tab alone.
|
|
cancel2()
|
|
wantTargets(ctx1, 1)
|
|
|
|
// We used to have a bug where Run would reset the first context as if
|
|
// it weren't the first, breaking its cancellation.
|
|
if err := Run(ctx1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|