appium测试代码nullpoint

      今天写了个简单向上滑动,执行到向上滑动操作,报nullpoint异常,经过各种乱碰终于解决了,现记录一下过程,以备以后参考!

      环境背景:java+testng+appium

      在@Test下调用 direct_slide_f("up");  这个方法的时候,最初成员变量up我是没有初始化的,只是简单声明了一下,然后在调用的的时候,没有传“up”,居然传了变量up;

      且    if(direct.equals(this.getUp())){     这句代码之前是   if(direct.equals(up)){  这样写的,所以就报了nullpoint。

      原因就是direct_slide_f 接收的应该是一个具体的字符串参数,而我传了一个没有初始化的变量 up,所以程序就不知道用什么去与   up 进行比较的,当然这个时候 up本身也没有值,

      其实就等于拿自己和自己做对比,然后就报nullpoint了!

     修改方案:将变量up初始化,然后调用方法direct_slide_f的时候,传入字符串“up”,而非变量up,执行就不报nullpoint了!

     总结:如果在新添加一段代码后,代码报nullpoint,首先检查下新添加代码中变量和方法的关系!


public class Login extends KeyClass implements BaseExecuteInterface{
    public Login(){}
    private boolean isInstall = false;
    public static AndroidDriver driver;
    public  final String up = "up";


    @Test
    public void login_f_tmp() throws Exception{
        String username="18576816231";
        String passwd="123456";
        driver.findElement(By.id("com.jiubei.shop:id/telephoneEt")).sendKeys(username);
        Thread.sleep(2000);
        driver.findElement(By.id("com.jiubei.shop:id/ed_pwd")).sendKeys(passwd);
        Thread.sleep(2000);
        //System.out.println("test");
        //key();
        driver.findElement(By.id("com.jiubei.shop:id/submitTv")).click();
        System.out.println("test2");
        //已进入到APP首页
        Thread.sleep(4000);
        driver.findElement(By.name("我的")).click();
        sleep(1);
      direct_slide_f("up");
    }
    // 调用出空指针,现在已修复
    public  void direct_slide_f(String direct){

        TouchAction touchAction =new TouchAction(driver);
        if(direct.equals(this.getUp())){    
            touchAction.longPress(360, 720).moveTo(360, 600).release().perform();
            System.out.println(up);
            sleep(3);
        }else if(direct.equals((this.getDown())){
            touchAction.longPress(360, 600).moveTo(360, 720).release().perform();
            sleep(3);
        }else {
            System.out.println("参数错误");
            sleep(3);
        }
    }

唉,测试代码能力一般,总是出各种白菜错误,各位请随便喷!哈哈哈哈!

 
 
 
 
原文地址:https://www.cnblogs.com/wujianqinjian/p/7750459.html