无法使用 Appium + Android Studio 3.0 + Java 8 [英] Failed to use Appium + Android Studio 3.0 + Java 8
问题描述
我正在尝试使用 appium java 客户端进行我的 Android UI 测试.但是,我无法让它运行.这是我的 build.gradle 和我的错误信息.
应用插件:'com.android.application'安卓 {compileSdkVersion 26默认配置{applicationId "com.example.wpjtest2"minSdk 版本 26目标SDK版本26版本代码 1版本名称1.0"testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}构建类型{释放 {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}编译选项{sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}}依赖{实现文件树(包括:['*.jar'],目录:'libs')实现 'com.android.support:appcompat-v7:26.1.0'实现 'com.android.support.constraint:constraint-layout:1.0.2'testImplementation 'junit:junit:4.12'androidTestImplementation 'com.android.support.test:runner:1.0.1'androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'androidTestImplementation 'io.appium:java-client:5.0.4'}
错误:
信息:Gradle 任务 [:app:assembleDebug, :app:assembleDebugAndroidTest]错误:java.lang.IllegalAccessException:没有这样的方法:org.springframework.core.io.buffer.DataBufferUtils.lambda$read$0(ReadableByteChannel)ReadableByteChannel/invokeStatic错误:java.lang.NoClassDefFoundError:org/reactivestreams/Publisher错误:java.lang.ClassNotFoundException:找不到类 org.reactivestreams.Publisher错误:java.nio.file.DirectoryNotEmptyException:C:\Users\zil\AppData\Local\Temp\lambdas5516872364251960030\org\springframework\core\io错误:java.lang.IllegalAccessException:没有这样的方法:org.springframework.beans.factory.config.YamlMapFactoryBean.lambda$createMap$0(Map,Properties,Map)void/invokeSpecial错误:java.lang.NoClassDefFoundError:org/yaml/snakeyaml/reader/UnicodeReader错误:java.lang.ClassNotFoundException:找不到类 org.yaml.snakeyaml.reader.UnicodeReader错误:任务 ':app:transformClassesWithDesugarForDebugAndroidTest' 的执行失败.>com.android.build.api.transform.TransformException: java.lang.RuntimeException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: 使用主类 com.google.devtools 执行 java 进程时出错.build.android.desugar.Desugar 带参数 {@C:\Users\xxx\project\WPJTest2\app\build\intermediates\tmp\desugar_args221997254795871866}信息:BUILD 在 15 秒内失败错误:java.lang.ClassNotFoundException:未找到类 javax.validation.Validator信息:9个错误信息:0 条警告信息:查看控制台中的完整输出
谁能帮我检查一下我是否有正确和足够的依赖项?有很多关于添加 Appium 的教程,但没有一个适合我.
另一方面,如果我可以使用 Java 7 与 Appium 集成,我该怎么做?
尽管
添加库到您的项目 app
或 my_feature
gradle 文件.Appium 和 Selenium 版本应该注意避免没有这样的方法错误" - 查看答案:
依赖项{androidTestImplementation 'junit:junit:4.12'androidTestImplementation 'io.appium:java-client:5.0.1'androidTestImplementation 'org.seleniumhq.selenium:selenium-java:3.4.0'}
在您的 Android 项目 .../src/androidTest/java/
文件夹中创建 JUnit 功能测试类.您也可以使用简单的 JUnit 测试包装器.对于测试需求,您可以在 Android 项目中使用 main
方法创建一个单独的 Java 项目或 Java 类,但将 Appium 测试集成到 Android Studio 测试功能中会更方便.还要检查您是否没有导入其他测试库,也不要在您的代码中混合使用它(例如,@Test 可用于 TestNG 以及 JUnit).在您的方法中使用 Appium Inspector 自动生成的代码,例如:
包 com.example.my_project;导入 org.junit.After;导入 org.junit.Before;导入 org.junit.Test;导入 org.openqa.selenium.remote.DesiredCapabilities;导入 java.net.MalformedURLException;导入 java.net.URL;导入 io.appium.java_client.MobileElement;导入 io.appium.java_client.android.AndroidDriver;公共类 SampleTest {私有 AndroidDriver司机;@测试公共无效测试计划()抛出 MalformedURLException {设置();testWorkUnit_WithCertainState_ShouldDoSomething();abotherTestWorkUnit_WithCertainState_ShouldDoSomething();拆除();}公共无效设置()抛出 MalformedURLException {DesiredCapabilities requiredCapabilities = 新的 DesiredCapabilities();URL remoteUrl = new URL("http://localhost:4723/wd/hub");requiredCapabilities.setCapability("平台名称",安卓");requiredCapabilities.setCapability(设备名称","安卓模拟器");requiredCapabilities.setCapability("应用程序包","com.example.my_project");requiredCapabilities.setCapability("应用活动","com.example.my_project.MyActivity");司机 =新的 AndroidDriver<>(远程网址,所需的能力);}public void testWorkUnit_WithCertainState_ShouldDoSomething() {移动元素 el1 =driver.findElementById("com.example.my_project:id/urlField");el1.sendKeys("example.com");}公共无效撕裂(){驱动程序退出();}}
至于代码中的注解:@Before
和 @After
用于类中每个测试用例的前后(@BeforeClass
和@AfterClass
需要静态且不方便).因此,它每次都会重新启动应用程序,并且不便于链接测试.所以最好有一个方法用@Test
注解,这样会调用其他没有注解的方法进行设置,按要求顺序做测试用例,完成.
I am trying to use appium java client for my Android UI test. However, I Cannot make it run. Here is my build.gradle and my error message.
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.wpjtest2"
minSdkVersion 26
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
androidTestImplementation 'io.appium:java-client:5.0.4'
}
error:
Information:Gradle tasks [:app:assembleDebug, :app:assembleDebugAndroidTest]
Error:java.lang.IllegalAccessException: no such method: org.springframework.core.io.buffer.DataBufferUtils.lambda$read$0(ReadableByteChannel)ReadableByteChannel/invokeStatic
Error:java.lang.NoClassDefFoundError: org/reactivestreams/Publisher
Error:java.lang.ClassNotFoundException: Class org.reactivestreams.Publisher not found
Error:java.nio.file.DirectoryNotEmptyException: C:\Users\zil\AppData\Local\Temp\lambdas5516872364251960030\org\springframework\core\io
Error:java.lang.IllegalAccessException: no such method: org.springframework.beans.factory.config.YamlMapFactoryBean.lambda$createMap$0(Map,Properties,Map)void/invokeSpecial
Error:java.lang.NoClassDefFoundError: org/yaml/snakeyaml/reader/UnicodeReader
Error:java.lang.ClassNotFoundException: Class org.yaml.snakeyaml.reader.UnicodeReader not found
Error:Execution failed for task ':app:transformClassesWithDesugarForDebugAndroidTest'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: Error while executing java process with main class com.google.devtools.build.android.desugar.Desugar with arguments {@C:\Users\xxx\project\WPJTest2\app\build\intermediates\tmp\desugar_args221997254795871866}
Information:BUILD FAILED in 15s
Error:java.lang.ClassNotFoundException: Class javax.validation.Validator not found
Information:9 errors
Information:0 warnings
Information:See complete output in console
Can anyone check for me to see whether I have correct and sufficient dependencies? There are so many tutorials for adding Appium but none of them works for me.
On the other hand, if I can use Java 7 to integrate with Appium, how should I do instead?
Although Appium is well documented and versatile, the working combination for Android Studio took me a working day. In my case it was a substitution for Espresso, because it doesn't currently support multi-feature testing for Instant Apps. Also Appium gives wider possiblities to test app integration, like Firebase messaging, using other apps etc.
- Install Appium Server as UI.
- Setup Appium Server for your localhost:
- Host: 127.0.0.1 Port: 4723
- Edit Configurations -> Set ANDROID_HOME and JAVA_HOME paths.
- Start server. You'll see a console, leave this window open.
- Run Android Emulator from Android Studio.
- Start Appium Session for your Emulator in Appium UI (File -> New Session Window):
- If everything goes fine and console shows no log errors, then Appium Inspector window will open. Also your app will be run in the Emulator. In Inspector find elements by clicking on a screenshot. Record your actins with buttons atop and get autogenerated code:
Add libraries to your project
app
ormy_feature
gradle file. Appium and Selenium versions should watch to avoid "No such method error" - see an answer:dependencies { androidTestImplementation 'junit:junit:4.12' androidTestImplementation 'io.appium:java-client:5.0.1' androidTestImplementation 'org.seleniumhq.selenium:selenium-java:3.4.0' }
Create a JUnit functional test class in your Android project
.../src/androidTest/java/
folder. You can use simple JUnit test wrapper as well. For testing needs you could have created a separate Java project or Java class withmain
method inside your Android project, but it is more convenient to integrate Appium tests into Android Studio testing features. Also check that you don't import other testing libraries and don't use it as a mix in your code (e.g. @Test is available for TestNG as well as for JUnit). Use autogenerated code from Appium Inspector inside your methods like:package com.example.my_project; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.remote.DesiredCapabilities; import java.net.MalformedURLException; import java.net.URL; import io.appium.java_client.MobileElement; import io.appium.java_client.android.AndroidDriver; public class SampleTest { private AndroidDriver<MobileElement> driver; @Test public void testPlan() throws MalformedURLException { setUp(); testWorkUnit_WithCertainState_ShouldDoSomething(); abotherTestWorkUnit_WithCertainState_ShouldDoSomething(); tearDown(); } public void setUp() throws MalformedURLException { DesiredCapabilities desiredCapabilities = new DesiredCapabilities(); URL remoteUrl = new URL("http://localhost:4723/wd/hub"); desiredCapabilities.setCapability( "platformName", "Android"); desiredCapabilities.setCapability( "deviceName", "Android Emulator"); desiredCapabilities.setCapability( "appPackage", "com.example.my_project"); desiredCapabilities.setCapability( "appActivity", "com.example.my_project.MyActivity"); driver = new AndroidDriver<>( remoteUrl, desiredCapabilities); } public void testWorkUnit_WithCertainState_ShouldDoSomething() { MobileElement el1 = driver.findElementById( "com.example.my_project:id/urlField"); el1.sendKeys("example.com"); } public void tearDown() { driver.quit(); } }
As for annotations in code: @Before
and @After
are used before and after each test case in your class (@BeforeClass
and @AfterClass
requires static and is not handlful). Therefore it restarts the app every time as a result and its not convenient for chaining tests. So it's better to have one method annotated with @Test
, which will call other unannotated methods for setting up, doing test case in required order and finishing.
这篇关于无法使用 Appium + Android Studio 3.0 + Java 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!