如何获取 cypress 中的“剪贴板”内容。我的网络应用程序中有一个按钮,单击按钮系统将执行“复制到剪贴板”并显示一条消息。下面是复制到剪贴板的 url 内容的示例(此 url 内容与网站 url 不同)
我仔细检查了该按钮标记中是否没有 href
属性。所以我使用了一个名为 Clipboardy 的插件,并且添加了 plugins/index.js
文件
const clipboardy = require('clipboardy');
module.exports = ( on ) => {
on('task', {
getClipboard () {
return clipboardy.readSync();
}
});
};
在我的测试中,我使用 cy.task() 来获取剪贴板内容,但这并没有打印实际的 url 内容
cy.get('td').find('div').find('span').find('button').find('i').eq(0).click().then(()=>{
cy.task('getClipboard').then((data)=>{
console.log("Helloooo:"+data);
})
})
<td class="sc-hgRTRy duUdhJ">
<div>
<span class="sc-bYwvMP jTmLTC">
<span class="sc-jzJRlG iVpVVy">
<span role="button" aria-expanded="true" aria-haspopup="true" aria-owns="5aa03785-1370-455e-a838-4154f7481a7b">
<button class="sc-feJyhm cJOKrG">
<i class="icon fas fa-link sc-htpNat cQABgO" aria-hidden="true" data-component-type="icon">
</i>
</button>
</span>
</span>
</span>
</div>
</td>
最佳答案
如果您不想使用clipboardy package你也可以使用这个:
cy.window().then((win) => {
win.navigator.clipboard.readText().then((text) => {
expect(text).to.eq('your copied text');
});
});
在你的情况下,它会变成类似
cy.get('td').find('div').find('span').find('button').find('i').eq(0).click().then(()=>{
cy.window().then((win) => {
win.navigator.clipboard.readText().then((text) => {
console.log("Helloooo:"+text);
});
});
})
关于javascript - 如何在 cypress 中获取复制到剪贴板的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61650737/