如何在Android的appium + node.js中进行和运行分布式测试

时间:2018-10-25 13:52:04

标签: android node.js testing webdriver appium

我很高兴欢迎您,我的朋友!不幸的是,我有一个需要解决的问题-这对我和与价格相关的所有信息都非常重要!预先感谢!

我最近遇到了UI测试。目前,我已经编写了一个在Android平台下运行测试的项目。我使用的技术堆栈:appium,nodejs,webdriverio。

这是我的主文件-index.js

const joinPath = require('./utils/utils').joinPath
const getDirContentRecursively = require('./utils/utils').getDirContentRecursively
const getFormattedDate = require('./utils/utils').getFormattedDate
const config = require('./config.json')

executeTests()

async function executeTests () {
  let args = process.argv
  if (!args.includes('android') && !args.includes('ios')) {
    await executeAndroid()
    await executeIos()
    return
  }
  if (args.includes('android')) await executeAndroid()
  if (args.includes('ios')) await executeIos()
}

async function executeAndroid () {
  console.log('Starting Android tests')
  let driver = getDriver(config.android.capabilities)
  let tests = getTests(config.android.testsDir)
  let reportDir = getReportDir(config.android.reportsDir)
  await executeTestsWithDriver(driver, tests, reportDir)
}

async function executeIos () {
  console.log('Starting iOS tests')
  let driver = getDriver(config.ios.capabilities)
  let tests = getTests(config.ios.testsDir)
  let reportDir = getReportDir(config.ios.reportsDir)
  await executeTestsWithDriver(driver, tests, reportDir)
}

function getDriver (caps) {
  const wdio = require('webdriverio')
  const driver = wdio.remote({
    protocol: config.appium.protocol,
    host: config.appium.host,
    port: config.appium.port,
    path: '/wd/hub',
    deprecationWarnings: false,
    desiredCapabilities: caps
  })
  addAdditionalCommands(driver)
  return driver
}

function addAdditionalCommands (driver) {
  const commandsDir = config.directories.commands
  getDirContentRecursively(commandsDir, '.js').forEach(file => {
    const addCommand = require(file)
    addCommand(driver)
  })
}

function getTests (platformTestsDir) {
  const tests = []

  getDirContentRecursively(config.directories.commonTests, '.js').forEach(filePath => tests.push(filePath))

  getDirContentRecursively(platformTestsDir, '.js').forEach(filePath => tests.push(filePath))
  return tests
}

function getReportDir (baseDir) {
  const date = getFormattedDate('dd-mm-yyyy_hh-MM-ss')
  return joinPath(__dirname, baseDir, date)
}

function executeTestsWithDriver (driver, tests, reportDir) {
  const runner = getTestRunner(reportDir)

  tests.forEach(path => {
    delete require.cache[path]
    runner.addFile(path)
  })

  global.reportDir = reportDir
  global.driver = driver
  global.account = require('./accounts/accounts.json')
  global.android_emulator = require('./emulators/android_emulators.json')

  return new Promise(resolve => runner.run(resolve))
}

function getTestRunner (reportDir) {
  const Mocha = require('mocha')
  const runner = new Mocha()

  runner.timeout(1800000)
  runner.reporter('mochawesome', {
    'reportTitle': 'Integration Test',
    'reportPageTitle': 'Integration Test',
    'reportDir': reportDir,
    'saveJson': true,
    'reportFilename': 'report'
  })
  return runner
}

package.json

{
  "name": "MyTesting",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "node index.js"
  },
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "dateformat": "^3.0.3",
    "eslint": "^5.7.0",
    "eslint-config-standard": "^12.0.0",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-node": "^7.0.1",
    "eslint-plugin-promise": "^4.0.1",
    "eslint-plugin-standard": "^4.0.0",
    "mkdirp": "^0.5.1",
    "mocha": "^5.2.0",
    "mocha-parallel-tests": "^2.0.5",
    "mochawesome": "^3.1.1",
    "node-fetch": "^2.2.0",
    "uuid": "^3.3.2",
    "webdriverio": "^4.14.0"
  },
  "dependencies": {}
}

测试运行正常,但我还有另一项任务。

必须以某种方式在一个设备上执行测试的一部分,而另一部分适用于另一台设备的方式开始测试。如果我没记错的话,那就叫做“分布式测试”。

这样的测试方案:

  1. 启动测试#1后,它会在一台设备上开始工作。
  2. 在他后面运行测试#2,但是在另一台设备上。
  3. 测试#1完成后,将进行下一个测试,如果尚未开始,则启动测试#3,否则将启动下一个测试。

不幸的是,我不了解如何在上述的二手技术堆栈框架内实现此问题的解决方案。也许有我找不到的信息源...我真的需要您的帮助,以帮助您如何在appium和node.js中发布分布式测试的启动...

0 个答案:

没有答案