-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix clipboard read/write on regular elements in e2e tests #55030
base: trunk
Are you sure you want to change the base?
Conversation
Size Change: +184 B (0%) Total Size: 1.7 MB
ℹ️ View Unchanged
|
Flaky tests detected in 745f8da. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6875697803
|
Okay it doesn't work on CI 😅 . I'll keep looking for alternatives. |
Thanks for identifying and working on this! It stumped me yesterday 😅 |
fcd0c36
to
b965e4b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this by applying the changes to press-keys.ts
against the work in #54718.
I can confirm that the test that is .skip
will pass 🎉 .
As such this feels like an improvement.
I'm not sure I'm best placed to review the technical implementation details to I've cc'd @WunderBart who has done a lot of work in this area in the hope he can assist.
Great work 🙇
@kevin940726, can you rebase this on top of the trunk? Let's ensure that changes here work as expected with the latest Playwright. |
2db7526
to
55c091d
Compare
const timeout = setTimeout( () => { | ||
resolve( false ); | ||
}, 50 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a safeguard to resolve the Promise
if the method isn't able to add an event listener, primarily due to activeElement
being unavailable. Is my logic/understanding here correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this is here in case the event never fires. This could happen for paste
events when the element doesn't attach an event listener and it needs to fallback to native "real" event. We need a flag here that basically means hasEventHandled
, and it that's false
we'll fire a real key press.
I know this is all very confusing and I've been thinking about better ways to document this 😅. Maybe I should make a comparison table or something like that...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any additional documentation would be helpful here.
@@ -450,7 +450,7 @@ test.describe( 'Copy/cut/paste', () => { | |||
// back to default browser behaviour, allowing the browser to insert | |||
// unfiltered HTML. When we swap out the post title in the post editor | |||
// with the proper block, this test can be removed. | |||
pageUtils.setClipboardData( { | |||
await pageUtils.setClipboardData( { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm seeing Unexpected
await of a non-Promise (non-"Thenable") value.
ESLint errors here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is possibly due to outdated build. Try running npm run dev
or npm run build
again and see if eslint still errors?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, @kevin940726!
The changes look good to me (thanks for the inline comments). The original RichText behavior works as before, which big plus ➕
Having test cases for regular elements would have been nice, but not a blocker.
I can still see some instances where the |
Since the basic clipboard support via shortcuts seems to be there, I was wondering if a (what might be a simpler) ctrl-c ctrl-v would work now. I tried to make a very simple PoC and it seems to be working well (at least for this case): test( 'should turn selected text into a link', async ( { page, editor } ) => {
// Set content to copy (could be turned into the setClipboardContent util?)
const tmpPage = await page.context().newPage();
await tmpPage.setContent( 'https://wordpress.org' );
await tmpPage.keyboard.press( 'Meta+A' );
await tmpPage.keyboard.press( 'Meta+C' );
await tmpPage.close();
await editor.canvas
.getByRole( 'button', { name: 'Add default block' } )
.click();
await page.keyboard.type('WordPress');
await page.keyboard.press( 'Meta+A' );
await page.keyboard.press( 'Meta+V' );
await page.pause(); // see the WordPress link (when headed :D)
} ); Please ignore me if this has already been considered and didn't work. 😅 If the current implementation works as expected, let's go with it! 👍 |
Nice catch! These tests were added after this PR so I forgot about them 😅. The tests could pass if it's running fast enough, but still better to be awaited.
I tried something like this but unfortunately it didn't work on Linux (CI) for some reason. Happy to revalidate this if you manage to get it work though! You can try the official docker image to run it in an environment very close to what's on CI. docker run --network host --rm -it -v $(pwd):/work/ -w /work/ mcr.microsoft.com/playwright:v1.39.0 /bin/bash |
Thanks for the snippet - very useful! I was able to make the ctrl-c ctrl-v work there as well, but it does seem to behave a bit differently than on my local machine. Let's go with this one and maybe actually wait for the official support 😅 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one, thanks! 🚀
What?
Currently, pasting values from clipboard to regular elements with no
paste
event handlers won't work as expected. This PR fixes that.Why?
See #54718 (comment).
How?
It's actually really tricky. The solution doesn't always work but it works in most cases which is good enough for us.
The key is to fire both the actual key press (
primary+v
) and the clipboard emulation event (dispatchEvent
) together. The order is different for writing to the clipboard (copy
andcut
) and reading from the clipboard (paste
) though. The actual reasoning can be shown in the inline comments, but it's still fairly complicated 😅 . It still took multiple retries to make it work on CI. Feel free to ask any questions!Testing Instructions
CI should pass, hopefully 🤞 .
Testing Instructions for Keyboard
N/A
Screenshots or screencast
N/A