Appium和UIAutomator英文和数字输入问题记录

本文深入探讨了使用Appium和UIAutomator在输入包含中文和数字的字符串时遇到的问题,通过实践代码展示了如何解决此类Bug,并揭示了问题可能源自于底层UI自动化工具。
  1. Appium对中文支持有问题已经是众所周之得了,但是今天用Appium编写一个创建Note的实例的时候发现Appium对含有英文和数字的字串输入也有问题。</span>  

比如如果想用driver.sendkeys来输入“Note1",事实上你会得到的是Note。实践中发现如果英文字串和数字之间加多一个空格就能解决问题,比如输入”Note 1",最终得到的就会是"Note1".

实践发现UIAutomator存在同样的问题,因为Appium底层调用的就是UIAutomator,所以最终的Bug应该是是属于UIAutomator这一边的。

以下Appium代码可以验证这个问题:

  1.  //Enter the note info and save it  
  2.  WebElement text = driver.findElementByClassName("android.widget.EditText");  
  3. <span style="color:#ff0000;"> text.sendKeys("Note 1");</span>  
  4.    
  5.  driver.sendKeyEvent(82);  
  6.  el = driver.findElement(By.name("Save"));  
  7.  el.click();  
  8.    
  9.  //Find out the new added note entry  
  10.  List <WebElement> entries = driver.findElements(By.className("android.widget.TextView"));  
  11.    
  12.  WebElement targetEntry = null;  
  13.  for(WebElement entry : entries) {  
  14.     <span style="color:#ff0000;">if(entry.getText().equals("Note1")) </span>{  
  15.         targetEntry = entry;  
  16.         break;  
  17.     }  
  18.  }  



修改后再次调用此方法,控制台打印下面信息,请帮我分析一下:Message: androidx.test.uiautomator.StaleObjectException Stacktrace: io.appium.uiautomator2.common.exceptions.StaleElementReferenceException: androidx.test.uiautomator.StaleObjectException at io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:45) at io.appium.uiautomator2.server.AppiumServlet.handleRequest(AppiumServlet.java:262) at io.appium.uiautomator2.server.AppiumServlet.handleHttpRequest(AppiumServlet.java:256) at io.appium.uiautomator2.http.ServerHandler.channelRead(ServerHandler.java:68) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:435) at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:267) at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:250) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:266) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1294) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:911) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:611) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:552) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:466) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:438) at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:140) at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144) at java.lang.Thread.run(Thread.java:1024) Caused by: androidx.test.uiautomator.StaleObjectException at androidx.test.uiautomator.UiObject2.getAccessibilityNodeInfo(UiObject2.java:647) at androidx.test.uiautomator.UiObject2.hashCode(UiObject2.java:105) at java.lang.Object.toString(Object.java:299) at java.util.Formatter$FormatSpecifier.printString(Formatter.java:3138) at java.util.Formatter$FormatSpecifier.print(Formatter.java:3015) at java.util.Formatter.format(Formatter.java:2678) at java.util.Formatter.format(Formatter.java:2614) at java.lang.String.format(String.java:4017) at io.appium.uiautomator2.utils.ReflectionUtils.invoke(ReflectionUtils.java:84) at io.appium.uiautomator2.core.AxNodeInfoExtractor.extractAxNodeInfo(AxNodeInfoExtractor.java:51) at io.appium.uiautomator2.core.AxNodeInfoExtractor.toAxNodeInfo(AxNodeInfoExtractor.java:41) at io.appium.uiautomator2.utils.ElementHelpers.canSetProgress(ElementHelpers.java:77) at io.appium.uiautomator2.model.BaseElement.canSetProgress(BaseElement.java:160) at io.appium.uiautomator2.handler.SendKeysToElement.setProgress(SendKeysToElement.java:47) at io.appium.uiautomator2.handler.SendKeysToElement.safeHandle(SendKeysToElement.java:111) at io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:41) ... 33 more 调用的这个方法内容如下: def random_operation(self, duration,package_name,timeout=25): crash_count, anr_count, flower_screen_count, black_screen_count, white_screen_count = 0, 0, 0, 0, 0 # 获取设备屏幕尺寸 screen = self.driver.get_window_size() screen_width = screen['width'] screen_height = screen['height'] start_time = time.time() last_operation_time = time.time() # 记录最后一次成功操作的时间 while time.time() - start_time < duration: # 检测全局超时 if time.time() - last_operation_time > timeout: print(f"操作超时({timeout}秒无响应),结束测试") break #驻留检测 try: currentt_package = self.get_current_package().lower() target_package = package_name.lower() if currentt_package != target_package: print(f"当前包名不匹配目标!!!") self.driver.activate_app(package_name) time.sleep(3) last_operation_time = time.time() except Exception as e: print(f"驻留检测失败:{e}") try: # 随机选择操作类型(点击/滑动/输入) operation = random.choice(['click', 'swipe', 'input']) if operation == 'click': # 随机坐标点击 x = random.randint(0, screen_width) y = random.randint(0, screen_height) self.driver.tap([(x, y)], duration=50) time.sleep(0.3) elif operation == 'swipe': # 随机方向滑动(上下左右) direction = random.choice(['up', 'down', 'left', 'right']) if direction == 'up': self.driver.swipe(screen_width // 2, screen_height * 3 // 4, screen_width // 2, screen_height // 4, 500) elif direction == 'down': self.driver.swipe(screen_width // 2, screen_height // 4, screen_width // 2, screen_height * 3 // 4, 500) elif direction == 'left': self.driver.swipe(screen_width * 3 // 4, screen_height // 2, screen_width // 4, screen_height // 2, 500) elif direction == 'right': self.driver.swipe(screen_width // 4, screen_height // 2, screen_width * 3 // 4, screen_height // 2, 500) time.sleep(0.5) elif operation == 'input': # 查找输入框元素并随机输入数字 input_elements = self.driver.find_elements(AppiumBy.CLASS_NAME, "android.widget.EditText") if input_elements: input_element = random.choice(input_elements) input_element.click() random_digits = ''.join(str(random.randint(0, 9)) for _ in range(random.randint(1, 10))) input_element.send_keys(random_digits) time.sleep(0.8) if self.check_app_crash(): crash_count += 1 if self.check_app_anr(): anr_count += 1 is_flower, is_black, is_white = self.verify_screen() if is_flower: flower_screen_count += 1 if is_black: black_screen_count += 1 if is_white: white_screen_count += 1 last_operation_time = time.time() except Exception as e: print(f"随机操作执行异常,异常信息:{e}") return (crash_count, anr_count, flower_screen_count, black_screen_count, white_screen_count)
最新发布
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值