【问题标题】:Failed to use Appium + Android Studio 3.0 + Java 8无法使用 Appium + Android Studio 3.0 + Java 8
【发布时间】:2018-07-21 13:57:16
【问题描述】:

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

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 的教程有很多,但没有一个对我有用。

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

【问题讨论】:

  • 我遇到了同样的问题。你找到解决办法了吗?如果有请分享给大家。

标签: java android appium


【解决方案1】:

尽管Appium 有据可查且用途广泛,但 Android Studio 的工作组合却花了我一个工作日。就我而言,它是 Espresso 的替代品,因为它是 doesn't currently support multi-feature testing for Instant Apps。此外,Appium 还为测试应用集成提供了更广泛的可能性,例如 Firebase 消息传递、使用其他应用等。

  1. Install Appium Server as UI
  2. 为您的本地主机设置 Appium 服务器
    • 主机:127.0.0.1 端口:4723
    • 编辑配置 -> 设置 ANDROID_HOME 和 JAVA_HOME 路径。
    • 启动服务器。你会看到一个控制台,让这个窗口保持打开状态。
  3. 从 Android Studio 运行 Android 模拟器
  4. 在 Appium UI 中为您的模拟器启动 Appium 会话(文件 -> 新会话窗口):
  5. 如果一切正常且控制台未显示日志错误,则 Appium Inspector 窗口 将打开。您的应用程序也将在模拟器中运行。在 Inspector 中,通过单击屏幕截图来查找元素。使用顶部的按钮记录您的肌动蛋白并获取自动生成的代码:
  6. 添加库到您的项目appmy_feature gradle 文件。 Appium 和 Selenium 版本应该注意避免“没有这样的方法错误” - 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'
    }
    
  7. 在您的 Android 项目 .../src/androidTest/java/ 文件夹中创建一个 JUnit 功能测试类。您也可以使用简单的 JUnit 测试包装器。对于测试需求,您可以在 Android 项目中创建一个单独的 Java 项目或带有 main 方法的 Java 类,但是将 Appium 测试集成到 Android Studio 测试功能中更方便。还要检查你没有导入其他测试库,也不要在你的代码中混合使用它(例如,@Test 可用于 TestNG 和 JUnit)。在您的方法中使用 Appium Inspector 自动生成的代码,例如:

        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注解,它会调用其他未注解的方法来设置,按要求的顺序做测试用例并完成。

【讨论】:

  • AndroidDriver 现在是一个泛型类,如果我们像这样定义私有 AndroidDriver driver;那么不需要类型转换 MobileElement el1 = (MobileElement) driver.findElementById("com.example.my_project:id/urlField");
  • 谢谢。已更新。
猜你喜欢
  • 2016-11-14
  • 1970-01-01
  • 2018-04-07
  • 1970-01-01
  • 1970-01-01
  • 2015-11-10
  • 2018-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多