From 9aab67b659ecdf50d17152ca0eb7a547c5f4aa1c Mon Sep 17 00:00:00 2001 From: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> Date: Thu, 16 Jun 2022 17:49:29 -0400 Subject: [PATCH] Update examples (#627) * Fix click example Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> * Fix eval example Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> * Fix screenshot example Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> * Fix submit example Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> * Fix text example Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> * Fix translator example Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> * Fix minor typo Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- .github/CONTRIBUTING.md | 8 +++++++- browser_test.go | 2 +- examples_test.go | 4 ++-- lib/assets/README.md | 2 +- lib/examples/compare-chromedp/click/main.go | 6 +++--- lib/examples/compare-chromedp/eval/main.go | 2 +- lib/examples/compare-chromedp/remote/main.go | 2 +- lib/examples/compare-chromedp/screenshot/main.go | 2 +- lib/examples/compare-chromedp/submit/main.go | 2 +- lib/examples/compare-chromedp/text/main.go | 4 ++-- lib/examples/translator/main.go | 15 +++++++++++---- lib/js/helper.js | 4 ++-- 12 files changed, 33 insertions(+), 20 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index d419c290..809f0e9f 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -36,7 +36,13 @@ Object model: ## Run tests -No magic, just `go test`. Read the test template [lab_test.go](../lab_test.go) to get started. +First, set this environment variable: + +```bash +export GODEBUG="tracebackancestors=1000" +``` + +Then, no magic, just `go test`. Read the test template [lab_test.go](../lab_test.go) to get started. The entry point of tests is [setup_test.go](../setup_test.go). All the test helpers are defined in it. diff --git a/browser_test.go b/browser_test.go index a3906a10..63b47f2b 100644 --- a/browser_test.go +++ b/browser_test.go @@ -284,7 +284,7 @@ func TestBinarySize(t *testing.T) { stat, err := os.Stat("tmp/translator") g.E(err) - g.Lte(float64(stat.Size())/1024/1024, 9.3) // mb + g.Lte(float64(stat.Size())/1024/1024, 9.4) // mb } func TestBrowserCookies(t *testing.T) { diff --git a/examples_test.go b/examples_test.go index 028157e3..d0579361 100644 --- a/examples_test.go +++ b/examples_test.go @@ -63,7 +63,7 @@ func Example() { // Rod provides a lot of debug options, you can set them with setter methods or use environment variables. // Doc for environment variables: https://pkg.go.dev/github.com/go-rod/rod/lib/defaults func Example_disable_headless_to_debug() { - // Headless runs the browser on foreground, you can also use env "rod=show" + // Headless runs the browser on foreground, you can also use flag "-rod=show" // Devtools opens the tab in each new tab opened automatically l := launcher.New(). Headless(false). @@ -84,7 +84,7 @@ func Example_disable_headless_to_debug() { // ServeMonitor plays screenshots of each tab. This feature is extremely // useful when debugging with headless mode. - // You can also enable it with env rod=monitor + // You can also enable it with flag "-rod=monitor" launcher.Open(browser.ServeMonitor("")) defer browser.MustClose() diff --git a/lib/assets/README.md b/lib/assets/README.md index 84e0d1e2..2948ee31 100644 --- a/lib/assets/README.md +++ b/lib/assets/README.md @@ -1,3 +1,3 @@ -# Asserts +# Assets Static files for the project diff --git a/lib/examples/compare-chromedp/click/main.go b/lib/examples/compare-chromedp/click/main.go index 2dbd815b..e31b8af9 100644 --- a/lib/examples/compare-chromedp/click/main.go +++ b/lib/examples/compare-chromedp/click/main.go @@ -13,16 +13,16 @@ func main() { MustConnect(). Trace(true). // log useful info about what rod is doing Timeout(15 * time.Second). - MustPage("https://golang.org/pkg/time/") + MustPage("https://pkg.go.dev/time/") // wait for footer element is visible (ie, page is loaded) page.MustElement(`body > footer`).MustWaitVisible() // find and click "Expand All" link - page.MustElement(`#pkg-examples > div`).MustClick() + page.MustElement(`#pkg-examples`).MustClick() // retrieve the value of the textarea - example := page.MustElement(`#example_After .play .input textarea`).MustText() + example := page.MustElement(`#example-After textarea`).MustText() log.Printf("Go's time.After example:\n%s", example) } diff --git a/lib/examples/compare-chromedp/eval/main.go b/lib/examples/compare-chromedp/eval/main.go index 4cf8e35d..63ff935c 100644 --- a/lib/examples/compare-chromedp/eval/main.go +++ b/lib/examples/compare-chromedp/eval/main.go @@ -12,7 +12,7 @@ import ( func main() { res := rod.New().MustConnect(). MustPage("https://www.google.com/"). - MustElement("#main"). + MustElement(`input`). MustEval("() => Object.keys(window)") log.Printf("window object keys: %v", res) diff --git a/lib/examples/compare-chromedp/remote/main.go b/lib/examples/compare-chromedp/remote/main.go index c0bbb565..4719362f 100644 --- a/lib/examples/compare-chromedp/remote/main.go +++ b/lib/examples/compare-chromedp/remote/main.go @@ -7,7 +7,7 @@ import ( "github.com/go-rod/rod" ) -var flagDevToolWsURL = flag.String("devtools-ws-url", "", "DevTools WebSsocket URL") +var flagDevToolWsURL = flag.String("devtools-ws-url", "", "DevTools WebSocket URL") // This example demonstrates how to connect to an existing Chrome DevTools // instance using a remote WebSocket URL. diff --git a/lib/examples/compare-chromedp/screenshot/main.go b/lib/examples/compare-chromedp/screenshot/main.go index 67208b4a..b09412b1 100644 --- a/lib/examples/compare-chromedp/screenshot/main.go +++ b/lib/examples/compare-chromedp/screenshot/main.go @@ -15,7 +15,7 @@ func main() { browser := rod.New().MustConnect() // capture screenshot of an element - browser.MustPage("https://google.com").MustElement("#main").MustScreenshot("elementScreenshot.png") + browser.MustPage("https://google.com").MustElement("body div").MustScreenshot("elementScreenshot.png") // capture entire browser viewport, returning jpg with quality=90 buf, err := browser.MustPage("https://brank.as/").Screenshot(true, &proto.PageCaptureScreenshot{ diff --git a/lib/examples/compare-chromedp/submit/main.go b/lib/examples/compare-chromedp/submit/main.go index 120e2203..aaec180b 100644 --- a/lib/examples/compare-chromedp/submit/main.go +++ b/lib/examples/compare-chromedp/submit/main.go @@ -14,7 +14,7 @@ func main() { page.MustElement(`input[name=q]`).MustWaitVisible().MustInput("chromedp").MustType(input.Enter) - res := page.MustElementR("a", "chromedp").MustParent().MustNext().MustText() + res := page.MustElementR("a", "chromedp").MustParent().MustParent().MustNext().MustText() log.Printf("got: `%s`", strings.TrimSpace(res)) } diff --git a/lib/examples/compare-chromedp/text/main.go b/lib/examples/compare-chromedp/text/main.go index 4e559f19..41708d45 100644 --- a/lib/examples/compare-chromedp/text/main.go +++ b/lib/examples/compare-chromedp/text/main.go @@ -9,8 +9,8 @@ import ( // This example demonstrates how to extract text from a specific element. func main() { - page := rod.New().MustConnect().MustPage("https://golang.org/pkg/time") + page := rod.New().MustConnect().MustPage("https://pkg.go.dev/time") - res := page.MustElement("#pkg-overview").MustText() + res := page.MustElement("#pkg-overview").MustParent().MustText() log.Println(strings.TrimSpace(res)) } diff --git a/lib/examples/translator/main.go b/lib/examples/translator/main.go index 3ca7d8a4..d2fc806d 100644 --- a/lib/examples/translator/main.go +++ b/lib/examples/translator/main.go @@ -1,19 +1,26 @@ package main import ( + "flag" "fmt" - "os" + "log" + "strings" "github.com/go-rod/rod" ) func main() { - // get the first commandline argument - source := os.Args[1] + flag.Parse() + + // get the commandline arguments + source := strings.TrimSpace(strings.Join(flag.Args(), " ")) + if source == "" { + log.Fatal("usage: go run main.go -- 'This is the phrase to translate to Spanish.'") + } browser := rod.New().MustConnect() - page := browser.MustPage("https://translate.google.com") + page := browser.MustPage("https://translate.google.com/?sl=auto&tl=es&op=translate") el := page.MustElement(`textarea[aria-label="Source text"]`) diff --git a/lib/js/helper.js b/lib/js/helper.js index 50fc3051..049ae27d 100644 --- a/lib/js/helper.js +++ b/lib/js/helper.js @@ -1,8 +1,8 @@ // The reason to use an extra js file to hold the functions is the lint and IDE support. // To debug just add "debugger" keyword to the line you want to pause, then run something like: // -// go run ./lib/js/generate -// rod=show,devtools go test -run ^TestClick$ +// go run ./lib/js/generate/main.go +// go test -run ^TestClick$ -- -rod=show,devtools const functions = { element(selector) {