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);
        }
    }

 

唉,測試代碼能力一般,總是出各種白菜錯誤,各位請隨便噴!哈哈哈哈!

 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM