| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package browsertests |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"fmt" |
| 20 |
"os/exec" |
| 21 |
"regexp" |
| 22 |
"runtime" |
| 23 |
"strings" |
| 24 |
"testing" |
| 25 |
"time" |
| 26 |
|
| 27 |
_ "embed" |
| 28 |
|
| 29 |
"github.com/chromedp/chromedp" |
| 30 |
) |
| 31 |
|
| 32 |
func maybeSkipBrowserTest(t *testing.T) { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" { |
| 37 |
t.Skip("This test only works on x86-64 Linux") |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
if _, err := exec.LookPath("google-chrome"); err == nil { |
| 42 |
return |
| 43 |
} |
| 44 |
if _, err := exec.LookPath("chrome"); err == nil { |
| 45 |
return |
| 46 |
} |
| 47 |
t.Skip("chrome not available") |
| 48 |
} |
| 49 |
|
| 50 |
func TestTopTable(t *testing.T) { |
| 51 |
maybeSkipBrowserTest(t) |
| 52 |
|
| 53 |
prof := makeFakeProfile() |
| 54 |
server := makeTestServer(t, prof) |
| 55 |
ctx := newContext(context.Background(), t) |
| 56 |
|
| 57 |
err := chromedp.Run(ctx, |
| 58 |
chromedp.Navigate(server.URL+"/top"), |
| 59 |
chromedp.WaitVisible(`#toptable`, chromedp.ByID), |
| 60 |
|
| 61 |
|
| 62 |
matchRegexp(t, "#node0", `200ms.*F2`), |
| 63 |
matchInOrder(t, "#toptable", "F2", "F3", "F1"), |
| 64 |
|
| 65 |
|
| 66 |
chromedp.Click(`#cumhdr1`, chromedp.ByID), |
| 67 |
matchInOrder(t, "#toptable", "F1", "F2", "F3"), |
| 68 |
) |
| 69 |
if err != nil { |
| 70 |
t.Fatal(err) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func TestFlameGraph(t *testing.T) { |
| 75 |
maybeSkipBrowserTest(t) |
| 76 |
|
| 77 |
prof := makeFakeProfile() |
| 78 |
server := makeTestServer(t, prof) |
| 79 |
ctx := newContext(context.Background(), t) |
| 80 |
|
| 81 |
var ignored []byte |
| 82 |
err := chromedp.Run(ctx, |
| 83 |
chromedp.Navigate(server.URL), |
| 84 |
chromedp.Evaluate(jsTestFixture, &ignored), |
| 85 |
eval(t, jsCheckFlame), |
| 86 |
) |
| 87 |
if err != nil { |
| 88 |
t.Fatal(err) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
var jsCheckFlame string |
| 94 |
|
| 95 |
func TestSource(t *testing.T) { |
| 96 |
maybeSkipBrowserTest(t) |
| 97 |
|
| 98 |
prof := makeFakeProfile() |
| 99 |
server := makeTestServer(t, prof) |
| 100 |
ctx := newContext(context.Background(), t) |
| 101 |
|
| 102 |
err := chromedp.Run(ctx, |
| 103 |
chromedp.Navigate(server.URL+"/source?f=F3"), |
| 104 |
chromedp.WaitVisible(`#content`, chromedp.ByID), |
| 105 |
matchRegexp(t, "#content", `F3`), |
| 106 |
matchRegexp(t, "#content", `Total:.*100ms`), |
| 107 |
matchRegexp(t, "#content", `\b22\b.*100ms`), |
| 108 |
) |
| 109 |
if err != nil { |
| 110 |
t.Fatal(err) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
func newContext(ctx context.Context, t *testing.T) context.Context { |
| 115 |
opts := append(chromedp.DefaultExecAllocatorOptions[:], |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
chromedp.NoSandbox, |
| 121 |
) |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
const browserDeadline = time.Second * 90 |
| 126 |
|
| 127 |
ctx, cancel := chromedp.NewExecAllocator(ctx, opts...) |
| 128 |
t.Cleanup(cancel) |
| 129 |
ctx, cancel = context.WithTimeout(ctx, browserDeadline) |
| 130 |
t.Cleanup(cancel) |
| 131 |
ctx, cancel = chromedp.NewContext(ctx) |
| 132 |
t.Cleanup(cancel) |
| 133 |
return ctx |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
func matchRegexp(t *testing.T, query, re string) chromedp.ActionFunc { |
| 139 |
return func(ctx context.Context) error { |
| 140 |
var value string |
| 141 |
err := chromedp.Text(query, &value, chromedp.ByQuery).Do(ctx) |
| 142 |
if err != nil { |
| 143 |
return fmt.Errorf("text %s: %v", query, err) |
| 144 |
} |
| 145 |
t.Logf("text %s:\n%s", query, value) |
| 146 |
m, err := regexp.MatchString(re, value) |
| 147 |
if err != nil { |
| 148 |
return err |
| 149 |
} |
| 150 |
if !m { |
| 151 |
return fmt.Errorf("%s: did not find %q in\n%s", query, re, value) |
| 152 |
} |
| 153 |
return nil |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
func matchInOrder(t *testing.T, query string, sequence ...string) chromedp.ActionFunc { |
| 161 |
return func(ctx context.Context) error { |
| 162 |
var value string |
| 163 |
err := chromedp.Text(query, &value, chromedp.ByQuery).Do(ctx) |
| 164 |
if err != nil { |
| 165 |
return fmt.Errorf("text %s: %v", query, err) |
| 166 |
} |
| 167 |
t.Logf("text %s:\n%s", query, value) |
| 168 |
remaining := value |
| 169 |
for _, s := range sequence { |
| 170 |
pos := strings.Index(remaining, s) |
| 171 |
if pos < 0 { |
| 172 |
return fmt.Errorf("%s: did not find %q in expected order %v in\n%s", query, s, sequence, value) |
| 173 |
} |
| 174 |
remaining = remaining[pos+len(s):] |
| 175 |
} |
| 176 |
return nil |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
func eval(t *testing.T, js string) chromedp.ActionFunc { |
| 184 |
return func(ctx context.Context) error { |
| 185 |
var result [][]any |
| 186 |
err := chromedp.Evaluate(js, &result).Do(ctx) |
| 187 |
if err != nil { |
| 188 |
return err |
| 189 |
} |
| 190 |
for _, s := range result { |
| 191 |
if len(s) > 0 && s[0] == "LOG" { |
| 192 |
t.Log(s[1:]...) |
| 193 |
} else if len(s) > 0 && s[0] == "ERROR" { |
| 194 |
t.Error(s[1:]...) |
| 195 |
} else { |
| 196 |
t.Error(s...) |
| 197 |
} |
| 198 |
} |
| 199 |
return nil |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
var jsTestFixture string |