Skip to content

Commit

Permalink
Update examples (#627)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
gmlewis committed Jun 16, 2022
1 parent aef98d8 commit 9aab67b
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 20 deletions.
8 changes: 7 additions & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion lib/assets/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Asserts
# Assets

Static files for the project
6 changes: 3 additions & 3 deletions lib/examples/compare-chromedp/click/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion lib/examples/compare-chromedp/eval/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/examples/compare-chromedp/remote/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/examples/compare-chromedp/screenshot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion lib/examples/compare-chromedp/submit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
4 changes: 2 additions & 2 deletions lib/examples/compare-chromedp/text/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
15 changes: 11 additions & 4 deletions lib/examples/translator/main.go
Original file line number Diff line number Diff line change
@@ -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"]`)

Expand Down
4 changes: 2 additions & 2 deletions lib/js/helper.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit 9aab67b

Please sign in to comment.