appium+python自动化42-微信公众号 (可能之后会遇到也遇到切换不了webview的问题 记录再此 还没试)

前言

本篇介绍如何在微信公众号上自动化测试,以操做个人我的公众号:yoyoketang为例,没关注的,先微信关注了,再跟着操做html

环境准备:
python 3.6
appium 1.7以上版本
微信6.6.6
微信里面webview 57.0
电脑上chrome版本 66.0
appium-chromedriver路径下chromedriver 2.28python

开启微信debug模式

1.以微信为例,webview的版本号能够用chrome浏览器查看到,因为微信用的是x5内核,跟其余app不太同样,这里须要先开启微信的debug模式android

开启微信debug模式:在微信聊天界面输入:debugx5.qq.com,如何勾选"打开TBS内核Inspector调试功能web

2.查看微信里面webview版本,直接在电脑chrome浏览器输入:chrome://inspect/#devices
再打开微信的公众号页面,刷新浏览器页面,就会出现webview版本号57.0chrome

ChromeOptions

1.在appium中context的切换时,识别webview的时候, 把com.tencent.mm:tools的webview识别成com.tencent.mm的webview. 从而致使context切换失败。
全部这里必须加上这个参数ChromeOptions,这个是关键。浏览器

'chromeOptions': {'androidProcess': 'com.tencent.mm:tools'}微信

2.appium1.7之后的版本支持Uiautomator2了,为了保险一点,最好加上这个,用Uiautomator2执行app

'automationName': 'Uiautomator2'less

from appium import webdriver import time # 做者:上海-悠悠 QQ交流群:512200893 desired_caps = { 'platformName': 'Android', 'platformVersion': '7.0', 'deviceName': 'A5RNW18316011440', 'appPackage': 'com.tencent.mm', 'appActivity': '.ui.LauncherUI', 'automationName': 'Uiautomator2', 'unicodeKeyboard': True, 'resetKeyboard': True, 'noReset': True, 'chromeOptions': {'androidProcess': 'com.tencent.mm:tools'} } 

操做公众号

1.操做步骤以下(前提要先关注微信公众号:yoyoketang):测试

  • 点微信首页搜索按钮
  • 输入yoyoketang内容搜索
  • 点开公众号
  • 点公众号菜单-精品分类

2.代码实现

切换webview

# 切换到webview time.sleep(2) print(driver.contexts) driver.switch_to.context('WEBVIEW_com.tencent.mm:tools')

打印结果:

['NATIVE_APP', 'WEBVIEW_com.tencent.mm:tools']

参考代码:

from appium import webdriver import time # 做者:上海-悠悠 QQ交流群:512200893 desired_caps = { 'platformName': 'Android', 'platformVersion': '7.0', 'deviceName': 'A5RNW18316011440', 'appPackage': 'com.tencent.mm', 'appActivity': '.ui.LauncherUI', 'automationName': 'Uiautomator2', 'unicodeKeyboard': True, 'resetKeyboard': True, 'noReset': True, 'chromeOptions': {'androidProcess': 'com.tencent.mm:tools'} } driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) driver.implicitly_wait(10) # 做者:上海-悠悠 QQ交流群:512200893 # 点微信首页搜索按钮 driver.find_element_by_accessibility_id("搜索").click() # 输入内容搜索 time.sleep(3) driver.find_element_by_id('com.tencent.mm:id/hx').send_keys("yoyoketang") # 点开公众号 time.sleep(3) driver.find_element_by_id('com.tencent.mm:id/l7').click() # 点公众号菜单-精品分类 time.sleep(3) driver.find_elements_by_id('com.tencent.mm:id/aaq')[0].click() # 切换到webview time.sleep(2) print(driver.contexts) driver.switch_to.context('WEBVIEW_com.tencent.mm:tools') # 点webview上元素 目前有个问题会报找不到元素 driver.find_element_by_xpath(".//*[@id='namespace_1']/div[1]/div/div[2]").click() time.sleep(2) driver.quit()

 来源: http://www.javashuo.com/article/p-zpcxhcja-hd.html

相关文章
相关标签/搜索