• 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 + "
    ");             
        
                }
            } 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 + "
    ");
    
                }
            } 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;
        }
    }

     

  • 相关阅读:
    安装ActivePython
    安装Selenium
    安装setuptools与pip
    windows下测试环境搭建--Python安装
    编程语言 标记语言 脚本语言
    XMLHttpRequest 对象
    事务
    jsp
    Cookie案例-显示用户的上次访问时间代码实现
    事务处理
  • 原文地址:https://www.cnblogs.com/testway/p/6145643.html
Copyright © 2020-2023  润新知