baseLayout.e2e.spec.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import type { Page } from '@playwright/test';
  2. import { test, expect } from '@playwright/test';
  3. const { uniq } = require('lodash');
  4. const RouterConfig = require('../../config/routes').default;
  5. const BASE_URL = `http://localhost:${process.env.PORT || 8001}`;
  6. function formatter(routes: any, parentPath = ''): string[] {
  7. const fixedParentPath = parentPath.replace(/\/{1,}/g, '/');
  8. let result: string[] = [];
  9. routes.forEach((item: { path: string; routes: string }) => {
  10. if (item.path && !item.path.startsWith('/')) {
  11. result.push(`${fixedParentPath}/${item.path}`.replace(/\/{1,}/g, '/'));
  12. }
  13. if (item.path && item.path.startsWith('/')) {
  14. result.push(`${item.path}`.replace(/\/{1,}/g, '/'));
  15. }
  16. if (item.routes) {
  17. result = result.concat(
  18. formatter(item.routes, item.path ? `${fixedParentPath}/${item.path}` : parentPath),
  19. );
  20. }
  21. });
  22. return uniq(result.filter((item) => !!item));
  23. }
  24. const testPage = (path: string, page: Page) => async () => {
  25. await page.evaluate(() => {
  26. localStorage.setItem('antd-pro-authority', '["admin"]');
  27. });
  28. await page.goto(`${BASE_URL}${path}`);
  29. await page.waitForSelector('footer', {
  30. timeout: 2000,
  31. });
  32. const haveFooter = await page.evaluate(() => document.getElementsByTagName('footer').length > 0);
  33. expect(haveFooter).toBeTruthy();
  34. };
  35. const routers = formatter(RouterConfig);
  36. routers.forEach((route) => {
  37. test(`test route page ${route}`, async ({ page }) => {
  38. await testPage(route, page);
  39. });
  40. });