Updating examples/simple/main.go

This commit is contained in:
Kenneth Shaw 2017-10-07 06:30:23 +07:00
parent 20927f98e0
commit 28ee6338d2
2 changed files with 18 additions and 16 deletions

View File

@ -25,6 +25,7 @@ the right link, and then taking a screenshot of a specific element on the
loaded page and saving that to a local file on disk.
```go
// examples/simple/main.go
package main
import (
@ -53,7 +54,7 @@ func main() {
// run task list
var site, res string
err = c.Run(ctxt, googleSearch("site:brank.as", "Easy Money Management", &site, &res))
err = c.Run(ctxt, googleSearch("site:brank.as", "Home", &site, &res))
if err != nil {
log.Fatal(err)
}
@ -70,7 +71,7 @@ func main() {
log.Fatal(err)
}
log.Printf("saved screenshot of #testimonials from search result listing `%s` (%s)", res, site)
log.Printf("saved screenshot from search result listing `%s` (%s)", res, site)
}
func googleSearch(q, text string, site, res *string) cdp.Tasks {
@ -78,19 +79,19 @@ func googleSearch(q, text string, site, res *string) cdp.Tasks {
sel := fmt.Sprintf(`//a[text()[contains(., '%s')]]`, text)
return cdp.Tasks{
cdp.Navigate(`https://www.google.com`),
cdp.Sleep(2 * time.Second),
cdp.WaitVisible(`#hplogo`, cdp.ByID),
cdp.SendKeys(`#lst-ib`, q+"\n", cdp.ByID),
cdp.WaitVisible(`#res`, cdp.ByID),
cdp.Text(sel, res),
cdp.Click(sel),
cdp.Sleep(2 * time.Second),
cdp.WaitVisible(`#footer`, cdp.ByQuery),
cdp.WaitNotVisible(`div.v-middle > div.la-ball-clip-rotate`, cdp.ByQuery),
cdp.WaitVisible(`a[href="/brankas-for-business"]`, cdp.ByQuery),
cdp.WaitNotVisible(`.preloader-content`, cdp.ByQuery),
cdp.Location(site),
cdp.Screenshot(`#testimonials`, &buf, cdp.ByID),
cdp.ScrollIntoView(`.banner-section.third-section`, cdp.ByQuery),
cdp.Sleep(2 * time.Second), // wait for animation to finish
cdp.Screenshot(`.banner-section.third-section`, &buf, cdp.ByQuery),
cdp.ActionFunc(func(context.Context, cdptypes.Handler) error {
return ioutil.WriteFile("testimonials.png", buf, 0644)
return ioutil.WriteFile("screenshot.png", buf, 0644)
}),
}
}

View File

@ -1,3 +1,4 @@
// examples/simple/main.go
package main
import (
@ -26,7 +27,7 @@ func main() {
// run task list
var site, res string
err = c.Run(ctxt, googleSearch("site:brank.as", "Easy Money Management", &site, &res))
err = c.Run(ctxt, googleSearch("site:brank.as", "Home", &site, &res))
if err != nil {
log.Fatal(err)
}
@ -43,7 +44,7 @@ func main() {
log.Fatal(err)
}
log.Printf("saved screenshot of #testimonials from search result listing `%s` (%s)", res, site)
log.Printf("saved screenshot from search result listing `%s` (%s)", res, site)
}
func googleSearch(q, text string, site, res *string) cdp.Tasks {
@ -51,19 +52,19 @@ func googleSearch(q, text string, site, res *string) cdp.Tasks {
sel := fmt.Sprintf(`//a[text()[contains(., '%s')]]`, text)
return cdp.Tasks{
cdp.Navigate(`https://www.google.com`),
cdp.Sleep(2 * time.Second),
cdp.WaitVisible(`#hplogo`, cdp.ByID),
cdp.SendKeys(`#lst-ib`, q+"\n", cdp.ByID),
cdp.WaitVisible(`#res`, cdp.ByID),
cdp.Text(sel, res),
cdp.Click(sel),
cdp.Sleep(2 * time.Second),
cdp.WaitVisible(`#footer`, cdp.ByQuery),
cdp.WaitNotVisible(`div.v-middle > div.la-ball-clip-rotate`, cdp.ByQuery),
cdp.WaitVisible(`a[href="/brankas-for-business"]`, cdp.ByQuery),
cdp.WaitNotVisible(`.preloader-content`, cdp.ByQuery),
cdp.Location(site),
cdp.Screenshot(`#testimonials`, &buf, cdp.ByID),
cdp.ScrollIntoView(`.banner-section.third-section`, cdp.ByQuery),
cdp.Sleep(2 * time.Second), // wait for animation to finish
cdp.Screenshot(`.banner-section.third-section`, &buf, cdp.ByQuery),
cdp.ActionFunc(func(context.Context, cdptypes.Handler) error {
return ioutil.WriteFile("testimonials.png", buf, 0644)
return ioutil.WriteFile("screenshot.png", buf, 0644)
}),
}
}