Skip to content

Commit

Permalink
add examples for handling new tabs and dialogs
Browse files Browse the repository at this point in the history
These are common use cases for event listeners, so add simple examples.
  • Loading branch information
mvdan committed May 13, 2019
1 parent 2408925 commit 04906f5
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
3 changes: 1 addition & 2 deletions event_test.go
Expand Up @@ -162,12 +162,11 @@ func TestClickNewTab(t *testing.T) {

ctx, cancel := testAllocate(t, "newtab.html")
defer cancel()
targetID := FromContext(ctx).Target.TargetID

ch := make(chan target.ID, 1)
ListenTarget(ctx, func(ev interface{}) {
if ev, ok := ev.(*target.EventTargetCreated); ok &&
ev.TargetInfo.OpenerID == targetID {
ev.TargetInfo.OpenerID != "" {
ch <- ev.TargetInfo.TargetID
}
})
Expand Down
83 changes: 79 additions & 4 deletions example_test.go
Expand Up @@ -13,12 +13,15 @@ import (
"path/filepath"
"strings"

"github.com/chromedp/cdproto/page"
cdpruntime "github.com/chromedp/cdproto/runtime"
"github.com/chromedp/cdproto/target"
"github.com/chromedp/chromedp"
)

func writeHTML(content string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
io.WriteString(w, strings.TrimSpace(content))
})
}
Expand All @@ -28,14 +31,12 @@ func ExampleTitle() {
defer cancel()

ts := httptest.NewServer(writeHTML(`
<html>
<head>
<title>fancy website title</title>
</head>
<body>
<div id="content"></div>
</body>
</html>
`))
defer ts.Close()

Expand Down Expand Up @@ -125,7 +126,6 @@ func ExampleListenTarget_consoleLog() {
defer cancel()

ts := httptest.NewServer(writeHTML(`
<html>
<body>
<script>
console.log("hello js world")
Expand All @@ -134,7 +134,6 @@ func ExampleListenTarget_consoleLog() {
document.body.appendChild(p);
</script>
</body>
</html>
`))
defer ts.Close()

Expand All @@ -159,3 +158,79 @@ func ExampleListenTarget_consoleLog() {
// console.log call:
// string - "hello js world"
}

func ExampleClickNewTab() {
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()

mux := http.NewServeMux()
mux.Handle("/first", writeHTML(`
<input id='newtab' type='button' value='open' onclick='window.open("/second", "_blank");'/>
`))
mux.Handle("/second", writeHTML(``))
ts := httptest.NewServer(mux)
defer ts.Close()

lctx, cancel := context.WithCancel(ctx)
ch := make(chan target.ID, 1)
chromedp.ListenTarget(lctx, func(ev interface{}) {
if ev, ok := ev.(*target.EventTargetCreated); ok &&
// if OpenerID == "", this is the first tab.
ev.TargetInfo.OpenerID != "" {
ch <- ev.TargetInfo.TargetID
cancel()
}
})
if err := chromedp.Run(ctx,
chromedp.Navigate(ts.URL+"/first"),
chromedp.Click("#newtab", chromedp.ByID),
); err != nil {
panic(err)
}
newCtx, cancel := chromedp.NewContext(ctx, chromedp.WithTargetID(<-ch))
defer cancel()

var urlstr string
if err := chromedp.Run(newCtx, chromedp.Location(&urlstr)); err != nil {
panic(err)
}
fmt.Println("new tab's path:", strings.TrimPrefix(urlstr, ts.URL))

// Output:
// new tab's path: /second
}

func ExampleAcceptAlert() {
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()

mux := http.NewServeMux()
mux.Handle("/second", writeHTML(``))
ts := httptest.NewServer(writeHTML(`
<input id='alert' type='button' value='alert' onclick='alert("alert text");'/>
`))
defer ts.Close()

chromedp.ListenTarget(ctx, func(ev interface{}) {
if ev, ok := ev.(*page.EventJavascriptDialogOpening); ok {
fmt.Println("closing alert:", ev.Message)
go func() {
if err := chromedp.Run(ctx,
page.HandleJavaScriptDialog(true),
); err != nil {
panic(err)
}
}()
}
})

if err := chromedp.Run(ctx,
chromedp.Navigate(ts.URL),
chromedp.Click("#alert", chromedp.ByID),
); err != nil {
panic(err)
}

// Output:
// closing alert: alert text
}

0 comments on commit 04906f5

Please sign in to comment.