Skip to content

Commit

Permalink
Refactor TestNavigationSpanCreation
Browse files Browse the repository at this point in the history
Adding better ways of waiting for API to end with the use of waitUntil
and waitForNavigation. Now comparing the elements within the slices
and not worrying about the order since the order can change on every
test run.
  • Loading branch information
ankur22 committed Sep 12, 2024
1 parent bf1dcbe commit 0f96ee8
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions tests/tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func TestNavigationSpanCreation(t *testing.T) {
name: "goto",
js: fmt.Sprintf(`
page = await browser.newPage();
await page.goto('%s');
await page.goto('%s', {waitUntil:'networkidle'});
page.close();
`, ts.URL),
expected: []string{
Expand All @@ -219,8 +219,8 @@ func TestNavigationSpanCreation(t *testing.T) {
name: "reload",
js: fmt.Sprintf(`
page = await browser.newPage();
await page.goto('%s');
await page.reload();
await page.goto('%s', {waitUntil:'networkidle'});
await page.reload({waitUntil:'networkidle'});
page.close();
`, ts.URL),
expected: []string{
Expand All @@ -240,8 +240,11 @@ func TestNavigationSpanCreation(t *testing.T) {
name: "go_back",
js: fmt.Sprintf(`
page = await browser.newPage();
await page.goto('%s');
await page.evaluate(() => window.history.back());
await page.goto('%s', {waitUntil:'networkidle'});
await Promise.all([
page.waitForNavigation(),
page.evaluate(() => window.history.back()),
]);
page.close();
`, ts.URL),
expected: []string{
Expand All @@ -252,6 +255,7 @@ func TestNavigationSpanCreation(t *testing.T) {
"navigation", // created when a new page is created
"page.goto",
"navigation", // created when a navigation occurs after goto
"page.waitForNavigation",
"navigation", // created when going back to the previous page
"page.close",
},
Expand All @@ -260,8 +264,11 @@ func TestNavigationSpanCreation(t *testing.T) {
name: "same_page_navigation",
js: fmt.Sprintf(`
page = await browser.newPage();
await page.goto('%s');
await page.locator('a[id=\"top\"]').click();
await page.goto('%s', {waitUntil:'networkidle'});
await Promise.all([
page.waitForNavigation(),
page.locator('a[id=\"top\"]').click(),
]);
page.close();
`, ts.URL),
expected: []string{
Expand All @@ -272,6 +279,7 @@ func TestNavigationSpanCreation(t *testing.T) {
"navigation", // created when a new page is created
"page.goto",
"navigation", // created when a navigation occurs after goto
"page.waitForNavigation",
"locator.click",
"navigation", // created when navigating within the same page
"page.close",
Expand All @@ -291,7 +299,10 @@ func TestNavigationSpanCreation(t *testing.T) {
assertJSInEventLoop(t, vu, tc.js)

got := tracer.getOrderedSpan()
assert.Equal(t, tc.expected, got, fmt.Sprintf("%s failed", tc.name))
// We can't use assert.Equal since the order of the span creation
// changes slightly on every test run. Instead we're going to make
// sure that the slice matches but not the order.
assert.ElementsMatch(t, tc.expected, got, fmt.Sprintf("%s failed", tc.name))
}()
}
}
Expand Down

0 comments on commit 0f96ee8

Please sign in to comment.