无法使用Appium + Android Studio 3.0 + Java 8 [英] Failed to use Appium + Android Studio 3.0 + Java 8

查看:251
本文介绍了无法使用Appium + Android Studio 3.0 + Java 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将appium java客户端用于我的Android UI测试.但是,我无法使其运行.这是我的build.gradle和错误消息.

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'
}

错误:

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

任何人都可以帮我检查一下我是否具有正确且足够的依赖关系吗?有太多添加Appium的教程,但没有一个对我有用.

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.

另一方面,如果我可以使用Java 7与Appium集成,应该怎么做?

On the other hand, if I can use Java 7 to integrate with Appium, how should I do instead?

推荐答案

尽管 Appium 有据可查且用途广泛, Android Studio的有效组合使我花了一个工作日.就我而言,它是Espresso的替代品,因为它当前不支持Instant Apps的多功能测试.此外,Appium还提供了更广泛的可能性来测试应用程序集成,例如Firebase消息传递,使用其他应用程序等.

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.

  1. 将Appium Server 安装为用户界面.
  2. 为您的本地主机设置Appium服务器:
    • 主机:127.0.0.1端口:4723
    • 编辑配置->设置ANDROID_HOME和JAVA_HOME路径.
    • 启动服务器.您会看到一个控制台,此窗口保持打开状态.
  1. Install Appium Server as UI.
  2. 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.

添加库到项目appmy_feature gradle文件中. Appium和Selenium版本应注意避免出现无此类方法错误"-查看答案:

Add libraries to your project app or my_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'
}

  • 在您的Android项目.../src/androidTest/java/文件夹中创建JUnit功能测试类.您也可以使用简单的JUnit测试包装器.为了满足测试需求,您可以在Android项目中使用main方法创建一个单独的Java项目或Java类,但是将Appium测试集成到Android Studio测试功能更加方便.还要检查您是否未导入其他测试库,也不要在代码中混合使用它(例如,@ Test可用于TestNG和JUnit).在以下方法中使用来自Appium Inspector的自动生成的代码,例如:

  • 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 with main 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();
          }
        }
    

  • 对于代码中的注释:@Before@After在类中的每个测试用例之前和之后都使用(@BeforeClass@AfterClass需要静态且不方便).因此,它因此每次都会重新启动应用程序,并且不便于进行链接测试.因此,最好使用一种带有@Test注释的方法,该方法将调用其他未注释的方法进行设置,按要求的顺序进行测试用例并完成.

    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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆