我在Android Studio中使用Appium + WebDriver为Android模拟器运行一个简单的计算器测试。我手动启动了Appium Server和Android Emulator,然后使用TestNG.xml运行测试。
以下是我的示例WebDriver代码 -
package com.example.user.mysampleapp2;
/**
* Created by CParmar on 14-09-2017.
*/
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.remote.MobilePlatform;
public class SampleTests {
public WebDriver driver;
@BeforeClass
public void setUp() throws MalformedURLException {
// Created object of DesiredCapabilities class.
DesiredCapabilities capabilities = new DesiredCapabilities();
// Set android deviceName desired capability. Set your device name.
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Nexus_6_API_25");
// Set BROWSER_NAME desired capability. It's Android in our case here.
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Android");
// Set android VERSION desired capability. Set your mobile device's OS version.
capabilities.setCapability(MobileCapabilityType.VERSION, "7.1.1");
// Set android platformName desired capability. It's Android in our case here.
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
// Set android appPackage desired capability. It is
// com.android.calculator2 for calculator application.
// Set your application's appPackage if you are using any other app.
capabilities.setCapability("appPackage", "com.android.calculator2");
// Set android appActivity desired capability. It is
// com.android.calculator2.Calculator for calculator application.
// Set your application's appPackage if you are using any other app.
capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
// Created object of RemoteWebDriver will all set capabilities.
// Set appium server address and port number in URL string.
// It will launch calculator app in android device.
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void testFirstCalculator() {
// Click on DELETE/CLR button to clear result text box before running test.
driver.findElements(By.xpath("//android.widget.Button")).get(0).click();
// Click on number 2 button.
driver.findElement(By.name("7")).click();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@AfterClass
public void End() {
driver.quit();
}
}
当我从Android Studio运行时,我得到了Appium Error。
使用以下命令启动Appium服务器:C:\ Program Files(x86)\ Appium \ node.exe lib \ server \ main.js --address 127.0.0.1 --port 4723 --platform-name Android --platform-版本23 --automation-name Appium --log-no-color info:欢迎来到Appium v1.4.16(REV ae6877eff263066b26328d457bd285c0cc62430d) info:Appium REST http接口监听器在127.0.0.1:4723上启动 info:[debug]非默认服务器args:{"地址":" 127.0.0.1"," logNoColors":true," platformName&# 34;:" Android和#34;" platformVersion":" 23"" automationName":" Appium"} info:Console LogLevel:debug 信息: - > POST / wd / hub / session {" desiredCapabilities":{" appPackage":" com.android.calculator2"," appActivity":& #34; com.android.calculator2.Calculator"" browserName":" Android和#34;" platformName":" Android和#34 ;, " DEVICENAME":" Nexus_6_API_25""版本":" 7.1.1"}" requiredCapabilities":{} "功能" {" desiredCapabilities" {" appPackage":" com.android.calculator2"" appActivity&#34 ;:" com.android.calculator2.Calculator"" browserName":" Android和#34;" platformName":"的Android&# 34;," DEVICENAME":" Nexus_6_API_25""版本":" 7.1.1"}" requiredCapabilities" :{}," alwaysMatch" {" browserName":" Android和#34;" platformName":" Android和#34;} " firstMatch":[]}} info:客户端用户代理字符串:Apache-HttpClient / 4.5.3(Java / 1.8.0_131) info:[debug]提供了以下所需功能,但appium无法识别。它们将被传递给在此服务器上运行的任何其他服务。 :版本 信息:[调试]没有获得应用但确实获得Android包,将尝试在设备上启动它 info:[debug]创建新的appium会话42818f6f-0677-486a-bc53-efa31009bbe7 info:启动android appium info:[debug]获取Java版本 info:Java版本是:1.8.0_131 info:[debug]检查adb是否存在 警告:ANDROID_HOME环境变量未设置为Android SDK根目录路径。与SDK 23+兼容时需要ANDROID_HOME。检查PATH的adb。 info:[debug]执行cmd:adb info:[debug]使用来自C:\ Users \ CParmar \ AppData \ Local \ Android \ sdk \ platform-tools \ adb.exe的adb
警告:没有应用功能,无法解析包/活动 info:[debug]使用快速重置?真正 info:[debug]准备会话设备 info:[debug]不检查app是否存在,因为我们假设它已经在设备上 info:检索设备 info:[debug]试图找到一个连接的Android设备 info:[debug]获取连接设备... info:[debug]执行cmd:" C:\ Users \ CParmar \ AppData \ Local \ Android \ sdk \ platform-tools \ adb.exe"设备 info:[debug] 1个设备已连接 info:找到设备模拟器-5554 info:[debug]将设备ID设置为emulator-5554 info:[debug]等待设备准备就绪并响应shell命令(timeout = 5) info:[debug]执行cmd:" C:\ Users \ CParmar \ AppData \ Local \ Android \ sdk \ platform-tools \ adb.exe" -s emulator-5554等待设备 info:[debug]执行cmd:" C:\ Users \ CParmar \ AppData \ Local \ Android \ sdk \ platform-tools \ adb.exe" -s emulator-5554 shell" echo' ready'" info:[debug]启动logcat捕获 错误:Logcat捕获失败:生成" C:\ Users \ CParmar \ AppData \ Local \ Android \ sdk \ platform-tools \ adb.exe" ENOENT info:[debug]停止logcat捕获
我已浏览论坛上的所有可用帖子,并在Android Studio运行配置(Android_Home)中使用SDK路径设置ANDROID_HOME变量。我仍然收到这个错误。
任何帮助?
谢谢, Chandresh Parmar
答案 0 :(得分:1)
您可以按照这些answers来修复正在设置的ANDROID_HOME
env变量。
在此期间,我建议安装appium-doctor并检查在appium测试执行之前环境设置中缺少的内容。
在你的情况下:
npm install appium-doctor -g
appium-doctor --android
你期望获得的回应是:
### Diagnostic starting ###
✔ The Node.js binary was found at: /usr/local/bin/node
✔ Node version is 7.9.0
✔ ANDROID_HOME is set to: /Users/<user>/Library/Android/sdk
✔ JAVA_HOME is set to: /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
✔ adb exists at: /Users/<user>/Library/Android/sdk/platform-tools/adb
✔ android exists at: /Users/<user>/Library/Android/sdk/tools/android
✔ emulator exists at: /Users/<user>/Library/Android/sdk/tools/emulator
✔ Bin directory of $JAVA_HOME is set
### Diagnostic completed, no fix needed. ###
修复所有x
标记并祝你好运
答案 1 :(得分:0)
从错误日志中,您可以清楚地看到,未设置 ANDROID_HOME 环境变量。
例如:您已在&#34; C:\ Users \ YourUserName \ AndroidSDK&#34;中安装了Android SDK。那么ANDROID_HOME路径的值将是相同的路径。
只需添加名为ANDROID_HOME的新环境变量,并将值作为路径。
而且你的appium版本很旧(1.4.16)。我建议你把它升级到1.6.3,这是现在非常稳定的appium版本