前置条件

| -- README.md
| -- macaca-test
|     | -- lib
|           | -- webdriver-client.js
|     | -- macaca-pc.test.js
|     | -- macaca-mobile.test.js  
| -- package.json
{
    "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"
}

你要测什么?

A. PC(跳到 1)
B. iOS/Android(跳到 2)

1. 桌面浏览器端测试

// 文件 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;
};

// 文件 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;
};

2. 移动端测试

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);
  });

  ...

});

最后

iOS 效果 https://testerhome.com/topics/4359

Android 效果 https://testerhome.com/topics/4442

PC 效果 https://testerhome.com/topics/4523


↙↙↙阅读原文可查看相关链接,并与作者交流