drag.spec.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { test, expect } from '@playwright/test';
  6. import { getOffsetByLocator, cssEscape } from '../utils';
  7. import PageModel from './models';
  8. const OFFSET = 10;
  9. test.describe('test drag', () => {
  10. let editorPage: PageModel;
  11. test.beforeEach(async ({ page }) => {
  12. editorPage = new PageModel(page);
  13. await page.goto('http://localhost:3000');
  14. await page.waitForTimeout(1000);
  15. });
  16. test('drag node', async ({ page }) => {
  17. // 获取 node
  18. const DRAG_NODE_ID = 'agent_0';
  19. const DRAG_TO_PORT_ID = 'switch_0';
  20. const agentLocator = await page.locator(`#${cssEscape(`$slotIcon$${DRAG_NODE_ID}`)}`);
  21. const fromOffset = await getOffsetByLocator(agentLocator);
  22. const from = {
  23. x: fromOffset.left + OFFSET,
  24. y: fromOffset.top + OFFSET,
  25. };
  26. const toLocator = await page.locator(`[data-from="${DRAG_TO_PORT_ID}"]`);
  27. const toOffset = await getOffsetByLocator(toLocator);
  28. const to = {
  29. x: toOffset.left,
  30. y: toOffset.top,
  31. };
  32. await editorPage.drag(from, to);
  33. await page.waitForTimeout(100);
  34. // 通过 data-to 判断是否移动成功
  35. const toLocator2 = await page.locator(`[data-from="${DRAG_TO_PORT_ID}"]`);
  36. const attribute = await toLocator2?.getAttribute('data-to');
  37. expect(attribute).toEqual(DRAG_NODE_ID);
  38. });
  39. test('drag branch', async ({ page }) => {
  40. const START_ID = 'case_0';
  41. const END_ID = 'case_default_1';
  42. const branchLocator = page.locator(`#${cssEscape(`$blockOrderIcon$${START_ID}`)}`);
  43. const fromOffset = await getOffsetByLocator(branchLocator);
  44. const from = {
  45. x: fromOffset.left + OFFSET,
  46. y: fromOffset.top + OFFSET,
  47. };
  48. const toBranchLocator = await page.locator(`#${cssEscape(`$blockOrderIcon$${END_ID}`)}`);
  49. const toOffset = await getOffsetByLocator(toBranchLocator);
  50. const to = {
  51. x: toOffset.left - OFFSET / 2,
  52. y: toOffset.top + OFFSET,
  53. };
  54. await editorPage.drag(from, to);
  55. await page.waitForTimeout(100);
  56. const fromOffset2 = await getOffsetByLocator(branchLocator);
  57. expect(fromOffset2.centerX).toBeGreaterThan(fromOffset.centerX);
  58. });
  59. });