2017-10-10 51 views
0

我试图去掌握使用Electron和Spectron进行测试。测试电子应用程序的文本输入

我想写在我的身体是contentEditable,然后检查文本是否符合我的测试。到目前为止,我已经为这个标题写了一个成功的测试,但是我无法找到这个标题的解决方案。

Index.html;

<!DOCTYPE html> 
    <html> 
    <head> 
    <meta charset="UTF-8"> 
    <title>Hello</title> 
    </head> 
    <body id = "test" contentEditable = true> 
    </body> 
</html> 

featureTest.js其中包括标题的通过测试和我想在底部的测试尝试。

const electron = require("electron"); 
var expect = require("chai").expect; 

var Application = require("spectron").Application; 
var assert = require("assert"); 

describe("application launch", function() { 
    this.timeout(10000); 

    beforeEach(function() { 
    this.app = new Application({ 
     path: `${__dirname}/../node_modules/.bin/electron`, 
     args: ["main.js"] 
    }); 
    return this.app.start(); 
    }); 

    afterEach(function() { 
    if (this.app && this.app.isRunning()) { 
     return this.app.stop(); 
    } 
    }); 

    it("title says Hello", function() { 
    return this.app.client 
     .waitUntilWindowLoaded() 
     .getTitle() 
     .then(text => expect(text).to.eq("Hello")); 
    }); 

    it("inputs and then finds the text on the page", function() { 
    return this.app.client 
     .waitUntilWindowLoaded() 
     .elementIdText("test") 
     .keys("Hello World!") 
     .then(text => expect(text).to.eq("Hello World!")); 
    }); 
}); 

main.js;

const {app, BrowserWindow, Menu} = require('electron') 
const path = require('path') 
const url = require('url') 
const fs = require("fs") 

let mainWindow; 

app.on('ready', function() { 

    mainWindow = new BrowserWindow({width: 800, height: 600}) 

    mainWindow.loadURL(url.format({ 
    pathname: path.join(__dirname, 'index.html'), 
    protocol: 'file:', 
    slashes: true 
    })) 

    // Open the DevTools. 
    // mainWindow.webContents.openDevTools() 

    mainWindow.on('closed',() => { 
    app.quit(); 
    }) 

    const mainMenu = Menu.buildFromTemplate(mainMenuTemplate); 

    Menu.setApplicationMenu(mainMenu); 

    if(process.platform == 'darwin'){ 
    mainMenuTemplate.unshift({}); 
    } 

}); 

的package.json

"devDependencies": { 
    "chai": "^4.1.2", 
    "electron": "^1.7.8", 
    "mocha": "^4.0.1", 
    "spectron": "^3.7.2" 
    } 

回答

2

我已经找到了解决我的问题,只是写在这里以供参考,如果别人需要

工作测试;

it("should enter and show text", function(){ 
    return this.app.client 
     .waitUntilWindowLoaded() 
     .click('#test') 
     .keys('Hello') 
     .getText('#test') 
     .then(text => expect(text).to.eq('Hello')) 

好像不用彷徨[某件事]功能,可使文本到文本变量自动的,你可以从测试。