internal/screentest: check http status before page tasks

When a page request returned a 404 unexpectedly
it would cause a testcase that waited for a css
selector to timeout instead of reporting the
test failure for mismatched status. This change
adds the status check as a task instead of a post
test check to report the failure after navigating
to a page.

Change-Id: I31c1b5dab6a412558c501d99873bff91d8dbc105
Reviewed-on: https://go-review.googlesource.com/c/website/+/409615
Reviewed-by: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Jamal Carvalho <jamal@golang.org>
Auto-Submit: Jamal Carvalho <jamal@golang.org>
This commit is contained in:
Jamal Carvalho 2022-06-01 15:09:43 +00:00 коммит произвёл Gopher Robot
Родитель ec0d7080e7
Коммит 17dd0dec33
2 изменённых файлов: 16 добавлений и 4 удалений

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

@ -777,6 +777,7 @@ func (tc *testcase) captureScreenshot(ctx context.Context, url string) ([]byte,
chromedp.Navigate(url),
waitForEvent("networkIdle"),
reduceMotion(),
checkResponse(tc, &res),
tc.tasks,
)
switch tc.screenshotType {
@ -790,10 +791,6 @@ func (tc *testcase) captureScreenshot(ctx context.Context, url string) ([]byte,
if err := chromedp.Run(ctx, tasks); err != nil {
return nil, fmt.Errorf("chromedp.Run(...): %w", err)
}
if res.Status != tc.status {
fmt.Fprintf(&tc.output, "\nFAIL http status mismatch: got %d; want %d", res.Status, tc.status)
return nil, fmt.Errorf("bad status: %d", res.Status)
}
return buf, nil
}
@ -912,6 +909,16 @@ func getResponse(u string, res *Response) chromedp.ActionFunc {
}
}
func checkResponse(tc *testcase, res *Response) chromedp.ActionFunc {
return func(context.Context) error {
if res.Status != tc.status {
fmt.Fprintf(&tc.output, "\nFAIL http status mismatch: got %d; want %d", res.Status, tc.status)
return fmt.Errorf("bad status: %d", res.Status)
}
return nil
}
}
// runConcurrently calls f on each integer from 0 to n-1,
// with at most max invocations active at once.
// It waits for all invocations to complete.

5
internal/screentest/testdata/fail.txt поставляемый
Просмотреть файл

@ -3,3 +3,8 @@ compare https://go.dev https://pkg.go.dev
test homepage
pathname /
capture viewport
test 404 missing selector
pathname /404
wait [role='treeitem'][aria-selected='true']
capture viewport