Appium 封装 appium 启动程序代码,在测试用例中调用报错,麻烦大神们帮忙看下是什么原因。

skill · 2015年05月16日 · 最后由 陈恒捷 回复于 2015年05月18日 · 1796 次阅读
package zy.com.Public;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;

public class TestBase {

public static  void init(Mydriver driver) {
    System.out.println("ggggg");
    try {
     File classpathRoot = new File(System.getProperty("user.dir"));
     File appDir = new File(classpathRoot, "apps");
    File app = new File(appDir, "Androidtest.apk");
   DesiredCapabilities capabilities = new DesiredCapabilities();
   capabilities.setCapability("platformVersion", "4.4.2"); 
   capabilities.setCapability("platformName", "Android");
   capabilities.setCapability("deviceName", "75UBBKP22AGS");
   capabilities.setCapability("app", app.getAbsolutePath());
//   capabilities.setCapability(CapabilityType.PLATFORM, "ANDROID");
  capabilities.setCapability("app-package", "com.android.test");
  capabilities.setCapability("app-activity", "com.android.test.ui.splash.SplashActivity");
    driver = new Mydriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}
}
public class AppTest 
    extends TestCase
{
    public static Mydriver driver = null;
    ParseXml xml = new ParseXml();
    HashMap<String, String> id = xml.readXml();
     @BeforeClass
   public void setUp(){
          TestBase.init(driver);

   }
   @AfterClass
   public void tearDown() throws Exception {  
       driver.quit();  
   }  
   public void testBegin()throws Exception{
       Thread.sleep(5000);
       for(int i=0;i<=6;i++){
           publicMethod.scrollTo(driver);
           Thread.sleep(3000);
       }
       WebElement el = driver.findElement(By.id(id.get("Slpash_begin")));
       Thread.sleep(1000);
           el.click();
           Thread.sleep(4000);
   }

报错信息:

FAILED CONFIGURATION: @AfterClass tearDown
java.lang.NullPointerException
    at zy.com.test_mave.AppTest.tearDown(AppTest.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.internal.TestMethodWorker.invokeAfterClassMethods(TestMethodWorker.java:225)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:114)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

共收到 4 条回复 时间 点赞

这代码逻辑。。。
init 接收参数 driver,却是在 init 里面对 driver 进行实例化。。
后面 driver 初始化为 null,然后 tearDown 里面去调用 driver ,不报空指针才怪。

#1 楼 @xuxu 我知道这里这里有问题,想问下,有啥解决方法

问题已解决,谢谢大家的帮忙,突然发傻了

#3 楼 @skillgirl 麻烦在帖子名称前面加上【已解决】并在正文附上解决方案吧。这样方便后面的人了解到这个问题已解决。

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册