当前位置:网站首页>In the WebView page of the UI automation test App, the processing method when the search bar has no search button
In the WebView page of the UI automation test App, the processing method when the search bar has no search button
2022-08-05 03:59:00 【Pit player】
一、遇到的问题

在做移动端的UI自动化测试时,经常会遇到上图所示的搜索框,这里有个麻烦就是搜索框没有“搜索”按钮,UI自动化测试时不能确认搜索.
要解决这个问题,首先 ,在使用unicodeAfter entering Chinese text in the input method,使用os模块进行adbCommand switch input method:os.system(“adb shell ime set com.sohu.inputmethod.sogou/.SogouIME”) ,Remember to use after switchingsleepwait a second or two,重点:必须sleep等待一下.
我们在使用 driver.press_keycode(‘66’) The method simulates the keyboard enter operation,Waiting for a pause,切换回unicode输入法os.system(“adb shell ime set io.appium.settings/.UnicodeIME”).

At this time, you can also enter Chinese,Search is also available.However, if this happens in the future, you need to call this method to switch.Just encapsulate it.
但是这种方法只能适用于Android环境,iOS环境不能使用.由于我是在Webview环境做UI自动化测试,无论是Android环境,iOS环境都可以使用js方法解决疑难杂症,操作时只需要通过python发送js方法就可以.
二、操作方法

1、找到搜索框元素
通过document对象找到搜索框的元素位置,如上图搜索框:
document.getElementsByTagName('input')[0];
2、js模拟回车事件的方法
下面是实现回车事件的JavaScript方法:
function inputeven() {
let el = document.getElementsByTagName('input')[0];
let evtType = 'keyup';
let keyCode = 13;
let evtObj;
if (document.createEvent) {
if (window.KeyEvent) {
//firefox 浏览器下模拟事件
evtObj = document.createEvent('KeyEvents');
evtObj.initKeyEvent(evtType, true, true, window, true, false, false, false, keyCode, 0)
} else {
//chrome 浏览器下模拟事件
evtObj = document.createEvent('UIEvents');
evtObj.initUIEvent(evtType, true, true, window, 1);
delete evtObj.keyCode;
if (typeof evtObj.keyCode === 'undefined') {
//为了模拟keycode
Object.defineProperty(evtObj, 'keyCode', {
value: keyCode
})
} else {
evtObj.key = String.fromCharCode(keyCode)
}
if (typeof evtObj.ctrlKey === 'undefined') {
//为了模拟ctrl键
Object.defineProperty(evtObj, 'ctrlKey', {
value: true
})
} else {
evtObj.ctrlKey = true
}
}
el.dispatchEvent(evtObj)
} else if (document.createEventObject) {
//IE 浏览器下模拟事件
evtObj = document.createEventObject();
evtObj.keyCode = keyCode;
el.fireEvent('on' + evtType, evtObj)
}
}
inputeven();
在进行UI自动化时,使用selenium的 execute_script() 方法发送js指令时,需要是字符串格式的,因此需要将上面的方法压缩:
js = function inputeven(){
let el=document.getElementsByTagName('input')[0];let evtType='keyup';let keyCode=13;let evtObj;if(document.createEvent){
if(window.KeyEvent){
evtObj=document.createEvent('KeyEvents');evtObj.initKeyEvent(evtType,true,true,window,true,false,false,false,keyCode,0)}else{
evtObj=document.createEvent('UIEvents');evtObj.initUIEvent(evtType,true,true,window,1);delete evtObj.keyCode;if(typeof evtObj.keyCode==='undefined'){
Object.defineProperty(evtObj,'keyCode',{
value:keyCode})}else{
evtObj.key=String.fromCharCode(keyCode)}if(typeof evtObj.ctrlKey==='undefined'){
Object.defineProperty(evtObj,'ctrlKey',{
value:true})}else{
evtObj.ctrlKey=true}}el.dispatchEvent(evtObj)}else if(document.createEventObject){
evtObj=document.createEventObject();evtObj.keyCode=keyCode;el.fireEvent('on'+evtType,evtObj)}}inputeven();
3、要适应pytest自动化测试框架,上面的方法需要进行如下封装:
def js_keyboard_enter(self, element_located):
"""
搜索框输入后,通过js方法确认搜索(用于搜索框没有搜索确认按钮)
:param element_located:已定位到的搜索框元素document对象
:return:
"""
# js方法实现搜索确认事件
js = "function inputeven(){let el=%slet evtType='keyup';"
"let keyCode=13;let evtObj;if(document.createEvent){if(window.KeyEvent){evtObj=document.createEvent"
"('KeyEvents');evtObj.initKeyEvent(evtType,true,true,window,true,false,false,false,keyCode,0)}"
"else{evtObj=document.createEvent('UIEvents');evtObj.initUIEvent(evtType,true,true,window,1);"
"delete evtObj.keyCode;if(typeof evtObj.keyCode==='undefined'){Object.defineProperty(evtObj,'keyCode',"
"{value:keyCode})}else{evtObj.key=String.fromCharCode(keyCode)}if(typeof evtObj.ctrlKey==='undefined')"
"{Object.defineProperty(evtObj,'ctrlKey',{value:true})}else{evtObj.ctrlKey=true}}el.dispatchEvent(evtObj)}"
"else if(document.createEventObject){evtObj=document.createEventObject();evtObj.keyCode=keyCode;el."
"fireEvent('on'+evtType,evtObj)}}inputeven();" % (element_located)
# 执行搜索确认的js方法
self.driver.execute_script(js)
这里我将操作的搜索框document对象作为入参,在使用该方法时只需要传入操作的搜索框元素document对象即可.
4、使用举例
在前面的第1步中,我们已经通过document对象找到操作的搜索框,search_document_loc = “document.getElementsByTagName(‘input’)[0];”
调用js_keyboard_enter()方法,执行回车事件,确认搜索:
@allure.step("搜索框输入后,通过js方法确认")
def search_enter(self):
self.js_keyboard_enter(search_document_loc)
粉丝专享
现在我邀请你进入我们的软件测试学习交流群:【769146372】,备注“入群”, 大家可以一起探讨交流软件测试,共同学习软件测试技术、面试等软件测试方方面面,还会有免费直播课,收获更多测试技巧,我们一起进阶Python自动化测试/测试开发,走向高薪之路.
喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一 键三连哦!
软件测试工程师自学教程:


边栏推荐
- ffmpeg 像素格式基础知识
- How to discover a valuable GameFi?
- iMedicalLIS监听程序(2)
- Acid (ACID) Base (BASE) Principles for Database Design
- 测试薪资这么高?刚毕业就20K
- 重载运算符
- Fifteen. Actual combat - MySQL database building table character set and collation
- 10 years of testing experience, worthless in the face of the biological age of 35
- [MRCTF2020]PYWebsite
- Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals
猜你喜欢

MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
![[极客大挑战 2019]FinalSQL](/img/e4/0c8225ef7c5e7e5bdbaac2ef6fc867.png)
[极客大挑战 2019]FinalSQL

UE4 opens door via interaction (keyboard key)

【测量学】速成汇总——摘录高数帮

36-Jenkins-Job迁移

Developing Hololens encountered The type or namespace name 'HandMeshVertex' could not be found..

How do newcomers get started and learn software testing?

iMedicalLIS监听程序(2)

The test salary is so high?20K just graduated

36-Jenkins-Job Migration
随机推荐
4T硬盘剩余很多提示“No space left on device“磁盘空间不足
MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
【树莓派】树莓派调光
Increasing leetcode - a daily topic 1403. The order of the boy sequence (greed)
多御安全浏览器 V10.8.3.1 版正式发布,优化多项内容
开发Hololens遇到The type or namespace name ‘HandMeshVertex‘ could not be found..
UE4 为子弹蓝图添加声音和粒子效果
The sword refers to Offer--find the repeated numbers in the array (three solutions)
You may use special comments to disable some warnings. 报错解决的三种方式
Detailed and comprehensive postman interface testing practical tutorial
日志导致线程Block的这些坑,你不得不防
国学*周易*梅花易数 代码实现效果展示 - 梅花心易
You may use special comments to disable some warnings. Three ways to report errors
【背包九讲——01背包问题】
iMedicalLIS listener (2)
This year's Qixi Festival, "love vegetables" are more loving than gifts
Ice Scorpion V4.0 attack, security dog products can be fully detected
商业智能BI业务分析思维:现金流量风控分析(一)营运资金风险
iMedicalLIS监听程序(2)
ffmpeg 像素格式基础知识