Search code examples
javascriptandroidmobilemocha.jsappium

Appium, Mocha, Android, JS - Is it possible to pass settings via command line (like version of android) when run tests?


I'm very new in mobile testing and I have several problems and questions.

Problem 1: I have several real devices on which I run tests. It's a little bit annoying to change Android version in conf file in capabilities every time I switch between devices.

Question 1: I want to pass version of Android when I run the test. When I write tests on Jasmine + Protractor, I could configure this in conf file like this:

switch (browser.params.env) {
    case 'case1':
      browser.params.url = 'url1';
      break;
    case 'case2':
      browser.params.url = 'url2';
      break;
    }

and run tests like this:

npm run test -- --browser.params.env=case1

Is it possible to do the same in Appium+Mocha? I was trying to use --default-capabilities flag, but it's not what I need.

Problem 2: I want to set username and password as environment variables (found an example here), but I don't understend how to do it correctly in Appium? At now users data store in separate file like this:

const users = [
  {
    id: 'someId1',
    pass: 'somePass1'
  }
]

But I don't think it's a good idea to store pass in tests. So how to use these environment variables?

Many thanks for help.


Solution

  • You can just create file like caps.js in config folder to store capabilities matrix:

    const caps = {}
    
    caps.android6 = {
      platformName: 'Android',
      automationName: 'UiAutomator2',
      deviceName: <device name>,
      appWaitActivity: <activity name>,
      appWaitPackage: <package name>,
      androidInstallTimeout: 90000,
      app: <path to app>
    }
    
    caps.android5 = {
      platformName: 'Android',
      platformVersion: '5.0',
      automationName: 'UiAutomator2',
      ...
    }
    
    caps.default = {...}
    
    
    module.exports = caps
    

    if you are using wd.js, you can create driver following way:

    const path = require('path')
    const caps = require(path.resolve('config','caps'))
    const wd = require('wd')
    const server = {host: 'localhost', port: 4723}
    
    const capabilities = caps[
      process.env('caps') !== 'undefined'
        ? process.env['caps']
        : 'default'
    ]
    const driver = wd.promiseChainRemote(server)
    await driver.init(capabilities)
    

    And when you run tests, you can do it like export caps=android6 && npm run tests, where tests is a script you specified in package.json to run your mocha tests. In this case android6 data from caps.js is used.

    Same approach you can use for providing credentials:

    export [email protected] password=124356 && npm run tests
    

    and in your tests you can read it via process.env