向导 - 使用 Selenium 和 WebDriver
优质
小牛编辑
203浏览
2023-12-01
为了能够使和Electron一起正常工作,我们需要告诉它Electron在哪,并且让它相信Electron就是Chrome浏览器。
WebDriverJs 是一个可以配合WebDriver做测试的node模块,我们会用它来做个演示。
记住9515
这个端口号,我们后面会用到
2. 安装WebDriverJS
$ npm install selenium-webdriver
在Electron下使用selenium-webdriver
和其平时的用法并没有大的差异,只是你需要手动设置连接ChromeDriver,以及Electron的路径:
WebdriverIO也是一个配合WebDriver用来测试的node模块
1. 启动ChromeDriver
$ chromedriver --url-base=wd/hub --port=9515
Only local connections are allowed.
记住9515
端口,后面会用到
3. 连接到ChromeDriver
const webdriverio = require('webdriverio');
var options = {
port: 9515, // "9515"是ChromeDriver使用的端口
desiredCapabilities: {
browserName: 'chrome',
chromeOptions: {
binary: '/Path-to-Your-App/electron', // Electron的路径
args: [/* cli arguments */] // 可选参数,类似:'app=' + /path/to/your/app/
}
};
var client = webdriverio.remote(options);
client
.init()
.url('http://google.com')
.setValue('#q', 'webdriverio')
.click('#btnG')
.getTitle().then(function(title) {
console.log('Title was: ' + title);
.end();
无需重新编译Electron,只要把app的源码放到Electron的资源目录里就可直接开始测试了。
当然,你也可以在运行Electron时传入参数指定你app的所在文件夹。这步可以免去你拷贝-粘贴你的app到Electron的资源目录。