使用Appium Vega驱动程序编写测试
Appium Vega驱动程序支持在Fire TV Stick上自动测试应用。本页介绍如何使用Python、Java或JavaScript创建自动测试。
先决条件
- 完成使用Appium设置测试中的步骤。
- 查看管理Appium会话以了解功能配置。
- 
    确认Appium服务器是否在默认端口4723上运行: curl http://localhost:4723/status
- 拥有安装目标应用的支持Vega的Fire TV设备或Vega虚拟设备。
编写您的测试脚本
- 选择您首选的集成开发环境和编程语言。
- 
    创建包含以下代码片段的新文件: Python: from appium import webdriver from appium.options.common import AppiumOptions bridge = "vda" desired_caps = { "platformName": "Vega", "appium:automationName": "automation-toolkit/JSON-RPC", "browserName": "", "kepler:device": f"{bridge}://default" # 或者,将“default”替换为设备序列号 (DSN) } desired_caps = AppiumOptions().load_capabilities(desired_caps) appium_url = "http://127.0.0.1:4723" def appium_session(): driver = webdriver.Remote(appium_url, options=desired_caps) try: output = driver.execute_script("shell", 'echo "Hello World!"') if "Hello" in output: print(f"执行脚本命令运行成功:\n{output}") else: print("发生了错误") finally: driver.quit() if __name__ == '__main__': appium_session()JavaScript: const {remote} = require('webdriverio'); const capabilities = { platformName: 'Vega', 'appium:automationName': 'automation-toolkit/JSON-RPC', 'kepler:device': 'vda://default', 'appium:appURL': 'com.amazon.keplervideoapp.main', }; const opts = { hostname: 'localhost', port: 4723, logLevel: 'info', capabilities, }; const test = async function runTest() { const driver = await remote(opts); try { const element = await driver.$('//children[@test_id="card169309"'); // getElementRect命令 const rect = await driver.getElementRect(element.elementId); console.log(`x: ${rect.x}, y: ${rect.y}, width: ${rect.width}, height: ${rect.height}`); } finally { await driver.deleteSession(); } } test().catch(console.error);Java: Java需要编译和依赖项管理。使用Maven或Gradle自动处理这些要求。 使用Maven设置项目 - 
        检查是否安装了 Maven。mvn --version
- 
        在您的项目根目录下创建一个 pom.xml。<project> <modelVersion>4.0.0</modelVersion> <groupId>com.amazon.akd</groupId> <artifactId>kepler-test</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>8.5.1</version> </dependency> </dependencies> </project>
- 
        创建测试类。参见文本类。 
 使用Gradle设置项目 - 
        验证 Gradle安装:gradle --version
- 
        在项目根目录中创建 build.gradle:plugins { id 'java' id 'application' } repositories { mavenCentral() } dependencies { implementation 'io.appium:java-client:8.5.1' } application { mainClass = 'com.amazon.akd.commands.page_and_source.ExecuteScriptExample' }
- 
        创建 settings.gradle。rootProject.name = 'kepler-test'
- 
        创建测试类。参见文本类。 
 
- 
        
测试类
package com.amazon.akd.commands.page_and_source;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.remote.options.BaseOptions;
import java.net.MalformedURLException;
import java.net.URL;
public class ExecuteScriptExample {
	public static void main(String[] args) throws MalformedURLException {
	    BaseOptions options = new BaseOptions()
	            .setPlatformName("Vega")
	            .setAutomationName("automation-toolkit/JSON-RPC")
	            .amend("kepler:device", "vda://default")
	            .amend("appium:appURL", "com.amazon.keplervideoapp.main");
	    AppiumDriver driver = new AppiumDriver(
	            new URL("http://127.0.0.1:4723"), options
	    );
	    try {
	        // execute_script命令
	        String output = driver.executeScript("shell", "echo 'Hello World!'").toString();
	        System.out.println(output);
	    } finally {
	        driver.quit();
	    }
	}
}
运行您的测试脚本
您可以使用首选的测试框架和工具运行测试脚本。
- 打开命令提示符。
- 导航到您的测试文件位置。
- 运行测试。
Python:
python test.py
输出示例:
$ python test.py
Execute script command run was successful:
Hello World!
JavaScript:
node test.js
输出示例:
$ node test.js
{
  x: 150,
  y: 200,
  width: 300,
  height: 400
}
Java
使用Maven:
mvn clean install
使用Gradle:
./gradlew run
输出示例:
# 使用Maven
$ mvn clean install
[INFO] Scanning for projects...
[INFO] Building kepler-test 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
...
Hello World!
# 使用Gradle
$ ./gradlew run
> Task :run
Hello World!
BUILD SUCCESSFUL in 3s
Last updated: 2025年9月30日

