Skip to content

Commit 274da7a

Browse files
committed
cmd/screentest: doc headless-shell; improve output
- Document the preference for headless-shell, the stripped-down Chrome binary. It is more reliable. - Clarify output, reduce vertical whitespace. - Fix race condition. Change-Id: I8d879bf4442a089e1b3eeb47074c76fe46e87a53 Reviewed-on: https://go-review.googlesource.com/c/website/+/628056 Reviewed-by: Hyang-Ah Hana Kim <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 1e9999a commit 274da7a

File tree

6 files changed

+220
-61
lines changed

6 files changed

+220
-61
lines changed

cmd/golangorg/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ The go.dev web site has a suite of visual checks that can be run with:
3131
./cmd/golangorg/screentest.sh
3232

3333
These checks can be run locally and will generate visual diffs of web pages
34-
from the set of testcases in `cmd/golangorg/testdata/screentest/*.txt`, comparing screenshots
34+
from the set of test cases in `cmd/golangorg/testdata/screentest/*.txt`, comparing screenshots
3535
of the live server and a locally running instance of cmd/golangorg.
36+
Screentest will start Chrome locally to render the pages, but that can be unreliable.
37+
Prefer Chrome headless-shell. See the documentation at the top of cmd/screenshot/main.go
38+
for more.
3639

3740
## Deploying to go.dev and golang.org
3841

cmd/golangorg/testdata/screentest/godev.txt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ windowsize 1536x960
22

33
test homepage
44
path /
5+
# Wait for the playground to run the sample program.
6+
sleep 4s
57
capture fullscreen
68
capture fullscreen 540x1080
79

810
test why go case studies
911
path /solutions/case-studies
12+
# Scrolling to bottom causes lazy-loading images to load.
13+
eval window.scrollTo({top: document.body.scrollHeight});
14+
sleep 1s
1015
capture fullscreen
1116
capture fullscreen 540x1080
1217

@@ -15,10 +20,14 @@ path /solutions/use-cases
1520
capture fullscreen
1621
capture fullscreen 540x1080
1722

18-
test getting started
19-
path /learn/
20-
capture fullscreen
21-
capture fullscreen 540x1080
23+
# This test will fail because the local server
24+
# uses fake download information, so the download button
25+
# will have a different Go version.
26+
#
27+
# test getting started
28+
# path /learn/
29+
# capture fullscreen
30+
# capture fullscreen 540x1080
2231

2332
test docs
2433
path /doc/

cmd/screentest/main.go

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,45 @@ can be slash-separated file paths (even on Windows).
1919
2020
The flags are:
2121
22-
-c
23-
Number of test cases to run concurrently.
22+
-c
23+
Number of test cases to run concurrently.
2424
-d
25-
URL of a Chrome websocket debugger. If omitted, screentest tries to find the
26-
Chrome executable on the system and starts a new instance.
25+
URL of a Chrome websocket debugger. If omitted, screentest uses the
26+
Chrome executable on the command path. It will look first for the
27+
headless-shell binary, which is preferred.
2728
-headers
28-
HTTP(S) headers to send with each request, as a comma-separated list of name:value.
29+
HTTP(S) headers to send with each request, as a comma-separated list of name:value.
2930
-run REGEXP
30-
Run only tests matching regexp.
31-
-o
32-
URL or slash-separated path for output files. If omitted, files are written
33-
to a subdirectory of the user's cache directory. Each test file is given
34-
its own directory, so test names in two files can be identical. But the directory
35-
name is the basename of the test file with the extension removed. Conflicting
36-
file names will overwrite each other.
37-
-u
38-
Instead of comparing screenshots, use the test screenshots to update the
39-
want screenshots. This only makes sense if wantURL is a storage location
40-
like a file path or GCS bucket.
41-
-v
42-
Variables provided to script templates as comma separated KEY:VALUE pairs.
31+
Run only tests matching regexp.
32+
-o
33+
URL or slash-separated path where output files for failing tests are written.
34+
If omitted, files are written to a subdirectory of the user's cache directory.
35+
At the start of each run, existing files are removed.
36+
Each test file is given its own directory, so test names in two files can be identical,
37+
but the directory name is the basename of the test file with the extension removed, so
38+
files with identical basenames will overwrite each other.
39+
-u
40+
Instead of comparing screenshots, use the test screenshots to update the
41+
want screenshots. This only makes sense if wantURL is a storage location
42+
like a file path or GCS bucket.
43+
-v
44+
Variables provided to script templates as comma-separated KEY:VALUE pairs.
45+
46+
# Headless Chrome
47+
48+
Screentest needs a headless Chrome process to render web pages. Although it can use a full
49+
Chrome browser, we have found the headless-shell build of Chrome to be more reliable.
50+
Install headless-shell on your local machine with this command:
51+
52+
npx @puppeteer/browsers install chrome-headless-shell@VERSION
53+
54+
Put the binary on your path and screentest will find it. Omit the -d flag in this case.
55+
56+
You can also run headless-shell in docker. We use this command:
57+
58+
docker run --detach --rm --network host --shm-size 8G --name headless-shell chromedp/headless-shell:VERSION
59+
60+
Then pass "-d ws://localhost:9222" to screentest.
4361
4462
# Scripts
4563
@@ -105,6 +123,11 @@ some other way.
105123
eval 'document.querySelector(".selector").remove();'
106124
eval 'window.scrollTo({top: 0});'
107125
126+
Use sleep DURATION to pause the browser for the duration. This is a last resort
127+
for deflaking; prefer to wait for an element.
128+
129+
sleep 50ms
130+
108131
Use capture [SIZE] [ARG] to create a test case with the properties
109132
defined in the test case. If present, the first argument to capture must be one of
110133
'fullscreen', 'viewport' or 'element'. The optional second argument provides
@@ -169,6 +192,8 @@ type options struct {
169192
}
170193

171194
func main() {
195+
log.SetFlags(0)
196+
log.SetPrefix("screentest: ")
172197
flag.Usage = func() {
173198
fmt.Printf("usage: screentest [flags] testURL wantURL path ...\n")
174199
fmt.Printf("\ttestURL is the URL or file path to be tested\n")
@@ -184,5 +209,7 @@ func main() {
184209
}
185210
if err := run(context.Background(), flag.Arg(0), flag.Arg(1), flag.Args()[2:], flags); err != nil {
186211
log.Fatal(err)
212+
} else {
213+
log.Print("PASS")
187214
}
188215
}

0 commit comments

Comments
 (0)