环境是 Appium+TestNG+Java,环境配置肯定正确,因为我用了别的设备是可以运行起来的
出错的设备是 OPPO R15 Android 8.1
曾经怀疑是 Android 8.1 的问题,但是用其他 Android 8.1 的设备可以正常运行
Appium:
Java 脚本:
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
public class Init {
public AndroidDriver<AndroidElement> driver = null;
/** 用例是否执行成功标识 */
public boolean isPassed = false;
/** 设备A的udid */
public String UDID_A = "93d2aa4c";
/** 设备A的deviceName */
public final String DEVICE_NAME_A = "deviceA";
public final String URL = "http://127.0.0.1:4721/wd/hub";
@BeforeTest
public void beforeTest() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", DEVICE_NAME_A);
capabilities.setCapability("udid", UDID_A);
capabilities.setCapability("appPackage","com.toinn.android.applaunch");
capabilities.setCapability("appActivity",".MainActivity");
capabilities.setCapability("newCommandTimeout", 300);
driver = new AndroidDriver<AndroidElement>(new URL(URL), capabilities);
}
@AfterTest
public void AfterTest() {
driver.quit();
}
}
import org.testng.annotations.Test;
import com.ty.base.Init;
import io.appium.java_client.android.AndroidKeyCode;
public class Class1 extends Init{
@Test
public void classOneMethod() throws InterruptedException {
driver.pressKeyCode(AndroidKeyCode.KEYCODE_HOME);
Thread.sleep(1000);
driver.findElementByXPath("//android.widget.TextView[@text='设置']").click();
Thread.sleep(1000);
driver.findElementByXPath("//android.widget.TextView[@text='电池']").click();
Thread.sleep(1000);
driver.pressKeyCode(AndroidKeyCode.KEYCODE_HOME);
}
}
报错如下:
FAILED CONFIGURATION: @BeforeTest beforeTest
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: Cannot stop and clear com.toinn.android.applaunch. Original error: Error executing adbExec. Original error: 'Command 'E:\AndroidSDK\platform-tools\adb.exe -P 5037 -s 93d2aa4c shell pm clear com.toinn.android.applaunch' exited with code 1'; Stderr: 'Error: java.lang.SecurityException: PID 9904 does not have permission android.permission.CLEAR_APP_USER_DATA to clear data of package com.toinn.android.applaunch'; Code: '1' (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 7.93 seconds
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:12'
System info: host: 'TUY-PC', ip: '192.168.165.2', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_121'
Driver info: io.appium.java_client.android.AndroidDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:247)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:129)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:142)
at io.appium.java_client.DefaultGenericMobileDriver.(DefaultGenericMobileDriver.java:37)
at io.appium.java_client.AppiumDriver.(AppiumDriver.java:161)
at io.appium.java_client.AppiumDriver.(AppiumDriver.java:170)
at io.appium.java_client.android.AndroidDriver.(AndroidDriver.java:82)
at com.ty.base.Init.beforeTest(Init.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:510)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:211)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.TestRunner.beforeRun(TestRunner.java:647)
at org.testng.TestRunner.run(TestRunner.java:615)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
at org.testng.SuiteRunner.run(SuiteRunner.java:259)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
at org.testng.TestNG.run(TestNG.java:1018)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)