Skip to content

Commit

Permalink
fix: tryfix safari clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
astandrik committed Dec 28, 2024
1 parent 2cafe1b commit a508373
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
19 changes: 13 additions & 6 deletions tests/suites/tenant/summary/objectSummary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,7 @@ test.describe('Object Summary', async () => {
]);
});

test('Copy path copies correct path to clipboard', async ({page, browserName}) => {
test.skip(browserName === 'webkit', 'Clipboard API is not reliable in Safari');

test.only('Copy path copies correct path to clipboard', async ({page}) => {
const pageQueryParams = {
schema: dsVslotsSchema,
database: tenantName,
Expand All @@ -215,9 +213,18 @@ test.describe('Object Summary', async () => {
const objectSummary = new ObjectSummary(page);
await objectSummary.clickActionMenuItem(dsVslotsTableName, RowTableAction.CopyPath);

await page.waitForTimeout(1000);

const clipboardContent = await getClipboardContent(page);
// Wait for clipboard operation to complete
await page.waitForTimeout(2000);

// Retry clipboard read a few times if needed
let clipboardContent = '';
for (let i = 0; i < 3; i++) {
clipboardContent = await getClipboardContent(page);
if (clipboardContent) {
break;
}
await page.waitForTimeout(500);
}
expect(clipboardContent).toBe('.sys/ds_vslots');
});

Expand Down
27 changes: 25 additions & 2 deletions tests/utils/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,35 @@ import type {Page} from '@playwright/test';
export const getClipboardContent = async (page: Page): Promise<string> => {
await page.context().grantPermissions(['clipboard-read']);

return page.evaluate(async () => {
// First try the modern Clipboard API
const clipboardText = await page.evaluate(async () => {
try {
const text = await navigator.clipboard.readText();
return text;
} catch (error) {
console.error('Failed to read clipboard:', error);
return null;
}
});

if (clipboardText !== null) {
return clipboardText;
}

// Fallback: Create a contenteditable element, focus it, and send keyboard shortcuts
return page.evaluate(async () => {
const el = document.createElement('div');
el.contentEditable = 'true';
document.body.appendChild(el);
el.focus();

try {
// Send paste command
document.execCommand('paste');
const text = el.textContent || '';
document.body.removeChild(el);
return text;
} catch (error) {
document.body.removeChild(el);
return '';
}
});
Expand Down

0 comments on commit a508373

Please sign in to comment.