admin管理员组

文章数量:1332361

About Automation Testing in android kotlin|java project, using with Appium framework.

After follow Automation Testing with Appium guideline

I did run the Auto Test on RealDevie/Emulator/Simulator successfully on both android/iOs sides.

But I can not run Auto Test from command line (Might be, this link is only for AndroidJunit|Esspresso framework)

These gradle command lines:

 ./gradlew connectedAndroidTest -Dtest=com.example.autotestkotlinsample.TestAndroidLogin
 ./gradlew connectedAndroidTest
 ./gradlew connectedDebugAndroidTest
 ./gradlew cAT

How to run automation from command line with output result totally same as Auto Test on Emulator?

p/s:

I had:

  • android java|kotlin project (java programming language)

  • include: appium framework (implementation appium library)

  • setup appium method

  • appium server started

  • testing framework: testNG (not Esspresso|AndroidJunit4 framework)

      package com.example.autotestkotlinsample.test
    
      import io.appium.java_client.android.AndroidDriver
      import io.appium.java_client.remote.options.BaseOptions
      import io.github.oshai.kotlinlogging.KotlinLogging
      import .openqa.selenium.By
      import .testng.annotations.AfterMethod
      import .testng.annotations.BeforeMethod
      import .testng.annotations.Test
      import java.MalformedURLException
      import java.URL
      import java.time.Duration
    
      class TestAndroidLogin {
      var driver: AndroidDriver? = null
      var d: Duration = Duration.ofSeconds(15)
    
      private val logger = KotlinLogging
    
      private fun getPackage() = "com.example.autotestkotlinsample"
    
      var usernameId: By = By.id("${getPackage()}:id/username")
      var passwordId: By = By.id("${getPackage()}:id/password")
      var loginId: By = By.id("${getPackage()}:id/login")
    
      @BeforeMethod
      @Throws(MalformedURLException::class)
      fun setUp() {
          val options: BaseOptions<*> = BaseOptions()
              .amend("appium:automationName", "UiAutomator2")
              .amend("appium:deviceName", "emulator-5554")
              .amend("appium:platformVersion", "13")
              .amend("appium:platformName", "Android")
              .amend("appium:app", "/Users/.../app-debug.apk")
              .amend("appium:newCommandTimeout", 5000)
              .amend("appium:connectHardwareKeyboard", true)
    
          driver = AndroidDriver(URL("http://127.0.0.1:4723"), options)
      }
    
      @Test // OK
      fun testFlowLoginSuccessMobile() {
          try {
              driver!!.manage().timeouts().implicitlyWait(d)
    
              // 1 - get Email, then input text
              driver!!.findElement(usernameId).sendKeys("[email protected]")
              // 2 - get Password, then input text
              driver!!.findElement(passwordId).sendKeys("12345")
              // 3 - click SignIn, then click
              driver!!.findElement(loginId).click()
    
              driver!!.manage().timeouts().implicitlyWait(d)
    
              logger.logger { "flowLoginSuccessMobile: SUCCESS" }
          } catch (e: Exception) {
              throw e
          }
      }
    
      @AfterMethod
      fun tearDown() {
          driver!!.quit()
      }
    

    }

File build.gradle:

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.android)
    id("com.google.devtools.ksp") version "2.0.21-1.0.27"
}

android {
    namespace = "com.example.autotestkotlinsample"
    testNamespace = "com.example.autotestkotlinsample.test"
    compileSdk = 35

    defaultConfig {
        applicationId = "com.example.autotestkotlinsample"
        minSdk = 29
        targetSdk = 35
        versionCode = 1
        versionName = "1.0"

        testApplicationId = "com.example.autotestkotlinsample.test"
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        testHandleProfiling = true
        testFunctionalTest = true
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
    buildFeatures {
        buildConfig = true
        viewBinding = true
    }
    packaging {
        resources {
            excludes.add("META-INF/LICENSE.md")
            excludes.add("META-INF/LICENSE-notice.md")
            excludes.add("META-INF/INDEX.LIST")
            excludes.add("META-INF/ioty.versions.properties")
            excludes.add("META-INF/DEPENDENCIES")
        }
    }
}

dependencies {

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.appcompat)
    implementation(libs.material)
    implementation(libs.androidx.activity)
    implementation(libs.androidx.constraintlayout)

    implementation(libs.slf4j)
    implementation(libs.kotlin.logging)

    implementation(libs.androidx.room.runtime)
    implementation(libs.androidx.uiautomator)
    annotationProcessor(libs.androidx.roompiler)
    ksp(libs.androidx.roompiler)

    // Kotlin Extensions and Coroutines support for Room
    implementation(libs.androidx.room.ktx)

    // Optional Room components
    implementation(libs.androidx.room.rxjava2)
    implementation(libs.androidx.room.rxjava3)
    implementation(libs.androidx.room.guava)
    testImplementation(libs.androidx.room.testing)
    implementation(libs.androidx.room.paging)

    implementation(libs.ext.appiumJavaClient)
    testImplementation(libs.ext.appiumJavaClient)

    implementation(libs.ext.appiumSeleniumClient)
    testImplementation(libs.ext.appiumSeleniumClient)
    implementation(libs.ext.appiumSeleniumHttp)
    testImplementation(libs.ext.appiumSeleniumHttp)

    testImplementation(libs.junit)
    androidTestImplementation(libs.ext.junit)
    androidTestImplementation(libs.espresso.core)
    androidTestImplementation(libs.junit.jupiter)
    androidTestImplementation(libs.testng)
//    androidTestImplementation(libs.test.runner)
}

libs.versions.toML file:

    [versions]
agp = "8.7.2"
appiumJavaClient = "9.3.0"
appiumSeleniumClient = "4.26.0"

junit = "4.13.2"
junitVersion = "1.2.1"
junitJupiter = "5.11.3"
roomRuntime = "2.6.1"
testng = "7.10.2"
espressoCore = "3.6.1"

appcompat = "1.7.0"
material = "1.12.0"
activity = "1.9.3"
constraintlayout = "2.2.0"

[libraries]
androidx-room-compiler = { module = "androidx.room:room-compiler", version.ref = "roomRuntime" }
androidx-room-guava = { module = "androidx.room:room-guava", version.ref = "roomRuntime" }
androidx-room-ktx = { module = "androidx.room:room-ktx", version.ref = "roomRuntime" }
androidx-room-paging = { module = "androidx.room:room-paging", version.ref = "roomRuntime" }
androidx-room-runtime = { module = "androidx.room:room-runtime", version.ref = "roomRuntime" }
androidx-room-rxjava2 = { module = "androidx.room:room-rxjava2", version.ref = "roomRuntime" }
androidx-room-rxjava3 = { module = "androidx.room:room-rxjava3", version.ref = "roomRuntime" }
androidx-room-testing = { module = "androidx.room:room-testing", version.ref = "roomRuntime" }

ext-appiumJavaClient = { group = "io.appium", name = "java-client", version.ref = "appiumJavaClient" }

ext-appiumSeleniumClient = { group = ".seleniumhq.selenium", name = "selenium-java", version.ref = "appiumSeleniumClient" }
ext-appiumSeleniumHttp = { group = ".seleniumhq.selenium", name = "selenium-http", version.ref = "appiumSeleniumClient" }

junit = { group = "junit", name = "junit", version.ref = "junit" }
ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
junit-jupiter = { group = ".junit.jupiter", name = "junit-jupiter", version.ref = "junitJupiter" }
testng = { group = ".testng", name = "testng", version.ref = "testng" }

espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }


[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }

Build system:

  • openjdk@17 & openjdk@21
  • Gradle 8.9 && 8.11

After researchs, this command line solution failed also

     ./gradlew -Pandroid.testInstrumentationRunnerArguments.class=com.example.autotestkotlinsample.test.TestAndroidLogin connectedDebugAndroidTest

本文标签: androidTask appconnectedDebugAndroidTest Starting 0 tests Finished 0 testsStack Overflow