Appium 微信小程序自动化

代码

import time
import logging

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from appium import webdriver


logging.basicConfig(level=logging.DEBUG)


class TestWeixin:

    def setup(self):
        caps = {
            "platformName": "Android",
            "deviceName": "Mumu",
            "appPackage": "com.tencent.mm",
            "appActivity": "com.tencent.mm.ui.LauncherUI",
            "autoGrantPermissions": True,
            "automationName": "UiAutomator2",
            "noReset": True,
            "unicodeKeyboard": "true",
            "resetKeyboard": "true",
            "chromedriverExecutable": "/Users/jinlong/PycharmProjects/hogwarts_appium_demo/driver/chromedriver/2.39/chromedriver",
            "chromeOptions": {
                "androidProcess": "com.tencent.mm:appbrand0"
            }
        }

        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_capabilities=caps)
        # self.driver.implicitly_wait(10)

    def teardown(self):
        time.sleep(20)
        self.driver.quit()

    def swipe_down(self):
        # 获取屏幕的高
        x = self.driver.get_window_size()['width']
        # 获取屏幕宽
        y = self.driver.get_window_size()['height']
        # 向下滑动
        self.driver.swipe(1 / 2 * x, 1 / 7 * y, 1 / 2 * x, 6 / 7 * y, 200)

    def test_swipe_in_miniprogram(self):

        # 下拉窗口,进入小程序界面
        WebDriverWait(self.driver, 20, 2).until(EC.element_to_be_clickable((By.XPATH, '//android.widget.TextView[@resource-id="com.tencent.mm:id/ddm" and @text="微信"]')))
        self.swipe_down()
        logging.info("下拉滑动到小程序入口界面")

        # 点击小程序名字
        miniprogram_name = "雪球股票"
        WebDriverWait(self.driver, 20, 2).until(EC.element_to_be_clickable((By.XPATH, f'//android.widget.TextView[@resource-id="com.tencent.mm:id/d0" and @text="{miniprogram_name}"]'))).click()
        logging.info("点击小程序名字")

        # 查看 webview
        logging.debug("当前context:{}".format(self.driver.context))
        logging.debug("\\n所有contexts:{}".format(self.driver.contexts))

        # 切换 webview
        for i in range(20):
            if "WEBVIEW_com.tencent.mm:appbrand0" in self.driver.contexts:
                self.driver.switch_to.context("WEBVIEW_com.tencent.mm:appbrand0")
                break
            time.sleep(1)

        # 切换 webview 后在查看
        logging.debug("\\n切换后context:{}".format(self.driver.current_context))
        logging.debug("\\n切换后所有contexts:{}".format(self.driver.contexts))
        # logging.debug(self.driver.page_source)

        # 查看窗口
        logging.debug("\\n所有窗口:{}".format(self.driver.window_handles))
        logging.debug("\\n默认窗口:{}".format(self.driver.current_window_handle))
        logging.debug("网页标题:{}".format(self.driver.title))

        # 查找 miniprogram 窗口,并切换
        time.sleep(10)
        # for window in self.driver.window_handles:
        #     logging.info("所有 window 窗口:{}".format(window))

        if self.driver.find_element_by_xpath('//body').get_attribute("is") == "pages/optional/optional":
            logging.debug("\\nif 内默认窗口:{}".format(self.driver.current_window_handle))
            logging.debug(self.driver.title)
            pass
        else:
            for i in range(0, 2):
                self.driver.switch_to.window(self.driver.window_handles[i])
                logging.debug("\\nelse 内默认窗口:{}".format(self.driver.current_window_handle))
                logging.debug(self.driver.page_source)
                logging.debug("网页标题:{}".format(self.driver.title))
                if self.driver.find_element_by_xpath('//body').get_attribute("is") == "pages/optional/optional":
                    break

        logging.debug("\\n当前窗口:", self.driver.current_window_handle)
        logging.debug(self.driver.page_source)
        logging.debug("网页标题:", self.driver.title)

        # 点击上证指数,跳转到新窗口
        # time.sleep(5)
        WebDriverWait(self.driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.wx-swiper-slides wx-view wx-navigator'))).click()

        time.sleep(5)
        # for window in self.driver.window_handles:
        #     logging.info("点击上证指数后,所有 window 窗口:", window)
        #     logging.info("点击上证指数后,网页标题:", self.driver.title)
        #     logging.info(self.driver.page_source)

        if self.driver.find_element_by_xpath('//body').get_attribute("is") == "pages/stock/stock":
            logging.debug("\\nif 内 点击上证指数后,默认窗口:{}".format(self.driver.current_window_handle))
            logging.debug(self.driver.title)
            pass
        else:
            for i in range(0, 3):
                self.driver.switch_to.window(self.driver.window_handles[i])
                logging.debug("\\nelse 内 点击上证指数后, 默认窗口:{}".format(self.driver.current_window_handle))
                logging.debug(self.driver.page_source)
                logging.debug("网页标题:{}".format(self.driver.title))
                if self.driver.find_element_by_xpath('//body').get_attribute("is") == "pages/stock/stock":
                    break

        logging.debug("\\n点击上证指数后,当前窗口:", self.driver.current_window_handle)
        # print(self.driver.page_source)
        logging.debug("点击上证指数后,网页标题:", self.driver.title)

        time.sleep(2)
        self.driver.find_elements_by_css_selector('.period__list wx-view')[1].click()

        time.sleep(3)
        self.driver.find_elements_by_css_selector('.period__list wx-view')[2].click()

        time.sleep(3)
        self.driver.find_elements_by_css_selector('.period__list wx-view')[5].click()