| -- README.md
| -- macaca-test
| | -- lib
| | -- webdriver-client.js
| | -- macaca-pc.test.js
| | -- macaca-mobile.test.js
| -- package.json
devDependencies
{
"name": "macaca-test-sample",
"version": "0.1.0",
"description": "macaca-test-sample",
"keywords": [
"sample"
],
"dependencies": {
},
"devDependencies": {
"wd-next": "*",
"macaca-chromedriver": "*",
"wd": "*"
},
"author": "your name",
"email": "your email",
"license": "MIT"
}
cnpm i
安装 node 模块A. PC(跳到 1)
B. iOS/Android(跳到 2)
// 文件 macaca-pc.test.js
var path = require('path');
var wd = require('wd-next'); // 引用 wd-next
var macacachromedriver = require('macaca-chromedriver'); // 引用 macaca-chromedriver
module.exports = (options) => {
var binPath = macacachromedriver.binPath;
process.env.PATH = `${process.env.PATH}:${path.dirname(binPath)}`;
return wd;
};
wd.initPromiseChain()
,之后 it
中的所有用例的方法都是使用 promise 链式调用方法// 文件 macaca-pc.test.js
var path = require('path');
var wd = require('wd-next'); // 引用 wd-next
var macacachromedriver = require('macaca-chromedriver'); // 引用 macaca-chromedriver
module.exports = (options) => {
var binPath = macacachromedriver.binPath;
process.env.PATH = `${process.env.PATH}:${path.dirname(binPath)}`;
/*
* 开始编写测试用例
*/
describe('macaca desktop sample', function () {
this.timeout(5 * 60 * 1000);
const driver = wd.initPromiseChain(); // 初始化一个promise
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();
});
});
return wd;
};
macaca run
Bravo! 就可以看到啦~ iOS 代码实现如下:
module.exports = function(username, password) {
return this
.waitForElementByXPath('//UIATextField[1]')
.sendKeys(username)
.waitForElementByXPath('//UIASecureTextField[1]')
.sendKeys(password)
.sleep(1000)
.sendKeys('\n')
.waitForElementByName('Login')
.click()
.sleep(5000);
};
但是由于 Android 下元素名会不同,实现如下:
module.exports = function(username, password) {
return this
.waitForElementsByClassName('android.widget.EditText', {}, 120000)
.then(function(els) {
return els[0];
})
.sendKeys(username)
.sleep(1000)
.elementsByClassName('android.widget.EditText')
.then(function(els) {
return els[1];
})
.sendKeys(password)
.sleep(1000)
.waitForElementByName('Login')
.click()
.sleep(5000);
};
可以查看我们已经写好的例子。
注意!这不是通用的 webdriver-client ,你需要根据自己的 APP 封装出自己的 webdriver-client。
// 这里是 wd 实例初始化
var wd = require('./lib/webdriver-client')({
platformName: platform,
app: 'path/to/your/app'
});
describe('macaca mobile sample', function() {
this.timeout(5 * 60 * 1000);
var driver = wd.initPromiseChain();
before(function() {
return driver.initDriver();
});
after(function() {
return driver
.sleep(1000)
.quit();
});
it('#1 should login success', function() {
return driver
.login('12345678', '111111')
.sleep(1000);
});
...
});
macaca run --server
用例就可以跑起来了。iOS 效果 https://testerhome.com/topics/4359