随着微信小程序性能和生态的日益完善,很多企业的产品业务逐步从App扩大到微信小程序和微信公众号。随着小程序我的项目页面越来越多,业务逻辑越来越简单,现有的小程序元源码数量和品种不能满足快速增长的业务需要。
残缺源码:y.wxlbyx.icu
但因为小程序自身的一些个性,业界不足成熟欠缺的解决方案,总会呈现各种问题(包含腾讯微信官网提供的自动化工具)。明天咱们要带来的是一些微信小程序自动化测试的最佳实际和教训,包含微信小程序的根本测试技术和操作方法,以及如何应用appium的WebView测试技术+ADB代理来实现微信小程序的自动化测试(可能是目前最实用的小程序自动测试技术),并附上PHP版源码。
小程序源码运行环境
平台差别:尽管运行环境很类似,但还是有一些区别:
JavaScript 语法和 API 反对不统一:从语法上来说,开发者能够通过开启 ES6 到 Es5 的性能来防止(详情);此外,applet 根底库内置了必要的 Polyfill 来补救 API 的差别。
wxss渲染性能不统一:尽管开启款式补全能够防止大部分问题,但倡议开发者须要别离在IOS和Android上查看小程序的真实性能。
微信小程序技术架构
微信小程序的技术架构如下图所示:
应用 chrome 调试小程序
应用Chrome浏览器提供的inspect剖析工具,在浏览器中输出如下地址:
chrome://inspect/#devices
应用Chrome浏览器查看手机上关上的WebView过程和根本信息:
能够应用chrome inspect来剖析微信小程序的控制结构和布局:
应用控制台执行您本人的 JavaScript 代码:
小程序性能测试
这里附上一个小程序性能测试图:
微信小程序自动测试
微信小程序自动测试关键步骤
1.原生自动化模式。
●能够应用appium来实现。毛病是管制定位不够精确,无奈深刻小程序;
2、WebView自动化模式:能够取得更多小程序外部品质数据。
●设置正确的chromedriver版本
●设置 chrome 选项并将其传递给 chromedriver
●应用ADB代理解决修复chromedriver的bug
3、为什么很多人还不确定?
●低版本的chromedriver在高版本的手机上有bug
●chromedriver与微信定制的chrome内核的连贯实现有问题
源码示例:
class TestWXMicroWebView: #For demonstration convenience, page object mode is not used def setup(self): caps = {} caps["platformName"] = "android" Caps ["devicename"] = "programmer Yifan. Com" caps["appPackage"] = "com.tencent.mm" caps["appActivity"] = "com.tencent.mm.ui.LauncherUI" caps["noReset"] = True caps['unicodeKeyboard'] = True caps['resetKeyboard'] = True caps['chromedriverExecutable'] = \ '/Users/seveniruby/projects/chromedriver/chromedrivers/chromedriver_78.0.3904.11' # options = ChromeOptions() # options.add_experimental_option('androidProcess', 'com.tencent.mm:appbrand0') caps['chromeOptions'] = { 'androidProcess': 'com.tencent.mm:appbrand0' } caps['adbPort'] = 5038 self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) self.driver.implicitly_wait(30) self. driver. find_ Element (by. XPath, "// * [@ text = 'address book']") self.driver.implicitly_wait(10) self.enter_micro_program() print(self.driver.contexts) def enter_micro_program(self): #Native automated testing size = self.driver.get_window_size() self.driver.swipe(size['width'] * 0.5, size['height'] * 0.4, size['width'] * 0.5, size['height'] * 0.9) self.driver.find_element(By.CLASS_NAME, 'android.widget.EditText').click() self. driver. find_ Element (by. XPath, "// * [@ text = 'Cancel']) self. driver. find_ element(By.CLASS_NAME, "android.widget.EditText"). send_ Keys ("snowball") self.driver.find_element(By.CLASS_NAME, 'android.widget.Button') self.driver.find_element(By.CLASS_NAME, 'android.widget.Button').click() self. driver. find_ Element (by. XPath, "// * [@ text = 'optional']) def find_top_window(self): for window in self.driver.window_handles: print(window) if ":VISIBLE" in self.driver.title: print(self.driver.title) else: self.driver.switch_to.window(window) def test_search_webview(self): #Enter WebView self.driver.switch_to.context('WEBVIEW_xweb') self.driver.implicitly_wait(10) self.find_top_window() #CSS positioning self.driver.find_element(By.CSS_SELECTOR, "[src*=stock_add]").click() #Wait for new window WebDriverWait(self.driver, 30).until(lambda x: len(self.driver.window_handles) > 2) self.find_top_window() self.driver.find_element(By.CSS_SELECTOR, "._input").click() #Input self.driver.switch_to.context("NATIVE_APP") ActionChains(self.driver).send_keys("alibaba").perform() #Click self.driver.switch_to.context('WEBVIEW_xweb') self.driver.find_element(By.CSS_SELECTOR, ".stock__item") self.driver.find_element(By.CSS_SELECTOR, ".stock__item").click()小程序自源码开发须要逾越的几个关卡:
●WebView 开关/x5内核调试开关
●chromeoption选项须要填写
●WebView版本和chromedriver版本对应问题
●chrome驱动低版本须要修复ps命令的bug
●上下文API有肯定提早,须要期待