change Run to allow many actions
This can simplify some common use cases, like running a few actions directly, or running no actions at all. It's also an almost entirely backwards compatible change, as all Run call sites should continue to compile and work. Leave Tasks, as it can still be useful for functions to return complex sequences of actions as a single Action.
This commit is contained in:
parent
fb23c1750a
commit
0d568ec2a4
|
@ -20,10 +20,10 @@ func TestExecAllocator(t *testing.T) {
|
||||||
|
|
||||||
want := "insert"
|
want := "insert"
|
||||||
var got string
|
var got string
|
||||||
if err := Run(taskCtx, Tasks{
|
if err := Run(taskCtx,
|
||||||
Navigate(testdataDir + "/form.html"),
|
Navigate(testdataDir+"/form.html"),
|
||||||
Text("#foo", &got, ByID),
|
Text("#foo", &got, ByID),
|
||||||
}); err != nil {
|
); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if got != want {
|
if got != want {
|
||||||
|
@ -52,7 +52,7 @@ func TestExecAllocatorCancelParent(t *testing.T) {
|
||||||
// processes and browsers.
|
// processes and browsers.
|
||||||
|
|
||||||
taskCtx, _ := NewContext(poolCtx)
|
taskCtx, _ := NewContext(poolCtx)
|
||||||
if err := Run(taskCtx, Tasks{}); err != nil {
|
if err := Run(taskCtx); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,7 @@ func FromContext(ctx context.Context) *Context {
|
||||||
// Run runs an action against the provided context. The provided context must
|
// Run runs an action against the provided context. The provided context must
|
||||||
// contain a valid Allocator; typically, that will be created via NewContext or
|
// contain a valid Allocator; typically, that will be created via NewContext or
|
||||||
// NewAllocator.
|
// NewAllocator.
|
||||||
func Run(ctx context.Context, action Action) error {
|
func Run(ctx context.Context, actions ...Action) error {
|
||||||
c := FromContext(ctx)
|
c := FromContext(ctx)
|
||||||
if c == nil || c.Allocator == nil {
|
if c == nil || c.Allocator == nil {
|
||||||
return ErrInvalidContext
|
return ErrInvalidContext
|
||||||
|
@ -77,7 +77,7 @@ func Run(ctx context.Context, action Action) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return action.Do(ctx, c.Target)
|
return Tasks(actions).Do(ctx, c.Target)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) newSession(ctx context.Context) error {
|
func (c *Context) newSession(ctx context.Context) error {
|
||||||
|
|
|
@ -16,11 +16,11 @@ func ExampleTitle() {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
var title string
|
var title string
|
||||||
if err := chromedp.Run(ctx, chromedp.Tasks{
|
if err := chromedp.Run(ctx,
|
||||||
chromedp.Navigate("https://github.com/chromedp/chromedp/issues"),
|
chromedp.Navigate("https://github.com/chromedp/chromedp/issues"),
|
||||||
chromedp.WaitVisible("#start-of-content", chromedp.ByID),
|
chromedp.WaitVisible("#start-of-content", chromedp.ByID),
|
||||||
chromedp.Title(&title),
|
chromedp.Title(&title),
|
||||||
}); err != nil {
|
); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ func ExampleExecAllocatorOption() {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// ensure that the browser process is started
|
// ensure that the browser process is started
|
||||||
if err := chromedp.Run(taskCtx, chromedp.Tasks{}); err != nil {
|
if err := chromedp.Run(taskCtx); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,7 +83,7 @@ func ExampleManyTabs() {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// ensure the first tab is created
|
// ensure the first tab is created
|
||||||
if err := chromedp.Run(ctx1, chromedp.Tasks{}); err != nil {
|
if err := chromedp.Run(ctx1); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,7 +91,7 @@ func ExampleManyTabs() {
|
||||||
ctx2, _ := chromedp.NewContext(ctx1)
|
ctx2, _ := chromedp.NewContext(ctx1)
|
||||||
|
|
||||||
// ensure the second tab is created
|
// ensure the second tab is created
|
||||||
if err := chromedp.Run(ctx2, chromedp.Tasks{}); err != nil {
|
if err := chromedp.Run(ctx2); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
44
nav_test.go
44
nav_test.go
|
@ -73,11 +73,11 @@ func TestNavigationEntries(t *testing.T) {
|
||||||
|
|
||||||
expIdx, expEntries := 1, 2
|
expIdx, expEntries := 1, 2
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
if err := Run(ctx, Tasks{
|
if err := Run(ctx,
|
||||||
Navigate(testdataDir + "/" + test.file),
|
Navigate(testdataDir+"/"+test.file),
|
||||||
WaitVisible(test.waitID, ByID),
|
WaitVisible(test.waitID, ByID),
|
||||||
NavigationEntries(&index, &entries),
|
NavigationEntries(&index, &entries),
|
||||||
}); err != nil {
|
); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if len(entries) != expEntries {
|
if len(entries) != expEntries {
|
||||||
|
@ -100,22 +100,22 @@ func TestNavigateToHistoryEntry(t *testing.T) {
|
||||||
|
|
||||||
var entries []*page.NavigationEntry
|
var entries []*page.NavigationEntry
|
||||||
var index int64
|
var index int64
|
||||||
if err := Run(ctx, Tasks{
|
if err := Run(ctx,
|
||||||
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
||||||
NavigationEntries(&index, &entries),
|
NavigationEntries(&index, &entries),
|
||||||
|
|
||||||
Navigate(testdataDir + "/form.html"),
|
Navigate(testdataDir+"/form.html"),
|
||||||
WaitVisible(`#form`, ByID), // for form.html
|
WaitVisible(`#form`, ByID), // for form.html
|
||||||
}); err != nil {
|
); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var title string
|
var title string
|
||||||
if err := Run(ctx, Tasks{
|
if err := Run(ctx,
|
||||||
NavigateToHistoryEntry(entries[index].ID),
|
NavigateToHistoryEntry(entries[index].ID),
|
||||||
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
||||||
Title(&title),
|
Title(&title),
|
||||||
}); err != nil {
|
); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if title != entries[index].Title {
|
if title != entries[index].Title {
|
||||||
|
@ -130,17 +130,17 @@ func TestNavigateBack(t *testing.T) {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
var title, exptitle string
|
var title, exptitle string
|
||||||
if err := Run(ctx, Tasks{
|
if err := Run(ctx,
|
||||||
WaitVisible(`#form`, ByID), // for form.html
|
WaitVisible(`#form`, ByID), // for form.html
|
||||||
Title(&exptitle),
|
Title(&exptitle),
|
||||||
|
|
||||||
Navigate(testdataDir + "/image.html"),
|
Navigate(testdataDir+"/image.html"),
|
||||||
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
||||||
|
|
||||||
NavigateBack(),
|
NavigateBack(),
|
||||||
WaitVisible(`#form`, ByID), // for form.html
|
WaitVisible(`#form`, ByID), // for form.html
|
||||||
Title(&title),
|
Title(&title),
|
||||||
}); err != nil {
|
); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,10 +156,10 @@ func TestNavigateForward(t *testing.T) {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
var title, exptitle string
|
var title, exptitle string
|
||||||
if err := Run(ctx, Tasks{
|
if err := Run(ctx,
|
||||||
WaitVisible(`#form`, ByID), // for form.html
|
WaitVisible(`#form`, ByID), // for form.html
|
||||||
|
|
||||||
Navigate(testdataDir + "/image.html"),
|
Navigate(testdataDir+"/image.html"),
|
||||||
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
||||||
Title(&exptitle),
|
Title(&exptitle),
|
||||||
|
|
||||||
|
@ -169,7 +169,7 @@ func TestNavigateForward(t *testing.T) {
|
||||||
NavigateForward(),
|
NavigateForward(),
|
||||||
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
||||||
Title(&title),
|
Title(&title),
|
||||||
}); err != nil {
|
); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,7 +211,7 @@ func TestReload(t *testing.T) {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
var title, exptitle string
|
var title, exptitle string
|
||||||
if err := Run(ctx, Tasks{
|
if err := Run(ctx,
|
||||||
Navigate(s.URL),
|
Navigate(s.URL),
|
||||||
WaitReady(`#count0`, ByID),
|
WaitReady(`#count0`, ByID),
|
||||||
Title(&exptitle),
|
Title(&exptitle),
|
||||||
|
@ -219,7 +219,7 @@ func TestReload(t *testing.T) {
|
||||||
Reload(),
|
Reload(),
|
||||||
WaitReady(`#count1`, ByID),
|
WaitReady(`#count1`, ByID),
|
||||||
Title(&title),
|
Title(&title),
|
||||||
}); err != nil {
|
); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -237,11 +237,11 @@ func TestCaptureScreenshot(t *testing.T) {
|
||||||
// set the viewport size, to know what screenshot size to expect
|
// set the viewport size, to know what screenshot size to expect
|
||||||
width, height := 650, 450
|
width, height := 650, 450
|
||||||
var buf []byte
|
var buf []byte
|
||||||
if err := Run(ctx, Tasks{
|
if err := Run(ctx,
|
||||||
emulation.SetDeviceMetricsOverride(int64(width), int64(height), 1.0, false),
|
emulation.SetDeviceMetricsOverride(int64(width), int64(height), 1.0, false),
|
||||||
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
||||||
CaptureScreenshot(&buf),
|
CaptureScreenshot(&buf),
|
||||||
}); err != nil {
|
); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -315,10 +315,10 @@ func TestLocation(t *testing.T) {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
var urlstr string
|
var urlstr string
|
||||||
if err := Run(ctx, Tasks{
|
if err := Run(ctx,
|
||||||
WaitVisible(`#form`, ByID), // for form.html
|
WaitVisible(`#form`, ByID), // for form.html
|
||||||
Location(&urlstr),
|
Location(&urlstr),
|
||||||
}); err != nil {
|
); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -334,10 +334,10 @@ func TestTitle(t *testing.T) {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
var title string
|
var title string
|
||||||
if err := Run(ctx, Tasks{
|
if err := Run(ctx,
|
||||||
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
||||||
Title(&title),
|
Title(&title),
|
||||||
}); err != nil {
|
); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -994,12 +994,12 @@ func TestFileUpload(t *testing.T) {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
var result string
|
var result string
|
||||||
if err := Run(ctx, Tasks{
|
if err := Run(ctx,
|
||||||
Navigate(s.URL),
|
Navigate(s.URL),
|
||||||
test.a,
|
test.a,
|
||||||
Click(`input[name="submit"]`),
|
Click(`input[name="submit"]`),
|
||||||
Text(`#result`, &result, ByID, NodeVisible),
|
Text(`#result`, &result, ByID, NodeVisible),
|
||||||
}); err != nil {
|
); err != nil {
|
||||||
t.Fatalf("test %d expected no error, got: %v", i, err)
|
t.Fatalf("test %d expected no error, got: %v", i, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user