Skip to content

Commit 4d43c4a

Browse files
committed
New accounts per browser-country combination
I want to add a test for the onboarding flow. However, the functional tests used to create a new account per country, while re-using those accounts in every browser. This meant that as the onboarding test ran in one browser first, the other browsers would fail because that account would no longer be onboarded. With this change, the tests create a new account for every country *and* browser combination.
1 parent 89eee41 commit 4d43c4a

File tree

7 files changed

+56
-56
lines changed

7 files changed

+56
-56
lines changed

functional-tests/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Before running the tests the `global-setup` script:
4848

4949
- Fetches feature flags from `/api/v1/admin/feature-flags` and saves them to `./functional-test-cache/enabled-feature-flags.json`
5050
- Signs up a unique test user account per country
51-
- Stores the login state per user in `./functional-test-cache/user-session-<country>.json`
51+
- Stores the login state per user in `./functional-test-cache/user-session-<browser+location>.json`
5252
- Stores the emails per user in `./functional-test-cache/user-emails.json`
5353

5454
## Artifacts

functional-tests/fixtures/authenticatedTest.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ const test = baseTest.extend<object, { storageState?: string }>({
1515
return;
1616
}
1717

18-
const storagePath = getTestUserSessionFilePath(countryCode);
18+
const storagePath = getTestUserSessionFilePath(testInfo.project.name);
1919
const exists = fs.existsSync(storagePath);
2020
if (exists) {
2121
await use(storagePath);
2222
} else {
23-
console.warn(`No user session file found for [${countryCode}]`);
23+
console.warn(
24+
`No user session file found for [${testInfo.project.name}]`,
25+
);
2426
await use(undefined);
2527
}
2628
},

functional-tests/playwright.config.ts

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,28 @@ export const getEnabledFeatureFlags = () => {
9797
return enabledFeatureFlags;
9898
};
9999

100-
const projects: Project[] = locations.flatMap((geo) =>
101-
baseDevices.map((base) => ({
102-
name: `${base.name} (${geo.name})`,
103-
use: {
104-
...base.use,
105-
countryCode: geo.name.toLowerCase(),
106-
geolocation: geo.geolocation,
107-
locale: geo.locale,
108-
permissions: ["geolocation"],
109-
enabledFeatureFlags: getEnabledFeatureFlags(),
110-
extraHTTPHeaders: {
111-
"Accept-Language": `${geo.locale},${geo.name.toLowerCase()};q=1.0`,
112-
"X-Client-Region": geo.name.toLowerCase(),
113-
"x-forced-client-region-token": createTestClientRegionToken(
114-
geo.name.toLowerCase(),
115-
),
116-
},
117-
},
118-
})),
100+
export const projects = locations.flatMap((geo) =>
101+
baseDevices.map(
102+
(base) =>
103+
({
104+
name: `${base.name} (${geo.name})`,
105+
use: {
106+
...base.use,
107+
countryCode: geo.name.toLowerCase(),
108+
geolocation: geo.geolocation,
109+
locale: geo.locale,
110+
permissions: ["geolocation"],
111+
enabledFeatureFlags: getEnabledFeatureFlags(),
112+
extraHTTPHeaders: {
113+
"Accept-Language": `${geo.locale},${geo.name.toLowerCase()};q=1.0`,
114+
"X-Client-Region": geo.name.toLowerCase(),
115+
"x-forced-client-region-token": createTestClientRegionToken(
116+
geo.name.toLowerCase(),
117+
),
118+
},
119+
},
120+
}) as const satisfies Project,
121+
),
119122
);
120123

121124
export default defineConfig({
@@ -163,10 +166,13 @@ export default defineConfig({
163166
testMatch: /global\-setup/,
164167
teardown: "global-teardown",
165168
},
166-
...projects.map((project) => ({
167-
...project,
168-
dependencies: ["global-setup"],
169-
})),
169+
...projects.map(
170+
(project) =>
171+
({
172+
...project,
173+
dependencies: ["global-setup"],
174+
}) as Project,
175+
),
170176
{
171177
name: "global-teardown",
172178
testMatch: /global\-teardown/,

functional-tests/tests/auth.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ test.describe(`Verify authentication [${process.env.E2E_TEST_ENV}]`, () => {
2222
countryCode: testInfo.project.use.countryCode,
2323
isMobile: testInfo.project.use.isMobile,
2424
});
25-
const userEmail = getTestUserEmailByCountryCode(
26-
testInfo.project.use.countryCode,
27-
);
25+
const userEmail = getTestUserEmailByCountryCode(testInfo.project);
2826
await signInUser(
2927
page,
3028
userEmail,

functional-tests/tests/global-setup.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
getBaseTestEnvUrl,
1212
} from "../utils/environment";
1313
import { goToFxA, signUpUser } from "../utils/fxa";
14-
import { locations } from "../playwright.config";
14+
import { getTestUserSessionFilePath } from "../utils/user";
15+
import { projects } from "../playwright.config";
1516

1617
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1718

@@ -41,22 +42,22 @@ async function setupFeatureFlags() {
4142
async function setupUserAccount(browser: Browser) {
4243
const emails: Record<string, string> = {};
4344

44-
for (const location of locations) {
45-
const countryCode = location.name.toLowerCase();
45+
for (const project of projects) {
46+
const projectName = project.name!;
4647
const context = await browser.newContext({
47-
geolocation: location.geolocation,
48-
locale: location.locale,
48+
geolocation: project.use.geolocation,
49+
locale: project.use.locale,
4950
permissions: ["geolocation"],
5051
});
5152
const page = await context.newPage();
5253

5354
// Sign up flow
5455
const timestamp = Date.now();
55-
const email = `${process.env.E2E_TEST_ACCOUNT_BASE_EMAIL}_${countryCode}_${timestamp}@restmail.net`;
56-
emails[countryCode] = email;
56+
const email = `${process.env.E2E_TEST_ACCOUNT_BASE_EMAIL}_${projectName.toLowerCase().replaceAll(" ", "-").replaceAll("(", "").replaceAll(")", "")}_${timestamp}@restmail.net`;
57+
emails[projectName] = email;
5758

5859
await goToFxA(page, {
59-
countryCode,
60+
countryCode: project.use.countryCode,
6061
isMobile: false,
6162
});
6263
await signUpUser(
@@ -69,11 +70,9 @@ async function setupUserAccount(browser: Browser) {
6970
await page.waitForURL("**/user/**");
7071

7172
// Store test user session
73+
const storageStatePath = getTestUserSessionFilePath(project.name);
7274
await context.storageState({
73-
path: path.resolve(
74-
__dirname,
75-
`./functional-test-cache/user-session-${countryCode}.json`,
76-
),
75+
path: storageStatePath,
7776
});
7877

7978
await context.close();

functional-tests/tests/global-teardown.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import fs from "fs";
77
import path from "path";
88
import { fileURLToPath } from "url";
99
import { test as teardown } from "../fixtures/authenticatedTest";
10-
import { getTestUserEmails, getTestUserSessionFilePath } from "../utils/user";
10+
import { getTestUserSessionFilePath } from "../utils/user";
1111
import { getBaseTestEnvUrl } from "../utils/environment";
12+
import { projects } from "../playwright.config";
1213

1314
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1415

@@ -22,10 +23,8 @@ function removeTestStorage() {
2223
}
2324

2425
async function deleteTestUserAccounts(browser: Browser) {
25-
const userEmails = getTestUserEmails();
26-
27-
for (const userCountryCode in userEmails) {
28-
const storageState = getTestUserSessionFilePath(userCountryCode);
26+
for (const project of projects) {
27+
const storageState = getTestUserSessionFilePath(project.name);
2928
const context = await browser.newContext({ storageState });
3029
const page = await context.newPage();
3130

functional-tests/utils/user.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
import { Page, request } from "@playwright/test";
5+
import { FullProject, Page, request } from "@playwright/test";
66
import fs from "fs";
77
import path from "path";
88
import { fileURLToPath } from "url";
@@ -74,25 +74,21 @@ export function getTestUserEmails(): Record<string, string> {
7474
return emails;
7575
}
7676

77-
export function getTestUserEmailByCountryCode(countryCode?: string): string {
77+
export function getTestUserEmailByCountryCode(project: FullProject): string {
7878
const emails = getTestUserEmails();
79-
const email = emails[countryCode ?? "us"];
79+
const email = emails[project.name];
8080
if (!email) {
81-
throw new Error(`No email found for [${countryCode}]`);
81+
throw new Error(`No email found for [${project.name}]`);
8282
}
8383

8484
return email;
8585
}
8686

87-
export function getTestUserSessionFilePath(countryCode: string) {
87+
export function getTestUserSessionFilePath(projectName: string) {
8888
const storagePath = path.resolve(
8989
__dirname,
90-
`../functional-test-cache/user-session-${countryCode}.json`,
90+
`../functional-test-cache/user-session-${projectName.toLowerCase().replaceAll(" ", "-").replaceAll("(", "").replaceAll(")", "")}.json`,
9191
);
9292

93-
if (!fs.existsSync(storagePath)) {
94-
throw new Error("User session storage not found");
95-
}
96-
9793
return storagePath;
9894
}

0 commit comments

Comments
 (0)