jeudi 25 octobre 2018

How to make and run distributed tests in appium + node.js for Android

I am pleased to welcome you, my friend! Unfortunately, I have a problem that needs to be solved - this is very important for me and any information that will concern this topic for price! Thanks in advance!

I recently met UI testing. At the moment I have written a project that runs testing under the Android platform. Technology stack that I use: appium, nodejs, webdriverio.

This is my main file - 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": {}
}

The tests run and work very well, but I have another task.

It is necessary to start testing in such a way that some part of the tests are performed on one device, and another part is appropriate on another device. If I'm not mistaken, then this is called "distributed tests."

The scheme of such testing:

  1. When test #1 is launched, it starts working on one device.
  2. Behind him runs the test #2., but on another device.
  3. When test # 1 is completed, the next test is taken, if it is not started, test # 3 is launched, otherwise the next one.
  4. etc.

Unfortunately, I do not understand how it is possible to achieve a solution to this problem within the framework of the used technology stack, which I described above. Perhaps there is a source of information that I could not find ... I really need your help on how to release the launch of distributed tests in the appium and node.js... Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire