Developer-Dashboard

 view release on metacpan or  search on metacpan

t/22-api-dashboard-playwright.t  view on Meta::CPAN

                $status       = 200;
                $reason       = 'OK';
                $content_type = 'application/json';
                $payload      = json_encode(
                    {
                        ok     => 1,
                        method => $method || 'GET',
                        name   => $name,
                        path   => $uri->path,
                    }
                );
            }
            elsif ( $uri->path eq '/echo' ) {
                my $name = defined scalar $uri->query_param('name') ? scalar $uri->query_param('name') : '';
                $status       = 200;
                $reason       = 'OK';
                $content_type = 'application/json';
                $payload      = json_encode(
                    {
                        ok            => 1,
                        method        => $method || 'GET',
                        name          => $name,
                        path          => $uri->path,
                        authorization => $headers{'authorization'} || '',
                        content_type  => $headers{'content-type'} || '',
                        request_body  => $body,
                    }
                );
            }
            elsif ( $uri->path eq '/image' ) {
                $status       = 200;
                $reason       = 'OK';
                $content_type = 'image/png';
                $payload      = $png;
            }

            my $response = join(
                "\r\n",
                "HTTP/1.1 $status $reason",
                "Content-Type: $content_type",
                'Connection: close',
                'Content-Length: ' . length($payload),
                '',
                ''
            );
            print {$client} $response;
            print {$client} $payload;
            close $client;
        }
        exit 0;
    }
    return $pid;
}

# _playwright_script()
# Purpose: return the embedded Node.js Playwright browser flow used by this Perl test.
# Input: none.
# Output: JavaScript source text string.
sub _playwright_script {
    return <<'JAVASCRIPT';
const assert = require('assert');
const fs = require('fs');
const { chromium } = require(process.env.PLAYWRIGHT_DIR);

function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

async function waitForFile(path, shouldExist) {
  const deadline = Date.now() + 10000;
  while (Date.now() < deadline) {
    const exists = fs.existsSync(path);
    if (!!exists === !!shouldExist) return;
    await sleep(100);
  }
  throw new Error(`Timed out waiting for file state ${shouldExist ? 'present' : 'absent'}: ${path}`);
}

async function waitForFileText(path, expectedText) {
  const deadline = Date.now() + 10000;
  while (Date.now() < deadline) {
    if (fs.existsSync(path)) {
      const text = fs.readFileSync(path, 'utf8');
      if (text.includes(expectedText)) return;
    }
    await sleep(100);
  }
  throw new Error(`Timed out waiting for file text ${JSON.stringify(expectedText)} in ${path}`);
}

async function waitForCollection(page, name, shouldExist) {
  await page.waitForFunction(
    ({ collectionName, shouldExist: shouldHaveCollection }) => {
      const names = Array.from(document.querySelectorAll('.api-collection-tab')).map((node) => node.textContent.trim());
      return shouldHaveCollection ? names.includes(collectionName) : !names.includes(collectionName);
    },
    { collectionName: name, shouldExist }
  );
}

async function clickCollection(page, name) {
  await page.getByRole('tab', { name, exact: true }).click();
  await page.waitForFunction((collectionName) => {
    const panel = document.querySelector('#api-collection-panel');
    return panel
      && panel.getAttribute('data-api-collection-panel') === collectionName
      && panel.offsetParent !== null;
  }, name);
}

function escapeRegExp(text) {
  return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

async function waitForStoredRequest(page, collectionName, requestName) {
  await page.waitForFunction(({ storageKey, targetCollection, targetRequest }) => {
    const raw = window.localStorage.getItem(storageKey);
    if (!raw) return false;
    let state;
    try {
      state = JSON.parse(raw);
    } catch (error) {
      return false;



( run in 2.269 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )