appium新版本不支持findElementByName,切換到findElementByAndroidUIAutomator


appium 1.7.6 不支持findElementByName(locator)  不知道為什么? 腳本中許多這樣的語句,麻煩事情多了

org.openqa.selenium.InvalidSelectorException: Locator Strategy 'name' is not supported for this session (WARNING: The server did not provide any stacktrace information)


*** Element info: {Using=name, value=取件}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:40)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:353)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:56)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:451)
at io.appium.java_client.DefaultGenericMobileDriver.findElementByName(DefaultGenericMobileDriver.java:104)
at io.appium.java_client.AppiumDriver.findElementByName(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElementByName(AndroidDriver.java:1)
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:497)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1137)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:753)
at org.testng.TestRunner.run(TestRunner.java:607)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:368)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:363)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:321)
at org.testng.SuiteRunner.run(SuiteRunner.java:270)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1284)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1209)
at org.testng.TestNG.runSuites(TestNG.java:1124)
at org.testng.TestNG.run(TestNG.java:1096)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)

 

處理方法:把以前腳本 driver.findElementByName("中文")  修改為driver.findElementByAndroidUIAutomator("text(\"中文\")")

寫一個測試類,循環讀寫每行代碼,如果含有driver.findElementByName 就進行替換處理

 

package auto.auto.test;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class EditFile {
    
    public static void main(String[] args) {
    
        File file = new File("D:\\AutoTest\\autotest\\src");
        ArrayList<String>  strArray =  getChildFileName(file);  //獲取子文件或文件夾
        for (int i = 0;i <strArray.size();i++){
            String filePath = strArray.get(i);
                if(filePath.contains(".java")){  //如果是java文件就處理
                    EditFile ef = new EditFile();
                    ef.write(filePath, ef.read(filePath)); // 先讀取並修改文件
                    System.out.println("edit file:"+strArray.get(i));
            }
        }
    }    
    
    
    

    /**
     * 讀取文件內容
     * 
     * @param filePath
     * @return
     */
    public String read(String filePath) {
        BufferedReader br = null;
        String line = null;
        StringBuffer buf = new StringBuffer();
        
        try {            
            br = new BufferedReader(new FileReader(filePath));          
           
            while ((line = br.readLine()) != null) {
                // 此處根據實際需要修改某些行的內容
                if (line.contains("driver.findElementByName")) {
                    line = line.replace("\")", "\\\")\")");
                    line = line.replace("driver.findElementByName(\"", "driver.findElementByAndroidUIAutomator(\"text(\\\"");
                    
                }                
                    buf.append(line + "\r\n");             
    
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    br = null;
                }
            }
        }
        
        return buf.toString();
    }
    
    /**
     * 將內容回寫到文件中
     * 
     * @param filePath
     * @param content
     */
    public void write(String filePath, String content) {
        BufferedWriter bw = null;
        
        try {
           
            bw = new BufferedWriter(new FileWriter(filePath));
           
            bw.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 關閉流
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    bw = null;
                }
            }
        }
    }
    
    
    
     /**
      * 遞歸獲取子文件或文件夾名稱
      * @param f
      * @return
      */
     public static  ArrayList<String>  getChildFileName(File f) {  
         ArrayList<String>  strArray = new ArrayList<String> (); 
         ArrayList<String>  tmp = new ArrayList<String> (); 
          File file[] = f.listFiles();  
         for (int i = 0; i < file.length; i++) {
             System.out.println("nnnnnn"+ file[i].getAbsolutePath());  
             strArray.add(file[i].getAbsolutePath());
   
             if (file[i].isDirectory()) {  
                 //strArray = strArray + listFile(file[i]);  
                 tmp = getChildFileName(file[i]);
                 strArray.addAll(tmp);
             }  
         }  
         return strArray;
     }  
}

 替換后發現部分帶有變量的語句替我不完整,如driver.findElementByAndroidUIAutomator(viceBox).click(); 應該替換成driver.findElementByAndroidUIAutomator("text(\"+viceBox+\")").click();,實際沒有被替換

需要使用正則表達式繼續替換。正則表達式:findElementByAndroidUIAutomator\((\s*[^" ])   \s*表示0個或者任意個空格  [^" ] 表示不以 “ 或空格開頭。

完整的代碼如下:

 

package auto.auto.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EditFile {
    static int count = 0;

    public static void main(String[] args) {

         File file = new File("D:\\AutoTest\\autotest\\src");  
        //File file = new File("D:\\test");
        ArrayList<String> strArray = getChildFileName(file); // 獲取子文件或文件夾
        for (int i = 0; i < strArray.size(); i++) {
            String filePath = strArray.get(i);
            if (filePath.contains(".java")) { // 如果是java文件就處理
                EditFile ef = new EditFile();
                ef.write(filePath, ef.read(filePath)); // 先讀取並修改文件
                System.out.println("edit file:" + strArray.get(i));
            }
        }
        System.out.println("修改次數:" + count);
    }

    /**
     * 讀取文件內容
     * 
     * @param filePath
     * @return
     */
    public String read(String filePath) {
        BufferedReader br = null;
        String line = null;
        StringBuffer buf = new StringBuffer();
        String findStr ="findElementByAndroidUIAutomator\\((\\s*[^\" ])";
        Pattern pattern = Pattern.compile(findStr);

        try {
            br = new BufferedReader(new FileReader(filePath));

            while ((line = br.readLine()) != null) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    line = line.replace(").click", "+\"\\\")\").click"); 
                    line = line.replace("findElementByAndroidUIAutomator(",
                            "findElementByAndroidUIAutomator(\"text(\\\"\"+");
                    count = count + 1;
                    
                }

            
                buf.append(line + "\r\n");

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    br = null;
                }
            }
        }

        return buf.toString();
    }

    /**
     * 將內容回寫到文件中
     * 
     * @param filePath
     * @param content
     */
    public void write(String filePath, String content) {
        BufferedWriter bw = null;

        try {

            bw = new BufferedWriter(new FileWriter(filePath));

            bw.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 關閉流
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    bw = null;
                }
            }
        }
    }

    /**
     * 遞歸獲取子文件或文件夾名稱
     * 
     * @param f
     * @return
     */
    public static ArrayList<String> getChildFileName(File f) {
        ArrayList<String> strArray = new ArrayList<String>();
        ArrayList<String> tmp = new ArrayList<String>();
        File file[] = f.listFiles();
        for (int i = 0; i < file.length; i++) {
            System.out.println("nnnnnn" + file[i].getAbsolutePath());
            strArray.add(file[i].getAbsolutePath());

            if (file[i].isDirectory()) {
                // strArray = strArray + listFile(file[i]);
                tmp = getChildFileName(file[i]);
                strArray.addAll(tmp);
            }
        }
        return strArray;
    }
}

 

 

 


免責聲明!

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



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