internal/screentest: add request blocking keyword to scripts

Scripts can use the block keyword to set URL patterns to block.
Wildcards ('*') are allowed.

While loading a webpage if chrome attempts to load a resource
from a blocked URL the request will fail. This is useful for
blocking things like dynamic badge images from a code coverage
or build indicator service.

Change-Id: Ifbde82d56918928333836b493e993c1ef1054a76
Reviewed-on: https://go-review.googlesource.com/c/website/+/381335
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Trust: Jamal Carvalho <jamalcarvalho@google.com>
Run-TryBot: Jamal Carvalho <jamalcarvalho@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Jamal Carvalho 2022-01-27 17:58:47 +00:00 коммит произвёл Jamal Carvalho
Родитель 3ff88697a8
Коммит 55fb4e21dc
1 изменённых файлов: 17 добавлений и 5 удалений

Просмотреть файл

@ -42,6 +42,10 @@
//
// compare https://go.dev::cache http://localhost:6060
//
// Use block URL ... to set URL patterns to block. Wildcards ('*') are allowed.
//
// block https://codecov.io/* https://travis-ci.com/*
//
// Use output DIRECTORY to set the output directory for diffs and cached images.
//
// output testdata/snapshots
@ -385,6 +389,7 @@ type testcase struct {
viewportHeight int
screenshotType screenshotType
screenshotElement string
blockedURLs []string
output bytes.Buffer
}
@ -412,6 +417,7 @@ func readTests(file string, vars map[string]string) ([]*testcase, error) {
gcsBucket bool
width, height int
lineNo int
blockedURLs []string
)
cache, err := os.UserCacheDir()
if err != nil {
@ -506,6 +512,8 @@ func readTests(file string, vars map[string]string) ([]*testcase, error) {
tasks = append(tasks, chromedp.WaitReady(args))
case "EVAL":
tasks = append(tasks, chromedp.Evaluate(args, nil))
case "BLOCK":
blockedURLs = append(blockedURLs, strings.Fields(args)...)
case "CAPTURE":
if originA == "" || originB == "" {
return nil, fmt.Errorf("missing compare for capture on line %d", lineNo)
@ -522,11 +530,12 @@ func readTests(file string, vars map[string]string) ([]*testcase, error) {
return nil, fmt.Errorf("url.Parse(%q): %w", originB+pathname, err)
}
test := &testcase{
name: testName,
tasks: tasks,
urlA: urlA.String(),
urlB: urlB.String(),
headers: headers,
name: testName,
tasks: tasks,
urlA: urlA.String(),
urlB: urlB.String(),
headers: headers,
blockedURLs: blockedURLs,
// Default to viewportScreenshot
screenshotType: viewportScreenshot,
viewportWidth: width,
@ -739,6 +748,9 @@ func (tc *testcase) captureScreenshot(ctx context.Context, url string) ([]byte,
if tc.headers != nil {
tasks = append(tasks, network.SetExtraHTTPHeaders(tc.headers))
}
if tc.blockedURLs != nil {
tasks = append(tasks, network.SetBlockedURLS(tc.blockedURLs))
}
tasks = append(tasks,
chromedp.EmulateViewport(int64(tc.viewportWidth), int64(tc.viewportHeight)),
chromedp.Navigate(url),