simoncck commited on
Commit
7c046f8
·
verified ·
1 Parent(s): cfcecfd

Update login.js

Browse files
Files changed (1) hide show
  1. login.js +43 -11
login.js CHANGED
@@ -4,27 +4,59 @@ export async function runLogin () {
4
  let browser;
5
  try {
6
  browser = await chromium.launch({
7
- args: ['--no-sandbox', '--ignore-certificate-errors'],
8
  });
9
  const ctx = await browser.newContext({ ignoreHTTPSErrors: true });
10
  const page = await ctx.newPage();
11
  await page.goto(process.env.N8N_URL, { waitUntil: 'networkidle', timeout: 60000 });
12
 
13
- /* sign-in if form present */
14
- const signIn = page.locator('[data-test-id="signin-form"]');
15
- if (await signIn.count()) {
16
- await page
17
- .locator('input[name="emailOrLdapLoginId"], input[name="email"]')
18
- .first().fill(process.env.N8N_EMAIL || '');
19
- await page.locator('input[name="password"]').first()
20
- .fill(process.env.N8N_PASSWORD || '');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  await page.getByRole('button', { name: /sign in/i }).click();
22
- await page.locator('text=Overview').waitFor({ timeout: 60000 });
 
23
  }
24
 
25
  /* take screenshot → Buffer */
26
  const png = await page.screenshot({ type: 'png', fullPage: true });
27
-
28
  console.log('✅ Playwright finished');
29
  return { ok: true, png }; // return raw Buffer
30
  } catch (err) {
 
4
  let browser;
5
  try {
6
  browser = await chromium.launch({
7
+ args: ['--no-sandbox'],
8
  });
9
  const ctx = await browser.newContext({ ignoreHTTPSErrors: true });
10
  const page = await ctx.newPage();
11
  await page.goto(process.env.N8N_URL, { waitUntil: 'networkidle', timeout: 60000 });
12
 
13
+ /* ---------- BASIC AUTH (optional) ---------- */
14
+ const basicUserInput = page.locator('input[type="text"]');
15
+ if (process.env.BASIC_USER && await basicUserInput.count()) {
16
+ await basicUserInput.fill(process.env.BASIC_USER);
17
+ await page.fill('input[type="password"]', process.env.BASIC_PASS || '');
18
+ await page.press('input[type="password"]', 'Enter');
19
+ }
20
+
21
+ /* ---------- detect current page ---------- */
22
+ const signInForm = page.locator('[data-test-id="signin-form"]');
23
+ const overviewTab = page.getByRole('link', { name: /^overview$/i });
24
+
25
+ let needSignIn = false;
26
+ try {
27
+ await Promise.race([
28
+ signInForm.waitFor({ state: 'visible', timeout: 45000 }),
29
+ overviewTab.waitFor({ state: 'visible', timeout: 45000 }),
30
+ ]);
31
+ needSignIn = await signInForm.count() > 0;
32
+ } catch (_) {
33
+ needSignIn = false; // neither selector appeared
34
+ }
35
+
36
+ /* ---------- perform sign-in ---------- */
37
+ if (needSignIn) {
38
+ const email = page.locator(
39
+ 'input[name="emailOrLdapLoginId"], input[name="email"]'
40
+ ).first();
41
+ const pass = page.locator('input[name="password"]').first();
42
+
43
+ if (email) {
44
+ console.log('Email entry found')
45
+ }
46
+ if (pass) {
47
+ console.log('Password entry found')
48
+ }
49
+ await email.fill(process.env.N8N_EMAIL || '');
50
+ await pass.fill(process.env.N8N_PASSWORD || '');
51
+ console.log('Filled')
52
  await page.getByRole('button', { name: /sign in/i }).click();
53
+ await overviewTab.waitFor({ state: 'visible', timeout: 60000 });
54
+ console.log('Singed in')
55
  }
56
 
57
  /* take screenshot → Buffer */
58
  const png = await page.screenshot({ type: 'png', fullPage: true });
59
+ console.log('Screen Captured')
60
  console.log('✅ Playwright finished');
61
  return { ok: true, png }; // return raw Buffer
62
  } catch (err) {