关于android java项目中的自动化测试,使用Appium框架。
我有:
android java项目(java编程语言)
包括:appium框架(实现appium库)
设置appium方法
appium 服务器已启动
还包括这些测试框架:testNG、espresso、uiautomator2
编写|运行测试用例后,找不到类|包java.net.http.HttpClient(而其他类必须从包java.net.http中找到,例如:URL,...)
NoClassDefFoundError:解析失败:Ljava/net/http/HttpClient
如何解决这个问题?
package ex.jvautotest;
import android.util.Log;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.options.BaseOptions;
public class FirstTest {
static AndroidDriver driver;
@BeforeClass
public static void setUp() throws MalformedURLException {
Log.d("", "SETUP ...");
var options = new BaseOptions()
.amend("appium:automationName", "UiAutomator2")
.amend("appium:deviceName", "emulator-5554")
.amend("appium:platformName", "Android")
.amend("appium:app", "/Users/.../Desktop/app-debug.apk")
.amend("appium:newCommandTimeout", 3600)
.amend("appium:connectHardwareKeyboard", true);
/// ERROR IN THIS LINE
driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), options);
Log.d("", "SETUP SUCCESS");
}
@Test
public void sampleTest() throws Exception {
try {
Duration d = Duration.ofSeconds(10);
// 1 - get Email, then input text
// By usernameId = By.id("com.example.autotestsample:id/username");
By usernameId = By.xpath(
"//android.widget.EditText[@resource-id=\"com.example.autotestsample:id/username\"]");
driver.findElement(usernameId).sendKeys("[email protected]");
} catch (Exception e) {
Log.d("", "Login failed");
throw e;
}
}
@AfterClass
public static void tearDown() {
driver.quit();
}
}
文件build.gradle:
plugins {
alias(libs.plugins.android.application)
}
android {
namespace = "ex.jvautotest"
compileSdk = 35
defaultConfig {
applicationId = "ex.jvautotest"
minSdk = 33
targetSdk = 35
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
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/io.netty.versions.properties")
excludes.add("META-INF/DEPENDENCIES")
}
}
}
dependencies {
// implementation("org.openjdk:java.net:21")
implementation(libs.appcompat)
implementation(libs.material)
implementation(libs.activity)
implementation(libs.constraintlayout)
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)
}
libs.versions.toML 文件:
[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 = "org.seleniumhq.selenium", name = "selenium-java", version.ref = "appiumSeleniumClient" }
ext-appiumSeleniumHttp = { group = "org.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 = "org.junit.jupiter", name = "junit-jupiter", version.ref = "junitJupiter" }
testng = { group = "org.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" }
构建系统:
我找到了另一种方法来避免这个问题。
之前:
Junit
框架/包之后:
成功:我使用
TestNG
框架/包
package ex.jvautotest;
import org.openqa.selenium.By;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.options.BaseOptions;
public class FirstTest {
static AndroidDriver driver;
@BeforeMethod
public static void setUp() throws MalformedURLException {
var options = new BaseOptions()
.amend("appium:automationName", "UiAutomator2")
.amend("appium:deviceName", "emulator-5554")
.amend("appium:platformName", "Android")
.amend("appium:app", "/Users/.../app-debug.apk")
.amend("appium:newCommandTimeout", 3600)
.amend("appium:connectHardwareKeyboard", true);
driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), options);
}
@Test
public void sampleTest() throws Exception {
try {
// 1 - get Email, then input text
By usernameId = By.id("com.example.autotestsample:id/username");
// 2 - get Password, then input text
By passwordId = By.id("com.example.autotestsample:id/password");
// 3 - click SignIn, then click
By loginId = By.id("com.example.autotestsample:id/login");
driver.findElement(usernameId).sendKeys("[email protected]");
driver.findElement(passwordId).sendKeys("12345");
driver.findElement(loginId).click();
} catch (Exception e) {
throw e;
}
}
@AfterMethod
public static void tearDown() {
driver.quit();
}
}