|
1 | | -const { chromium } = require('playwright'); |
2 | | -const path = require('path'); |
3 | | -const process = require('process'); |
| 1 | +const { chromium } = require("playwright"); |
| 2 | +const path = require("path"); |
| 3 | +const process = require("process"); |
4 | 4 |
|
5 | 5 | console.log("INFO - ******Inside Setup file******"); |
6 | 6 |
|
7 | 7 | module.exports = async function () { |
8 | 8 | // if a playwright page object doesn't exist, create one |
9 | 9 | const browser = await chromium.launch({ |
10 | | - args: ['--ignore-certificate-errors'], |
| 10 | + args: ["--ignore-certificate-errors"], |
11 | 11 | headless: true, |
12 | 12 | }); |
13 | 13 |
|
14 | | - const page = await browser.newPage(); |
| 14 | + const page = await browser.newPage(); |
15 | 15 | console.log("INFO - new page object created"); |
16 | | - |
| 16 | + |
17 | 17 | // Extra safety for debugging slow loads |
18 | | - page.on('console', msg => console.log('INFO - [PAGE LOG] ', msg.text())); |
19 | | - page.on('response', res => { |
20 | | - if (res.status() >= 400) |
21 | | - console.log(`ERROR - [HTTP ${res.status()}] ${res.url()}`); |
| 18 | + page.on("console", (msg) => console.log("INFO - [PAGE LOG] ", msg.text())); |
| 19 | + page.on("response", (res) => { |
| 20 | + if (res.status() >= 400) console.log(`ERROR - [HTTP ${res.status()}] ${res.url()}`); |
22 | 21 | }); |
23 | 22 |
|
24 | 23 | // --- Step 1: Go to DataFed --- |
25 | 24 | console.log("INFO - 1. Got to DataFed"); |
26 | | - await page.goto('https://@DATAFED_DOMAIN@/ui/welcome', { waitUntil: 'networkidle', timeout: 60000 }); |
| 25 | + await page.goto("https://@DATAFED_DOMAIN@/ui/welcome", { |
| 26 | + waitUntil: "networkidle", |
| 27 | + timeout: 60000, |
| 28 | + }); |
27 | 29 |
|
28 | 30 | // --- Step 2: Click Login/Register --- |
29 | 31 | console.log("INFO - 2. Login and Register"); |
30 | | - const loginButton = page.getByRole('button', { name: 'Log In / Register' }); |
31 | | - await loginButton.waitFor({ state: 'visible', timeout: 30000 }); |
| 32 | + const loginButton = page.getByRole("button", { name: "Log In / Register" }); |
| 33 | + await loginButton.waitFor({ state: "visible", timeout: 30000 }); |
32 | 34 | await loginButton.click(); |
33 | 35 |
|
34 | 36 | // --- Step 3: Check if link is visible --- |
35 | 37 | console.log("INFO - 3. Check if link is visible"); |
36 | | - const globusLink = page.getByRole('link', { name: /Globus/i }); |
37 | | - await globusLink.waitFor({ state: 'visible', timeout: 30000 }); |
| 38 | + const globusLink = page.getByRole("link", { name: /Globus/i }); |
| 39 | + await globusLink.waitFor({ state: "visible", timeout: 30000 }); |
| 40 | + await page.waitForTimeout(1000); |
38 | 41 |
|
39 | 42 | // --- Step 4: Click Globus ID to sign in --- |
40 | 43 | console.log("INFO - 4. Use Globus ID to sign in"); |
41 | | - const globusIDButton = page.getByRole('button', { name: 'Globus ID to sign in' }); |
42 | | - await globusIDButton.waitFor({ state: 'visible', timeout: 30000 }); |
| 44 | + const globusIDButton = page.getByRole("button", { name: "Globus ID to sign in" }); |
| 45 | + await globusIDButton.waitFor({ state: "visible", timeout: 30000 }); |
43 | 46 | await globusIDButton.click(); |
| 47 | + await page.waitForTimeout(1000); |
44 | 48 |
|
45 | 49 | // --- Step 5: Wait for Globus redirect --- |
46 | 50 | console.log("INFO - 5. Wait for Globus login to redirect"); |
47 | | - await page.waitForLoadState('networkidle', { timeout: 45000 }); |
| 51 | + await page.waitForLoadState("networkidle", { timeout: 45000 }); |
48 | 52 |
|
49 | 53 | // --- Step 6: Fill in credentials robustly --- |
50 | 54 | console.log("INFO - 6. Fill in credentials"); |
51 | 55 | const usernameField = page.getByLabel(/username/i); |
52 | | - await usernameField.waitFor({ state: 'visible', timeout: 45000 }); |
| 56 | + await usernameField.waitFor({ state: "visible", timeout: 45000 }); |
53 | 57 | await usernameField.fill(process.env.DATAFED_WEB_TEST_USERNAME); |
54 | 58 |
|
55 | 59 | const passwordField = page.getByLabel(/password/i); |
56 | | - await passwordField.waitFor({ state: 'visible', timeout: 45000 }); |
| 60 | + await passwordField.waitFor({ state: "visible", timeout: 45000 }); |
57 | 61 | await passwordField.fill(process.env.DATAFED_WEB_TEST_PASSWORD); |
58 | 62 |
|
59 | 63 | // --- Step 7: Submit form --- |
60 | 64 | await page.click('button[type="submit"]'); |
61 | | - await page.waitForURL('https://@DATAFED_DOMAIN@/ui/main') |
62 | 65 |
|
| 66 | + await Promise.race([ |
| 67 | + page.waitForURL(/\/ui\/main$/, { timeout: 45000 }), |
| 68 | + page.waitForURL(/\/ui\/register$/, { timeout: 45000 }), |
| 69 | + ]); |
| 70 | + |
| 71 | + let current = page.url(); |
| 72 | + console.log("INFO - 7a Redirected to:", current); |
| 73 | + |
| 74 | + if (current.includes("/ui/register")) { |
| 75 | + console.log("INFO - 7b. Registering:", current); |
| 76 | + await page.getByRole('button', { name: 'Continue Registration' }).click(); |
| 77 | + } else { |
| 78 | + console.log("INFO - 7b. Grabbing fresh url:", current); |
| 79 | + if (!page.url().includes("/ui/main")) { |
| 80 | + await page.waitForURL("https://@DATAFED_DOMAIN@/ui/main", { timeout: 45000 }); |
| 81 | + } |
| 82 | + current = page.url(); |
| 83 | + console.log("INFO - 7c. URL is: ", current); |
| 84 | + } |
| 85 | + |
| 86 | + await page.screenshot({ path: "after_login.png", fullPage: true }); |
| 87 | + if (current.includes("/ui/main")) { |
| 88 | + console.log("INFO - 8b Successful: ", current); |
| 89 | + } else { |
| 90 | + throw new Error(`Unexpected redirect URL: ${current}`); |
| 91 | + } |
| 92 | + //await Promise.all([ |
| 93 | + // page.waitForNavigation({ url: /\/ui\/main/ }), // robust matching |
| 94 | + // page.click('button[type="submit"]'), |
| 95 | + //]); |
| 96 | + //page.screenshot(path="final.png", full_page=True) |
63 | 97 | console.log("INFO - ******PAST LOGIN******"); |
64 | | - await page.context().storageState({ path: './.auth/auth.json'}); //TESTING |
| 98 | + await page.context().storageState({ path: "./.auth/auth.json" }); //TESTING |
65 | 99 | console.log("INFO - ******Done with login******"); |
66 | 100 |
|
67 | 101 | await browser.close(); |
68 | 102 | }; |
69 | | - |
|
0 commit comments