欢迎光临散文网 会员登陆 & 注册

干货| app自动化测试之Andriod微信小程序的自动化测试

2022-10-20 15:50 作者:爱测软件测试  | 我要投稿

随着微信小程序的功能和生态日益完善,很多公司的小程序项目页面结构越来越多,业务逻辑也越来越复杂。如何做好小程序的自动化测试就成为测试同学普遍面临的一大痛点难题。

微信小程序

小程序内嵌于微信内部,页面包含 Native 原生元素和 Web 元素,相当于混合应用。并且,小程序 Web 部分是基于腾讯的 X5 内核开发的,也是特殊的 WebView。那么,对微信小程序进行自动化测试,包括操作原生应用、WebView、基于微信官方自动化 SDK。

WebView页面元素获取

使用元素定位工具:

  • weditor

weditor安装方式

pip install weditor

参考文档:GitHub - alibaba/web-editor: web editor for atx 2 使用 chrome inspect 定位时,解析元素是把页面解析为了 html 页面,使用 weditor,则会把页面解析为原生页面,而 Appium 在操作元素时,也是把页面解析成了原生去操作的(切换 webview 除外)

Mac:

adb shell dumpsys activity top| grep ACTIVITY

Windows:

adb shell dumpsys activity top| findstr ACTIVITY

from time import sleep

from appium import webdriver

class TestDemo:  

  def setup(self):        

self.desire_cap= {         

   'automationName': "uiautomator2",   

         "platformName": "Android",   

         "deviceName": "wechat",        

    "appPackage": "com.tencent.mm",     

       "appActivity": ".ui.LauncherUI",   

         "noReset": "true",    

        'chromeOptions': {'androidProcess': 'com.tencent.mm:appbrand0'}        }   

     # androidProcess:webview是独立进程的,导致无法获取,需要在 chromeOptions 添加 androidProcess 即可        self.driver=webdriver.Remote("http://127.0.0.1:4723/wd/hub",self.desire_cap)        self.driver.implicitly_wait(20)   

 def teardown(self):    

    self.driver.quit()    

def test_demo(self):       

 # 刚打开微信时,页面加载时间未知,  

      # 需要通过find_element触发隐式等待,防止后续操作失败        self.driver.find_element_by_xpath("//*[@text='通讯录']")    

   size = self.driver.get_window_size()    

    # 获取当前屏幕的宽、高      

  width = size.get("width")       

 height = size.get("height")    

    # 滑动打开小程序页面       

 self.driver.swipe((width / 2), int((height * 0.2)), (width / 2), (height * 0.8), 2000)    

   self.driver.find_element_by_xpath("//*[@resource-id='com.tencent.mm:id/gam' and @text='雪球']").click()    

    sleep(5)        

print(self.driver.contexts,'第一次打印')        self.driver.find_element_by_xpath("//*[@content-desc='搜索股票信息/代码']/..").click()     

   self.driver.find_element_by_xpath('//*[@text="请输入股票名称/代码"]').send_keys("阿里巴巴")      

  text = self.driver.find_element_by_xpath('//*[@content-desc="阿里巴巴"]')     

   assert text

小程序的测试方法有很多种,因为微信版本经常升级,webview的设置也会发生变化,所以可用的测试方法可能会因为每个版本而不同

干货| app自动化测试之Andriod微信小程序的自动化测试的评论 (共 条)

分享到微博请遵守国家法律