问题描述:
正常启动 appium 服务, 在 eclipse 中右键 Run as TestNG Test 之后, apk 可以正常安装并启动, 但是总会在 btn.click();处报错, 错误为 java.lang.NullPointerException.
我仔细看了 appium 的日志, [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"value":{"ELEMENT":"1"},"status":0}, 说明找得到这个控件, 但是一调用该控件的.click() 方法就出错了. 有大家遇到和我一样的问题么????
完整的脚本及错误日志如下;
运行环境:
eclipse
jdk 1.8.0_131
appium 1.6.5
java 脚本
package appium;
import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By.ById;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
public class testDemo {
AndroidDriver driver;
SDK sdk = new SDK("360");
@BeforeMethod(alwaysRun = true)
public void setUp() throws Exception {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "apps");
File app = new File(appDir, "ContactManager.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("appium-version", "1.6.5");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "vivo X5L");
capabilities.setCapability("udid", "BMBY8LU4YTMBS8SO");
capabilities.setCapability("platformVersion", "4.4.2");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "com.example.android.contactmanager");
capabilities.setCapability("appActivity", ".ContactManager");
driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
}
@AfterMethod(alwaysRun = true)
public void tearDown() throws Exception {
driver.quit();
}
@Test(groups = { "test" })
public void test() {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement btn=driver.findElement(ById.id("com.example.android.contactmanager:id/addContactButton"));
btn.click();
List textFieldsList = driver.findElementsByClassName("android.widget.EditText");
textFieldsList.get(0).sendKeys("JerryWang");
textFieldsList.get(1).sendKeys("12345678901");
textFieldsList.get(2).sendKeys("jerry@wang.com");
driver.swipe(100, 500, 100, 100, 2);
driver.findElementByName("Save").click();
}
}
eclipse console 的报错信息
FAILED: test
java.lang.NullPointerException
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:275)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:82)
at appium.testDemo.test(testDemo.java:49)
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:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
appium 的日志
[Appium] Welcome to Appium v1.6.5
[Appium] Appium REST http interface listener started on 0.0.0.0:4723
[HTTP] --> POST /wd/hub/session {"capabilities":[{"desiredCapabilities":{"app":"D:\\workspace_new\\appium\\apps\\Contact
Manager.apk","appPackage":"com.example.android.contactmanager","appActivity":".ContactManager","appium-version":"1.6.5",
"platformVersion":"4.4.2","brows