Skip to content

Commit

Permalink
better test example
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Nov 9, 2022
1 parent 11886f2 commit 7a080b1
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ First, set this environment variable:
export GODEBUG="tracebackancestors=1000"
```

Then, no magic, just `go test`. Read the test template [lab_test.go](../lab_test.go) to get started.
Then, no magic, just `go test`. Read the test template [rod_test.go](../rod_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
8 changes: 4 additions & 4 deletions .github/ISSUE_TEMPLATE/question.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ Rod Version: v0.0.0
cd rod
```

1. Use your code to replace the content of function `TestLab` in file `lab_test.go`.
1. Use your code to replace the content of function `TestRod` in file `rod_test.go`.

1. Test your code with: `go test -run TestLab`, make sure it fails as expected.
1. Test your code with: `go test -run TestRod`, make sure it fails as expected.

1. Replace ALL THE CONTENT under "The code to demonstrate your question" with your `TestLab` function, like below:
1. Replace ALL THE CONTENT under "The code to demonstrate your question" with your `TestRod` function, like below:

```go
func TestLab(t *testing.T) {
func TestRod(t *testing.T) {
g := setup(t)
g.Eq(1, 2) // the test should fail, here 1 doesn't equal 2
}
Expand Down
20 changes: 0 additions & 20 deletions lab_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion lib/utils/check-issue/body-invalid.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test
```

```go
func TestLab(t *testing.T) {
func TestRod(t *testing.T) {
g := setup(t)
g.Eq(1, 1)

Expand Down
8 changes: 4 additions & 4 deletions lib/utils/check-issue/body.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ Rod Version: v1.0.0
cd rod
```

1. Use your code to replace the content of function `TestLab` in file `lab_test.go`.
1. Use your code to replace the content of function `TestRod` in file `rod_test.go`.

1. Test your code with: `go test -run TestLab`.
1. Test your code with: `go test -run TestRod`.

1. Replace this section with your entire `lab_test.go` content, like below:
1. Replace this section with your entire `rod_test.go` content, like below:

```go
func TestLab(t *testing.T) {
func TestRod(t *testing.T) {
g := setup(t)
g.Eq(1, 1)
}
Expand Down
26 changes: 26 additions & 0 deletions rod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package rod_test

import (
"testing"
)

// This is the template to demonstrate how to test Rod.
func TestRod(t *testing.T) {
g := setup(t)
g.cancelTimeout() // Cancel timeout protection

// You can use the pre-launched g.browser or g.page for testing
browser, page := g.browser, g.page

// You can also use the g.html to serve static html content
page.MustNavigate(g.html(doc)).MustWaitLoad()

g.Eq(browser.MustVersion().ProtocolVersion, "1.3")
g.Has(page.MustElement("body").MustText(), "ok")
}

const doc = `
<html>
<body>ok</body>
</html>
`
4 changes: 4 additions & 0 deletions setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ func (g G) blank() string {
return g.srcFile("./fixtures/blank.html")
}

func (g G) html(content string) string {
return g.Serve().Route("/", "", content).URL()
}

// Get abs file path from fixtures folder, such as "file:///a/b/click.html".
// Usually the path can be used for html src attribute like:
//
Expand Down

0 comments on commit 7a080b1

Please sign in to comment.