我正在使用 Electron + Nightmare.js 进行单元测试。
我需要将一个字符串复制到剪贴板> 聚焦一个元素> 粘贴内容。然后测试我的 JavaScript 是否处理得当。
我在 Electron 文档中阅读了关于 clipboard api 的信息,
和 copy/paste in webview ,但不确定它如何与 Nightmare.js API 集成,可能在 .action
中作为suggested in this issue .
一个例子是:
import nightmare from 'nightmare'
nightmare.action('copyPaste', function(name, options, parent, win, renderer, done) {
// some magic here
});
// and then
let res = await page
.wait('.my-element-to-render')
.copyPaste(blob)
.evaluate(() => {
return document.querySelector('.my-element').value;
}).end();
expect(res).to.equal('my pasted string');
对此有什么建议或经验吗?
根据我从 nightmare.action
得到的论点什么相当于<webview>
所以我可以调用它的复制/粘贴方法?
最佳答案
复制/粘贴在 Electron 中不起作用。这是由于应用程序的菜单缺少与 native 剪贴板的键绑定(bind)。您可以使用这段 JS 代码来解决这个问题。
您还应该查看此 GitHub存储库 - 它是一个干净的 HowTo 解决这个问题。
代码片段
var app = require("app");
var BrowserWindow = require("browser-window");
var Menu = require("menu");
var mainWindow = null;
app.on("window-all-closed", function(){
app.quit();
});
app.on("ready", function () {
mainWindow = new BrowserWindow({
width: 980,
height: 650,
"min-width": 980,
"min-height": 650
});
mainWindow.openDevTools();
mainWindow.loadUrl("file://" + __dirname + "/index.html");
mainWindow.on("closed", function () {
mainWindow = null;
});
// Create the Application's main menu
var template = [{
label: "Application",
submenu: [
{ label: "About Application", selector: "orderFrontStandardAboutPanel:" },
{ type: "separator" },
{ label: "Quit", accelerator: "Command+Q", click: function() { app.quit(); }}
]}, {
label: "Edit",
submenu: [
{ label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
{ label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
{ type: "separator" },
{ label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
{ label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
{ label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
{ label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" }
]}
];
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
});
关于javascript - 是否可以使用 Electron 进行复制/粘贴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43584851/