You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Appium+Mocha+Android+JS:命令行传配置与环境变量使用咨询

Hey there, let's tackle your two Appium issues one by one—they're both super common for folks new to mobile testing, so you're in the right place!

1. Passing Android Version via Command Line for Multi-Device Testing

I get why --default-capabilities might not cut it here—it's more for overriding default settings rather than dynamic per-run values. Here are two reliable ways to pass the Android version via command line:

Option 1: Parse Custom Command-Line Flags

If you're using JavaScript/TypeScript, you can read directly from process.argv to grab the version you want:

  • First, run your tests with a custom flag:
    npm test -- --android-version 13
    
  • Then in your test config file (like wdio.conf.js or a standalone Appium setup script), parse the argument:
    // Pull command line arguments
    const args = process.argv.slice(2);
    const versionArg = args.find(arg => arg.startsWith('--android-version'));
    // Fallback to a default version if no flag is provided
    const androidVersion = versionArg ? versionArg.split('=')[1] : '12';
    
    // Inject into your capabilities
    const capabilities = {
      platformName: 'Android',
      platformVersion: androidVersion,
      deviceName: 'Your Device',
      appPackage: 'com.your.app',
      appActivity: '.MainActivity'
      // Add other required capabilities
    };
    

Option 2: Use Environment Variables (Simpler Alternative)

This avoids manual flag parsing entirely. Just set the version as an env var before running tests:

  • Linux/macOS:
    ANDROID_VERSION=13 npm test
    
  • Windows (Command Prompt):
    set ANDROID_VERSION=13 && npm test
    
  • Windows (PowerShell):
    $env:ANDROID_VERSION="13"; npm test
    

Then in your config, reference the env var directly:

const capabilities = {
  platformName: 'Android',
  platformVersion: process.env.ANDROID_VERSION || '12', // Default to 12 if not set
  // Rest of your capabilities
};

2. Storing Credentials as Environment Variables

Storing usernames/passwords in test files is a big no-no for security—great call wanting to fix that! Here's how to set it up properly:

Step 1: Set the Environment Variables

First, define the vars in your terminal (or make them permanent by adding to your shell config):

  • Linux/macOS:
    export TEST_USER="your-actual-username"
    export TEST_PASS="your-actual-password"
    
  • Windows (Command Prompt):
    set TEST_USER=your-actual-username
    set TEST_PASS=your-actual-password
    
  • Windows (PowerShell):
    $env:TEST_USER="your-actual-username"
    $env:TEST_PASS="your-actual-password"
    

Step 2: Access Them in Your Tests

Now pull the vars into your test code instead of hardcoding:

JavaScript/TypeScript Example:

const username = process.env.TEST_USER;
const password = process.env.TEST_PASS;

// Use them in your login flow
await driver.findElement(By.id('username-field')).sendKeys(username);
await driver.findElement(By.id('password-field')).sendKeys(password);

Python Example:

import os

username = os.environ.get('TEST_USER')
password = os.environ.get('TEST_PASS')

// Login action
driver.find_element(By.ID, 'username-field').send_keys(username)
driver.find_element(By.ID, 'password-field').send_keys(password)

Pro Tip: Use a .env File for Local Development

If you don't want to type the env vars every time, use a .env file with a helper package:

  1. Create a .env file in your project root:
    TEST_USER=your-actual-username
    TEST_PASS=your-actual-password
    
  2. Critical: Add .env to your .gitignore so you never commit sensitive data!
  3. Load the file in your test setup:
    • For JS: Install dotenv with npm install dotenv, then add at the top of your config:
      require('dotenv').config();
      
    • For Python: Install python-dotenv with pip install python-dotenv, then add:
      from dotenv import load_dotenv
      load_dotenv()
      

内容的提问来源于stack exchange,提问作者Elena

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:56:21