index.ts 786 B

12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import type { Locator } from '@playwright/test';
  6. /**
  7. * @param {import('@playwright/test').Locator} locator
  8. */
  9. export async function getOffsetByLocator(locator: Locator) {
  10. return locator.evaluate((el) => {
  11. const rect = el.getBoundingClientRect();
  12. const left = rect.left;
  13. const top = rect.top;
  14. const width = rect.width;
  15. const height = rect.height;
  16. return {
  17. left,
  18. top,
  19. width,
  20. height,
  21. centerX: left + width / 2,
  22. centerY: top + height / 2,
  23. right: left + width,
  24. bottom: top + height,
  25. };
  26. });
  27. }
  28. export function cssEscape(str: string) {
  29. return str.replace(/([ !"#$%&'()*+,.\/:;<=>?@[\]^`{|}~])/g, '\\$1');
  30. }