如何以编程方式启动 Appium server 2.0
Posted
技术标签:
【中文标题】如何以编程方式启动 Appium server 2.0【英文标题】:How to start Appium server 2.0 Programmatically 【发布时间】:2021-11-20 08:12:15 【问题描述】:我知道以编程方式启动 Appium 1.x,但无法为 Appium 2.0 启动
AppiumServiceBuilder builder = new AppiumServiceBuilder();
builder.usingAnyFreePort();
builder.usingDriverExecutable(new File("<path to Node>\\node.exe"));
builder.withAppiumJS(new File("<path to Appium>\\appium"));
HashMap<String, String> environment = new HashMap();
environment.put("PATH", "/usr/local/bin:" + System.getenv("PATH"));
builder.withEnvironment(environment);
service = AppiumDriverLocalService.buildService(builder);
service.start();
【问题讨论】:
【参考方案1】:使用以下命令安装 appium 2.0
npm install -g appium@next
确保系统环境变量中有 nodejs 的路径 在用户变量中添加新的键 NODE_PATH 和 APPIUM_PATH
对于 NODE_PATH 将值设置为 C:\Program Files\nodejs\node.exe
对于 APPIUM_PATH 将值设置为 C:\Users
您将下面的行添加到上面的代码中 builder = new AppiumServiceBuilder().withArgument(() -> "--base-path", "/wd/hub").withArgument(() -> "--plugins", "images");
这是完整的代码
AppiumServiceBuilder builder;
builder = new AppiumServiceBuilder().withArgument(() -> "--base-path", "/wd/hub").withArgument(() -> "--plugins", "images");
builder.usingAnyFreePort();
builder.usingDriverExecutable(new File("C:\\Program Files\\nodejs\\node.exe"));
builder.withAppiumJS(new File("C:\\Users\\<user_name\\AppData\\Roaming\\npm\\node_modules\\appium"));
HashMap<String, String> environment = new HashMap();
environment.put("PATH", "/usr/local/bin:" + System.getenv("PATH"));
builder.withEnvironment(environment);
service = AppiumDriverLocalService.buildService(builder);
service.start();
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("appium:platformName", "android");
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("appium:automationName", "UiAutomator2");
capabilities.setCapability("app", "path_to_app\\app\\Learn_Android_With_Source_Code.apk");
androidDriver = new AndroidDriver<AndroidElement>(service.getUrl(), capabilities);
androidDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
return androidDriver;
【讨论】:
以上是关于如何以编程方式启动 Appium server 2.0的主要内容,如果未能解决你的问题,请参考以下文章
Appium:如何在开始测试运行 appium-testng 之前使用 java 在 aws 设备场设备上以编程方式创建 google 帐户
如何以编程方式为 AWS-Device Farm 设置 Appium --no-reset 标志