问题描述
我正在尝试按照 POM(页面对象模型)自动化 android 应用程序。运行 test1.java 后测试失败,出现 NullPointerException
package Tests;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeTest;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.remote.MobileCapabilityType;
public class BaseClass {
public AppiumDriver<MobileElement> driver;
@BeforeTest
public void setup() {
try {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("platformName","Android");
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION,"10");
caps.setCapability(MobileCapabilityType.DEVICE_NAME,"emulator-55546");
caps.setCapability(MobileCapabilityType.UDID,"");
caps.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT,60);
caps.setCapability(MobileCapabilityType.APP,"");
caps.setCapability(MobileCapabilityType.broWSER_NAME,"");
caps.setCapability("appPackage","com.google.android.calculator");
caps.setCapability("appActivity","com.android.calculator2.Calculator");
URL url = new URL("http://127.0.0.1:4723/wd/hub");
driver = new AppiumDriver<MobileElement>(url,caps);
// For browser URL Get
// driver.get("http://www.ally.com");
// driver.close();
}
catch(Exception exp){
System.out.println("Cause is :"+exp.getCause());
System.out.println("Message is :"+exp.getMessage());
exp.printstacktrace();
}
}
@AfterSuite
public void teardown() {
driver.quit();
}
}
由 test1.java 扩展,它是实际的测试用例文件
package Tests;
import org.testng.annotations.Test;
import Pages.calculator1;
public class test1 extends BaseClass{
calculator1 calculator1;
@Test
public void sampletest() {
calculator1 = new calculator1(driver);
calculator1.number1();
calculator1.plus();
calculator1.number2();
}
}
最后,我有calculator1.java,它将包含定位器和方法
package Pages;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
public class calculator1 {
AppiumDriver<MobileElement> driver;
public calculator1(AppiumDriver<MobileElement> driver)
{
this.driver=driver;
}
//ALL LINKS AND BUTTONS
MobileElement el1 = (MobileElement) driver.findElementById("com.google.android.calculator:id/digit_7");
public void number1()
{
el1.click();
}
MobileElement el2 = (MobileElement) driver.findElementByAccessibilityId("plus");
public void plus()
{
el2.click();
}
MobileElement el3 = (MobileElement) driver.findElementById("com.google.android.calculator:id/digit_8");
public void number2()
{
el2.click();
}
}
运行 test1.java 作为 testng testcase 后得到以下错误:
Failed: sampletest
java.lang.NullPointerException
at Pages.calculator1.<init>(calculator1.java:15)
at Tests.test1.sampletest(test1.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:599)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:822)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.testng.TestRunner.privateRun(TestRunner.java:764)
at org.testng.TestRunner.run(TestRunner.java:585)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
at org.testng.SuiteRunner.run(SuiteRunner.java:286)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1218)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.runSuites(TestNG.java:1069)
at org.testng.TestNG.run(TestNG.java:1037)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)