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:
Daniel Martí 2019-04-01 16:57:22 +01:00
parent fb23c1750a
commit 0d568ec2a4
5 changed files with 35 additions and 35 deletions

View File

@ -20,10 +20,10 @@ func TestExecAllocator(t *testing.T) {
want := "insert"
var got string
if err := Run(taskCtx, Tasks{
Navigate(testdataDir + "/form.html"),
if err := Run(taskCtx,
Navigate(testdataDir+"/form.html"),
Text("#foo", &got, ByID),
}); err != nil {
); err != nil {
t.Fatal(err)
}
if got != want {
@ -52,7 +52,7 @@ func TestExecAllocatorCancelParent(t *testing.T) {
// processes and browsers.
taskCtx, _ := NewContext(poolCtx)
if err := Run(taskCtx, Tasks{}); err != nil {
if err := Run(taskCtx); err != nil {
t.Fatal(err)
}

View File

@ -60,7 +60,7 @@ func FromContext(ctx context.Context) *Context {
// Run runs an action against the provided context. The provided context must
// contain a valid Allocator; typically, that will be created via NewContext or
// NewAllocator.
func Run(ctx context.Context, action Action) error {
func Run(ctx context.Context, actions ...Action) error {
c := FromContext(ctx)
if c == nil || c.Allocator == nil {
return ErrInvalidContext
@ -77,7 +77,7 @@ func Run(ctx context.Context, action Action) error {
return err
}
}
return action.Do(ctx, c.Target)
return Tasks(actions).Do(ctx, c.Target)
}
func (c *Context) newSession(ctx context.Context) error {

View File

@ -16,11 +16,11 @@ func ExampleTitle() {
defer cancel()
var title string
if err := chromedp.Run(ctx, chromedp.Tasks{
if err := chromedp.Run(ctx,
chromedp.Navigate("https://github.com/chromedp/chromedp/issues"),
chromedp.WaitVisible("#start-of-content", chromedp.ByID),
chromedp.Title(&title),
}); err != nil {
); err != nil {
panic(err)
}
@ -57,7 +57,7 @@ func ExampleExecAllocatorOption() {
defer cancel()
// 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)
}
@ -83,7 +83,7 @@ func ExampleManyTabs() {
defer cancel()
// ensure the first tab is created
if err := chromedp.Run(ctx1, chromedp.Tasks{}); err != nil {
if err := chromedp.Run(ctx1); err != nil {
panic(err)
}
@ -91,7 +91,7 @@ func ExampleManyTabs() {
ctx2, _ := chromedp.NewContext(ctx1)
// ensure the second tab is created
if err := chromedp.Run(ctx2, chromedp.Tasks{}); err != nil {
if err := chromedp.Run(ctx2); err != nil {
panic(err)
}

View File

@ -73,11 +73,11 @@ func TestNavigationEntries(t *testing.T) {
expIdx, expEntries := 1, 2
for i, test := range tests {
if err := Run(ctx, Tasks{
Navigate(testdataDir + "/" + test.file),
if err := Run(ctx,
Navigate(testdataDir+"/"+test.file),
WaitVisible(test.waitID, ByID),
NavigationEntries(&index, &entries),
}); err != nil {
); err != nil {
t.Fatal(err)
}
if len(entries) != expEntries {
@ -100,22 +100,22 @@ func TestNavigateToHistoryEntry(t *testing.T) {
var entries []*page.NavigationEntry
var index int64
if err := Run(ctx, Tasks{
if err := Run(ctx,
WaitVisible(`#icon-brankas`, ByID), // for image.html
NavigationEntries(&index, &entries),
Navigate(testdataDir + "/form.html"),
Navigate(testdataDir+"/form.html"),
WaitVisible(`#form`, ByID), // for form.html
}); err != nil {
); err != nil {
t.Fatal(err)
}
var title string
if err := Run(ctx, Tasks{
if err := Run(ctx,
NavigateToHistoryEntry(entries[index].ID),
WaitVisible(`#icon-brankas`, ByID), // for image.html
Title(&title),
}); err != nil {
); err != nil {
t.Fatal(err)
}
if title != entries[index].Title {
@ -130,17 +130,17 @@ func TestNavigateBack(t *testing.T) {
defer cancel()
var title, exptitle string
if err := Run(ctx, Tasks{
if err := Run(ctx,
WaitVisible(`#form`, ByID), // for form.html
Title(&exptitle),
Navigate(testdataDir + "/image.html"),
Navigate(testdataDir+"/image.html"),
WaitVisible(`#icon-brankas`, ByID), // for image.html
NavigateBack(),
WaitVisible(`#form`, ByID), // for form.html
Title(&title),
}); err != nil {
); err != nil {
t.Fatal(err)
}
@ -156,10 +156,10 @@ func TestNavigateForward(t *testing.T) {
defer cancel()
var title, exptitle string
if err := Run(ctx, Tasks{
if err := Run(ctx,
WaitVisible(`#form`, ByID), // for form.html
Navigate(testdataDir + "/image.html"),
Navigate(testdataDir+"/image.html"),
WaitVisible(`#icon-brankas`, ByID), // for image.html
Title(&exptitle),
@ -169,7 +169,7 @@ func TestNavigateForward(t *testing.T) {
NavigateForward(),
WaitVisible(`#icon-brankas`, ByID), // for image.html
Title(&title),
}); err != nil {
); err != nil {
t.Fatal(err)
}
@ -211,7 +211,7 @@ func TestReload(t *testing.T) {
defer cancel()
var title, exptitle string
if err := Run(ctx, Tasks{
if err := Run(ctx,
Navigate(s.URL),
WaitReady(`#count0`, ByID),
Title(&exptitle),
@ -219,7 +219,7 @@ func TestReload(t *testing.T) {
Reload(),
WaitReady(`#count1`, ByID),
Title(&title),
}); err != nil {
); err != nil {
t.Fatal(err)
}
@ -237,11 +237,11 @@ func TestCaptureScreenshot(t *testing.T) {
// set the viewport size, to know what screenshot size to expect
width, height := 650, 450
var buf []byte
if err := Run(ctx, Tasks{
if err := Run(ctx,
emulation.SetDeviceMetricsOverride(int64(width), int64(height), 1.0, false),
WaitVisible(`#icon-brankas`, ByID), // for image.html
CaptureScreenshot(&buf),
}); err != nil {
); err != nil {
t.Fatal(err)
}
@ -315,10 +315,10 @@ func TestLocation(t *testing.T) {
defer cancel()
var urlstr string
if err := Run(ctx, Tasks{
if err := Run(ctx,
WaitVisible(`#form`, ByID), // for form.html
Location(&urlstr),
}); err != nil {
); err != nil {
t.Fatal(err)
}
@ -334,10 +334,10 @@ func TestTitle(t *testing.T) {
defer cancel()
var title string
if err := Run(ctx, Tasks{
if err := Run(ctx,
WaitVisible(`#icon-brankas`, ByID), // for image.html
Title(&title),
}); err != nil {
); err != nil {
t.Fatal(err)
}

View File

@ -994,12 +994,12 @@ func TestFileUpload(t *testing.T) {
defer cancel()
var result string
if err := Run(ctx, Tasks{
if err := Run(ctx,
Navigate(s.URL),
test.a,
Click(`input[name="submit"]`),
Text(`#result`, &result, ByID, NodeVisible),
}); err != nil {
); err != nil {
t.Fatalf("test %d expected no error, got: %v", i, err)
}