最近有很多朋友对 Macaca 做 PC 端测试有很多兴趣,也有很多疑问点,下面简单介绍下。
用例写起来比较好理解:
describe('macaca desktop sample', function() {
this.timeout(5 * 60 * 1000);
const driver = wd.initPromiseChain();
const initialURL = 'https://www.baidu.com';
before((done) => {
return driver
.init()
.setNormalSize()
.end(done);
});
it('#0 should go into macaca', function() {
return driver
.get(initialURL)
.sleep(3000);
});
it('#1 should works with macaca', function() {
return driver
.elementById('kw')
.sendKeys('macaca')
.sleep(3000)
.elementById('su')
.click()
.sleep(5000)
.source()
.then(function(html) {
html.should.containEql('macaca');
})
.takeScreenshot();
});
it('#2 should go into web', function() {
return driver
.get(initialURL)
.sleep(3000);
});
it('#3 should works with web', function() {
return driver
.elementById('kw')
.sendKeys('TesterHome')
.sleep(3000)
.elementById('su')
.click()
.sleep(5000)
.source()
.then(function(html) {
html.should.containEql('TesterHome');
})
.takeScreenshot();
});
after((done) => {
return driver
.quit(done);
});
});
测试用例源码,看过之前文章的同学对这个例子应该不陌生了,欢迎下载到本地试验,实践会理解得更全面。
由于网络原因,下载所依赖的chromedriver
或者 electron
驱动 会有问题,使用如下方式解决。
export CHROMEDRIVER_CDNURL=http://npm.taobao.org/mirrors/chromedriver/
export ELECTRON_MIRROR=https://npm.taobao.org/mirrors/electron/
然后运行下面的命令即可:
$ make test-pc
运行的效果如下:
本示例使用了 Google 的 Chromedriver 作为驱动,见 macaca-chromedriver。
更多 API 相关的文档请见: macaca/guide
通过对 client 端的扩展,可以实现其他浏览器的测试,欢迎动手试试。
后面会分享 headless webkit
的方案,选择 phantomjs, electron
之一进行介绍。另外,PC 与 mobile 端的持续集成方案是一致的,见搭建自己的自动化测试平台,当然,也可以使用其他持续集成系统。
有任何疑问请提 issue macaca/support。
欢迎讨论,互相学习。
微博: http://weibo.com/xudafeng
Github: https://github.com/xudafeng
弱弱的问一句,使用 make test-pc 提示 make 不是内部命令,是什么原因啊,
#1 楼 @lemon2limeng 你好,这是个示例而已,可以看 Makefile 中的指定,是一行 sh 命令
您好,有个问题一直没解决。。我执行 make test-pc 后,会出现"macaca run .....",但不弹出浏览器窗口,是什么原因呢?
PS:我是 Windows 操作系统,clone 了 git 代码后,安装了 make、npm install 了依赖、export 了 chromedriver 和 electron 驱动
你的例子的驱动换成 Electron 了啊,是不是应该把文章改一下?
我执行 make test-pc 后一直停留在 loading....那里,请问如何解决
-
7 楼 @xdf 非常感谢,确实是需要加这两个地址,不过地址后边应该加/,否则会找不到文件
export CHROMEDRIVER_CDNURL=http://npm.taobao.org/mirrors/chromedriver/
export ELECTRON_MIRROR=https://npm.taobao.org/mirrors/electron/
#3 楼 @Alison2012win 您好,请问 windows 下 macaca 浏览器不谈出窗口问题解决了么?目前我这边也遇到同样问题,谢谢。
#9 楼 @xiaoweizhuangren 请升级最新 cli
学习了。
@xdf 环境配置好了以后,启动 macaca run --verbose 模拟器多次闪退,并且不断重启,最后能执行成功官方例子。我把我的配置发出来
@diao2007 make test-pc
您好,执行 make test-pc 提示如下:
macaca run --verbose -d ./macaca-test/macaca-desktop-sample.test.js
index.js:17:12 [master] pid:2631 webdriver server start with config:
{ port: 3456,
window: true,
ip: '192.168.1.5',
host: 'chengxuengdembp',
loaded_time: '2016-06-27 21:05:51' }
middlewares.js:17:10 [master] pid:2631 base middlewares attached
router.js:105:10 [master] pid:2631 router set
webdriver sdk launched
/usr/local/lib/node_modules/macaca-cli/node_modules/mocha/lib/utils.js:628
throw new Error("cannot resolve path (or pattern) '" + path + "'");
^
Error: cannot resolve path (or pattern) './macaca-test/macaca-desktop-sample.test.js'
at Object.lookupFiles (/usr/local/lib/node_modules/macaca-cli/node_modules/mocha/lib/utils.js:628:15)
at /usr/local/lib/node_modules/macaca-cli/node_modules/mocha/bin/_mocha:326:30
at Array.forEach (native)
at Object. (/usr/local/lib/node_modules/macaca-cli/node_modules/mocha/bin/_mocha:325:6)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
Test completed!
make: *** [test-pc] Error 1
cannot resolve path (or pattern) '" + path + "'"); the 个 path 是指什么的 path 呀
你好 @xdf
我下载了 macaca-test-sample-master.zip 样例 但是执行的时候是如此。
D:\macaca\macaca-test-sample-master\macaca-test 的目录
2016/07/13 23:16
.D:\macaca\macaca-test-sample-master\macaca-test>platform=android macaca run --verbose -d macaca-desktop-sample.test.js
'platform' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
D:\macaca\macaca-test-sample-master\macaca-test>macaca run --verbose -d macaca-desktop-sample.test.js
index.js:17:12 [master] pid:14388 webdriver server start with config:
{ port: 3456,
window: true,
ip: '169.254.166.89',
host: 'cmd-PC',
loaded_time: '2016-07-19 11:33:19' }
middlewares.js:17:10 [master] pid:14388 base middlewares attached
router.js:105:10 [master] pid:14388 router set
webdriver sdk launchedmacaca desktop sample
responseHandler.js:11:12 [master] pid:14388 Recieve HTTP Request from Client: method: POST url: /wd/hub/session, jsonBody: {"desiredCapabilities":{"autoAcceptAlerts":true,"browserName":"chrome","platformName":"desktop","version":"","javascriptEnabled":true,"platform":"ANY"}}
session.js:47:10 [master] pid:14388 Creating session, sessionId: 50828aa2-0d56-47f5-8237-5efc1a7907dc.
session.js:31:14 [master] pid:14388 Error: Cannot find module 'macaca-chrome'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at detectDevice (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\webdriver-server\lib\server\controllers\session.js:28:20)
at Object.createDevice (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\webdriver-server\lib\server\controllers\session.js:10:18)
at next (native)
at onFulfilled (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:65:19)
at D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:54:5
at Object.co (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:50:10)
please run:macaca install chrome
responseHandler.js:50:12 [master] pid:14388 Send Error Respone to Client: TypeError: Cannot set property 'proxyMode' of undefined
responseHandler.js:56:14 [master] pid:14388 TypeError: Cannot set property 'proxyMode' of undefined
at Object.createDevice (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\webdriver-server\lib\server\controllers\session.js:16:20)
at next (native)
at onFulfilled (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:65:19)
at D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:54:5
at Object.co (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:50:10)
at Object.toPromise (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:118:63)
at next (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:99:29)
at onFulfilled (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:69:7)
at D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:54:5
at Object.co (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:50:10)
TypeError: Cannot set property 'proxyMode' of undefined
at Object.createDevice (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\webdriver-server\lib\server\controllers\session.js:16:20)
at next (native)
at onFulfilled (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:65:19)
at D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:54:5
at Object.co (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:50:10)
at Object.toPromise (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:118:63)
at next (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:99:29)
at onFulfilled (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:69:7)
at D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:54:5
at Object.co (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:50:10)
1) "before all" hook
responseHandler.js:11:12 [master] pid:14388 Recieve HTTP Request from Client: method: DELETE url: /wd/hub/session, jsonBody: {}
responseHandler.js:19:14 [master] pid:14388 Send HTTP Respone to Client: 405 Method Not Allowed
2) "after all" hook0 passing (361ms)
2 failing
1) macaca desktop sample "before all" hook:
Error: [init({"autoAcceptAlerts":true,"browserName":"chrome","platformName":"desktop"})] The environment you requested was unavailable.
at D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\wd\lib\webdriver.js:134:15
at Request._callback (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\wd\lib\http-utils.js:87:7)
at Request.self.callback (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\node_modules\request\request.js:368:22)
at Request. (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\node_modules\request\request.js:1219:14)
at IncomingMessage. (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\node_modules\request\request.js:1167:12)
at endReadableNT (_stream_readable.js:913:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
2) macaca desktop sample "after all" hook:
Error: [quit()] Unexpected data in simpleCallback.
at exports.newError (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\wd\lib\utils.js:139:13)
at null. (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\wd\lib\callbacks.js:42:12)
at cb (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\wd\lib\webdriver.js:165:22)
at D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\wd\lib\webdriver.js:179:5
at Request._callback (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\wd\lib\http-utils.js:87:7)
at Request.self.callback (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\node_modules\request\request.js:368:22)
at Request. (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\node_modules\request\request.js:1219:14)
at IncomingMessage. (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\node_modules\request\request.js:1167:12)
at endReadableNT (_stream_readable.js:913:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
Test completed!
#3 楼 @alison2012win 你好 windonws 下 gitbash 也就是 MINGW64 命令行窗口下,怎么安装 make 命令呢?
@harsayer 直接 macaca run
#19 楼 @xdf
cmd@cmd-PC MINGW64 /d/macaca/macaca-test-sample-master/macaca-test
$ macaca run
webdriver sdk launched
D:\nodejs\node-global\node_modules\macaca-cli\node_modules\mocha\lib\utils.js:628
throw new Error("cannot resolve path (or pattern) '" + path + "'");
^
Error: cannot resolve path (or pattern) 'macaca-test'
at Object.lookupFiles (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\mocha\lib\utils.js:628:15)
at D:\nodejs\node-global\node_modules\macaca-cli\node_modules\mocha\bin_mocha:326:30
at Array.forEach (native)
at Object. (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\mocha\bin_mocha:325:6)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
Test completed!
@harsayer macaca run -d ./macaca-test/macaca-desktop-sample.test.js
https://github.com/macacajs/macaca-test-sample/blob/master/Makefile#L33
#21 楼 @xdf
cmd@cmd-PC MINGW64 /d/macaca/macaca-test-sample-master/macaca-test
$ macaca run -d /d/macaca/macaca-test-sample-master/macaca-test/macaca-desktop-sample.test.js --verbose
index.js:17:12 [master] pid:6560 webdriver server start with config:
{ port: 3456,
window: true,
ip: '169.254.166.89',
host: 'cmd-PC',
loaded_time: '2016-07-21 10:15:07' }
middlewares.js:17:10 [master] pid:6560 base middlewares attached
router.js:108:10 [master] pid:6560 router set
webdriver sdk launchedmacaca desktop sample
responseHandler.js:11:12 [master] pid:6560 Recieve HTTP Request from Client: method: POST url: /wd/hub/session, jsonBody: {"desiredCapabilities":{"autoAcceptAlerts":true,"browserName":"chrome","platformName":"desktop","version":"","javascriptEnabled":true,"platform":"ANY"}}
session.js:47:10 [master] pid:6560 Creating session, sessionId: 769ed0d9-e4bb-48ef-80ec-a1f5335a318d.
session.js:31:14 [master] pid:6560 Error: Cannot find module 'macaca-chrome'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at detectDevice (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\webdriver-server\lib\server\controllers\session.js:28:20)
at Object.createDevice (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\webdriver-server\lib\server\controllers\session.js:10:18)
at next (native)
at onFulfilled (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:65:19)
at D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:54:5
at Object.co (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:50:10)
please run:macaca install chrome
responseHandler.js:50:12 [master] pid:6560 Send Error Respone to Client: TypeError: Cannot set property 'proxyMode' of undefined
responseHandler.js:56:14 [master] pid:6560 TypeError: Cannot set property 'proxyMode' of undefined
at Object.createDevice (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\webdriver-server\lib\server\controllers\session.js:16:20)
at next (native)
at onFulfilled (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:65:19)
at D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:54:5
at Object.co (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:50:10)
at Object.toPromise (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:118:63)
at next (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:99:29)
at onFulfilled (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:69:7)
at D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:54:5
at Object.co (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:50:10)
TypeError: Cannot set property 'proxyMode' of undefined
at Object.createDevice (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\webdriver-server\lib\server\controllers\session.js:16:20)
at next (native)
at onFulfilled (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:65:19)
at D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:54:5
at Object.co (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:50:10)
at Object.toPromise (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:118:63)
at next (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:99:29)
at onFulfilled (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:69:7)
at D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:54:5
at Object.co (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\co\index.js:50:10)
1) "before all" hook
responseHandler.js:11:12 [master] pid:6560 Recieve HTTP Request from Client: method: DELETE url: /wd/hub/session, jsonBody: {}
responseHandler.js:19:14 [master] pid:6560 Send HTTP Respone to Client: 405 Method Not Allowed
2) "after all" hook0 passing (124ms)
2 failing
1) macaca desktop sample "before all" hook:
Error: [init({"autoAcceptAlerts":true,"browserName":"chrome","platformName":"desktop"})] The environment you requested was unavailable.
at D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\wd\lib\webdriver.js:134:15
at Request._callback (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\wd\lib\http-utils.js:87:7)
at Request.self.callback (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\node_modules\request\request.js:368:22)
at Request. (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\node_modules\request\request.js:1219:14)
at IncomingMessage. (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\node_modules\request\request.js:1167:12)
at endReadableNT (_stream_readable.js:921:12)2) macaca desktop sample "after all" hook:
Error: [quit()] Unexpected data in simpleCallback.
at exports.newError (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\wd\lib\utils.js:139:13)
at null. (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\wd\lib\callbacks.js:42:12)
at cb (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\wd\lib\webdriver.js:165:22)
at D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\wd\lib\webdriver.js:179:5
at Request._callback (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\wd\lib\http-utils.js:87:7)
at Request.self.callback (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\node_modules\request\request.js:368:22)
at Request. (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\node_modules\request\request.js:1219:14)
at IncomingMessage. (D:\macaca\macaca-test-sample-master\node_modules\macaca-wd\node_modules\request\request.js:1167:12)
at endReadableNT (_stream_readable.js:921:12)Test completed!
@harsayer 看 log
npm i macaca-android -g
npm i macaca-chrome -g 也都安装了
cmd@cmd-PC MINGW64 /d/macaca/macaca-test-sample-master/macaca-test
$ macaca run -d /d/macaca/macaca-test-sample-master/macaca-test/macaca-desktop-sample.test.js --verbose
index.js:17:12 [master] pid:13924 webdriver server start with config:
{ port: 3456,
window: true,
ip: '169.254.166.89',
host: 'cmd-PC',
loaded_time: '2016-07-21 10:32:54' }
middlewares.js:17:10 [master] pid:13924 base middlewares attached
router.js:108:10 [master] pid:13924 router set
webdriver sdk launched
module.js:327
throw err;
^
Error: Cannot find module 'webdriver-client'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object. (D:\macaca\macaca-test-sample-master\macaca-test\macaca-desktop-sample.test.js:3:10)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at D:\nodejs\node-global\node_modules\macaca-cli\node_modules\mocha\lib\mocha.js:219:27
at Array.forEach (native)
at Mocha.loadFiles (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\mocha\lib\mocha.js:216:14)
at Mocha.run (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\mocha\lib\mocha.js:468:10)
at Object. (D:\nodejs\node-global\node_modules\macaca-cli\node_modules\mocha\bin_mocha:403:18)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
Test completed!
我看了下 macaca-desktop-sample.test.js 文件里是
var wd = require('webdriver-client')({
platformName: 'desktop',
browserName: 'electron'
我是否要改成 browserName: 'chrome'
同时改文件 D:\macaca\macaca-test-sample-master.travis.yml 内容为
language: node_js
sudo: required
node_js:
源码 404 了。
@sunkuan2007 好了,以后以官网为准
macaca 框架支持对 windows 系统中运行的 exe 进行测试吗?
Hi ,
I am getting error like this
Please help me to solve this
求助,pc 桌面版基于 electronjs 的桌面版应用的 UI 页面中,需要使用到鼠标右键的操作,试问鼠标右键的模拟操作 macaca 如何实现呢,api 中未发现,借问求分享哦