File size: 2,370 Bytes
52d3f94
 
f0dc812
52d3f94
 
 
7c046f8
52d3f94
f0dc812
 
 
52d3f94
7c046f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52d3f94
7c046f8
 
52d3f94
 
f0dc812
 
7c046f8
cfcecfd
f0dc812
52d3f94
 
 
 
 
 
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
import { chromium } from 'playwright';

export async function runLogin () {
  let browser;
  try {
    browser = await chromium.launch({
      args: ['--no-sandbox'],
    });
    const ctx  = await browser.newContext({ ignoreHTTPSErrors: true });
    const page = await ctx.newPage();
    await page.goto(process.env.N8N_URL, { waitUntil: 'networkidle', timeout: 60000 });

    /* ---------- BASIC AUTH (optional) ---------- */
    const basicUserInput = page.locator('input[type="text"]');
    if (process.env.BASIC_USER && await basicUserInput.count()) {
      await basicUserInput.fill(process.env.BASIC_USER);
      await page.fill('input[type="password"]', process.env.BASIC_PASS || '');
      await page.press('input[type="password"]', 'Enter');
    }

    /* ---------- detect current page ---------- */
    const signInForm  = page.locator('[data-test-id="signin-form"]');
    const overviewTab = page.getByRole('link', { name: /^overview$/i });

    let needSignIn = false;
    try {
      await Promise.race([
        signInForm.waitFor({ state: 'visible', timeout: 45000 }),
        overviewTab.waitFor({ state: 'visible', timeout: 45000 }),
      ]);
      needSignIn = await signInForm.count() > 0;
    } catch (_) {
      needSignIn = false;  // neither selector appeared
    }

    /* ---------- perform sign-in ---------- */
    if (needSignIn) {
      const email = page.locator(
        'input[name="emailOrLdapLoginId"], input[name="email"]'
      ).first();
      const pass  = page.locator('input[name="password"]').first();

      if (email) {
        console.log('Email entry found')
      }
      if (pass) {
        console.log('Password entry found')
      }
      await email.fill(process.env.N8N_EMAIL || '');
      await pass.fill(process.env.N8N_PASSWORD || '');
      console.log('Filled')
      await page.getByRole('button', { name: /sign in/i }).click();
      await overviewTab.waitFor({ state: 'visible', timeout: 60000 });
      console.log('Singed in')
    }

    /* take screenshot → Buffer */
    const png = await page.screenshot({ type: 'png', fullPage: true });
    console.log('Screen Captured')
    console.log('✅ Playwright finished');
    return { ok: true, png };          // return raw Buffer
  } catch (err) {
    return { ok: false, error: err.message };
  } finally {
    if (browser) await browser.close();
  }
}