forked from jknphy/e2e-agama-playwright
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global-setup.ts
76 lines (61 loc) · 2.36 KB
/
global-setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// See https://playwright.dev/docs/auth#reuse-signed-in-state
// and https://playwright.dev/docs/test-global-setup-teardown
import { expect, chromium, firefox, FullConfig, BrowserType, FullProject } from '@playwright/test';
const fs = require('fs');
function browserType(typeName:string):BrowserType {
if (typeName === "firefox") {
return firefox;
} else if (typeName === "chromium") {
return chromium;
} else {
throw new Error(`Unsupported browser type "${typeName}"`);
}
}
// find the project (browser) to use for logging in:
// - specified by the "--project" command line option
// - or find the first installed browser
function findProject(config: FullConfig):FullProject {
let project : FullProject;
const optionIndex = process.argv.findIndex(a => a === "--project");
if (optionIndex >= 0) {
const projectName = process.argv[optionIndex + 1];
project = config.projects.find(p => p.name === projectName);
}
else {
project = config.projects.find(p => fs.existsSync(p.use.launchOptions.executablePath));
}
if (project === undefined) {
throw new Error("Web browser not found");
}
return project;
}
async function globalSetup(config: FullConfig) {
// baseURL and storageState are the same for all projects, use the first one
const { baseURL, storageState } = config.projects[0].use;
// storage not configure => login was disabled
if (storageState === undefined) return;
const project = findProject(config);
const browser = await browserType(project.use.defaultBrowserType).launch(project.use.launchOptions);
const page = await browser.newPage({
baseURL,
ignoreHTTPSErrors: true
});
// go to the terminal app to see if the user needs to log into the system
await page.goto("/cockpit/@localhost/system/terminal.html");
// login page displayed?
try {
await page.waitForSelector("#login-user-input", { timeout: 5000 });
}
catch {
// form not found, login not required
return;
}
await page.getByLabel('User name').fill('root');
await page.getByLabel('Password', { exact: true }).fill('linux');
await page.getByRole('button', { name: 'Log in' }).click();
await expect(page.getByLabel('Password', { exact: true })).not.toBeVisible();
// Save the signed-in state to a file
await page.context().storageState({ path: storageState as string });
await browser.close();
}
export default globalSetup;