英文:
How to give Chrome clipboard permissions in cypress 12
问题
这是您要翻译的内容:
"I am testing how "copy to clipboard" works in Cypress using Javascript and I used w3schools website for this.
Here is the code:
/// <reference types="Cypress" />
describe('w3schools', () => {
it.only('using clipboard', () =>{
cy.visit('https://www.w3schools.com/howto/howto_js_copy_clipboard.asp');
cy.wrap( //to give permision to read write from clipboard
Cypress.automation('remote:debugger:protocol', {
command: 'Browser.grantPermissions',
params: {
permissions: ['clipboardReadWrite', 'clipboardSanitizedWrite'],
origin: window.location.origin,
},
}),
);
cy.get('.tooltip > .w3-button').click()
cy.window().its('navigator.permissions').invoke('readText')
})
})
I tried to give chrome browser clipboard permissions using the following video
https://www.youtube.com/watch?v=4eEc3x24D64&t=210s
However, cypress error message says the permission is still not granted.
Clipboard permission not granted. Did I miss some additional settings or tweaks? The answers there did not work as cypress.json file is replaced with cypress.config.js"
英文:
I am testing how "copy to clipboard" works in Cypress using Javascript and I used w3schools website for this.
Here is the code:
/// <reference types="Cypress" />
describe('w3schools', () => {
it.only('using clipboard', () =>{
cy.visit('https://www.w3schools.com/howto/howto_js_copy_clipboard.asp');
cy.wrap( //to give permision to read write from clipboard
Cypress.automation('remote:debugger:protocol', {
command: 'Browser.grantPermissions',
params: {
permissions: ['clipboardReadWrite', 'clipboardSanitizedWrite'],
origin: window.location.origin,
},
}),
);
cy.get('.tooltip > .w3-button').click()
cy.window().its('navigator.permissions').invoke('readText')
})
})
I tried to give chrome browser clipboard permissions using the following video
https://www.youtube.com/watch?v=4eEc3x24D64&t=210s
However, cypress error message says the permission is still not granted.
Clipboard permission not granted. Did I miss some additional settings or tweaks? The answers there did not work as cypress.json file is replaced with cypress.config.js
答案1
得分: 7
如果您正在使用最新的Cypress版本12.9.0,invoke()
命令已更改为查询,从而改变了其工作方式。我认为这可能导致了您的错误。
您可以替代使用.then()
。
cy.window().its('navigator.clipboard')
.then((clip) => clip.readText())
.should('equal', 'Hello World')
此外,将CDP移至顶部 - 完整的结构如下:
Cypress.automation('remote:debugger:protocol', {
command: 'Browser.grantPermissions',
params: {
permissions: ['clipboardReadWrite', 'clipboardSanitizedWrite'],
origin: window.location.origin,
},
})
cy.visit('https://www.w3schools.com/howto/howto_js_copy_clipboard.asp')
cy.contains('button', 'Copy text').click()
cy.window().its('navigator.clipboard')
.then((clip) => clip.readText())
.should('equal', 'Hello World')
英文:
If you're using the latest Cypress version 12.9.0, the invoke()
command has been changed to a query which changes the way it works. I think this is causing your error.
You can substitute .then()
instead.
cy.window().its('navigator.clipboard')
.then((clip) => clip.readText())
.should('equal', 'Hello World')
Also move the CDP to the top - the full structure:
Cypress.automation('remote:debugger:protocol', {
command: 'Browser.grantPermissions',
params: {
permissions: ['clipboardReadWrite', 'clipboardSanitizedWrite'],
origin: window.location.origin,
},
})
cy.visit('https://www.w3schools.com/howto/howto_js_copy_clipboard.asp')
cy.contains('button', 'Copy text').click()
cy.window().its('navigator.clipboard')
.then((clip) => clip.readText())
.should('equal', 'Hello World')
答案2
得分: 4
你可以使用 cy.stub()
或 cy.spy()
来捕捉剪贴板的活动。
请注意,应用程序与剪贴板进行交互的其他方式,因此这仅限于您正在测试的特定页面。
Cypress.automation('remote:debugger:protocol', {
command: 'Browser.grantPermissions',
params: {
permissions: ['clipboardReadWrite', 'clipboardSanitizedWrite'],
origin: window.location.origin,
},
})
cy.visit('https://www.w3schools.com/howto/howto_js_copy_clipboard.asp', {
onBeforeLoad: (contentWindow) => {
cy.spy(contentWindow.navigator.clipboard, 'writeText').as('writeText')
}
})
cy.contains('button', 'Copy text').click()
cy.get('@writeText')
.should('have.been.calledWith', 'Hello World')
英文:
You can use cy.stub()
or cy.spy()
to catch the clipboard activity.
Note, there are other ways for the app to interact with the clipboard, so this is limited to the particular page you are testing.
Cypress.automation('remote:debugger:protocol', {
command: 'Browser.grantPermissions',
params: {
permissions: ['clipboardReadWrite', 'clipboardSanitizedWrite'],
origin: window.location.origin,
},
})
cy.visit('https://www.w3schools.com/howto/howto_js_copy_clipboard.asp', {
onBeforeLoad: (contentWindow) => {
cy.spy(contentWindow.navigator.clipboard, 'writeText').as('writeText')
}
})
cy.contains('button', 'Copy text').click()
cy.get('@writeText')
.should('have.been.calledWith', 'Hello World')
答案3
得分: 1
在版本12.7.0上,您可以使用以下代码来授予剪贴板权限。
cy.window()
.its('navigator.clipboard')
.then((clipboard) =>
cy
.stub(clipboard, 'writeText')
.resolves()
// @ts-ignore
.as('writeText'),
)
然后,您可以使用以下代码来检查剪贴板内容。
cy.get('@writeText')
.should('have.been.calledOnce')
.its('firstCall.args.0')
.should('equal', expected)
如果您需要进一步的帮助,请告诉我。
英文:
On v12.7.0, you can use the following to grant clipboard permissions.
cy.window()
.its('navigator.clipboard')
.then((clipboard) =>
cy
.stub(clipboard, 'writeText')
.resolves()
// @ts-ignore
.as('writeText'),
)
then you can check the clipboard with
cy.get('@writeText')
.should('have.been.calledOnce')
.its('firstCall.args.0')
.should('equal', expected)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论