chromedp/context_test.go
Daniel Martí b647c708b4 don't create an extra tab when starting a browser
Chrome already starts with a blank page, so use that for the first
target context instead of creating a new tab.

Add the first version of the Targets API, which is useful to test this
feature.

Fixes #291.
2019-04-07 01:18:15 +02:00

56 lines
984 B
Go

package chromedp
import (
"context"
"testing"
)
func TestTargets(t *testing.T) {
t.Parallel()
// Start one browser with one tab.
ctx1, cancel := NewContext(context.Background())
defer cancel()
if err := Run(ctx1); err != nil {
t.Fatal(err)
}
{
infos, err := Targets(ctx1)
if err != nil {
t.Fatal(err)
}
if want, got := 1, len(infos); want != got {
t.Fatalf("want %d targets, got %d", want, got)
}
}
// Start a second tab on the same browser
ctx2, cancel := NewContext(ctx1)
defer cancel()
if err := Run(ctx2); err != nil {
t.Fatal(err)
}
{
infos, err := Targets(ctx2)
if err != nil {
t.Fatal(err)
}
if want, got := 2, len(infos); want != got {
t.Fatalf("want %d targets, got %d", want, got)
}
}
// The first context should also see both targets.
{
infos, err := Targets(ctx1)
if err != nil {
t.Fatal(err)
}
if want, got := 2, len(infos); want != got {
t.Fatalf("want %d targets, got %d", want, got)
}
}
}