Prompt on cypress
cesarlima-hotmart opened this issue ยท 12 comments
Prompt alert when testing the application with cypress. Is there a way to solve it?
I guess just disabling fallback might be an okay~ish thing for such case, if you don't need to test successful copying. Then either using fork right now from https://github.com/sudodoki/copy-to-clipboard/pull/82/files or waiting for it to be merged, which also needs some readme/typing updates
In fact, i'm testing if the text is successfully copied to clipboard.
@cesarlima-hotmart then you might be in trouble ๐ฌ see https://github.com/sudodoki/copy-to-clipboard/tree/master/tests and https://github.com/sudodoki/copy-to-clipboard/blob/master/e2e_helpers/assertions/assertBuffer.js for possible references. I used extra elements to do 'reseting' of the buffer by calling ctrl/cmd+c with some text selected and pasted stuff into textarea/etc to verify I was able to copy something from buffer and that stuff I needed was copied.
I wasn't able to reliably write tests for at least most commong platforms (Chrome / Firefox) as it seemed line endings were inconsistent across different browsers, so I ended up with regexes that were more relaxed with whitespacing than exact matches on strings
Yeah, cypress runs on chromium, but i think the problem is the app runs in a webview inside it. The weird thing is, in this cypress webview, it works if I click manually on the button that calls the copy, but it prompts if i click on it by the cypress click command.
there're some issues with security which might be in play, as execCommand cannot be executed without user's interaction, not sure whether that's the case here
The solution i've found is check the parameters of the alert and assume that it is going to be the copied value in the real scenario.
cy.window().then(win => {
cy.stub(win, 'prompt').returns(win.prompt).as('copyToClipboardPrompt');
});
let link: string;
cy.get('.transmission-info-container .info .column:last-child .link').find('a').then($a => {
link = $a.text().trim();
});
cy.get('.transmission-info-container .info .column:last-child .link > div').click();
cy.get('@copyToClipboardPrompt').should('be.called');
cy.get('@copyToClipboardPrompt').should(prompt => {
expect(prompt.args[0][1]).to.equal(link);
});
Thank you!
@cesarlima-hotmart
I lost 2 days before find your solution. Good life and good health to you !
@cesarlima-hotmart thanks, it works fine in cypress test runner, but when I run it as a node module in Electron( by command npx cypress run --spec "cypress/*/my-spec.js
") it showed error as AssertionError: Timed out retrying after 15000ms: expected copyToClipboardPrompt to have been called at least once, but it was never called
God bless you man!
@cesarlima-hotmart
This world bless you. Thanks!
@inoutw Did you ever find a solution to to your assertion error? I'm having similar issues
Update: Turns out what happens is that Electron does not support alerts, so the "prompt" action didn't register.
To get around this issue I just had a modal/message pop up that shows me what I copied and did a simple
.contains(`Copied ${copiedData}`).should("exist");