appium控制多个夜神模拟器

重复代码比较多没有优化,商单考虑是代码的健壮性

from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import time
import threading


class wx_app:

    def __init__(self, driver):
        self.driver = driver

    def wait_a_moment(self, element):
        WebDriverWait(self.driver, 40).until(lambda x: x.find_element_by_android_uiautomator(element))
        time.sleep(0.2)

    def wait_existence(self, element, wait_time=40):
        WebDriverWait(self.driver, wait_time).until(lambda x: x.find_elements_by_id(element))
        time.sleep(0.2)

    def back_button(self):
        # 点击返回按钮
        self.wait_existence("com.tencent.mm:id/ei")
        self.driver.find_element(By.ID, 'com.tencent.mm:id/ei').click()

    def swipe_down(self, t=600, n=1):
        # 向下滑动屏幕
        l = self.driver.get_window_size()  # 获取屏幕尺寸
        print(l)
        # 根据比率来滑动屏幕
        x1 = l['width'] * 0.1  # x坐标
        y1 = l['height'] * 0.3  # 起始y坐标
        y2 = l['height'] * 0.5  # 终点y坐标

        for i in range(n):  # n为1,只滑动一次
            self.driver.swipe(x1, y1, x1, y2, t)

    def wx_find(self):
        self.wait_a_moment('new UiSelector().text("发现")')
        self.driver.find_element_by_android_uiautomator('new UiSelector().text("发现")').click()

    def circle_of_friends(self):
        self.wait_a_moment('new UiSelector().text("朋友圈")')
        self.driver.find_element_by_android_uiautomator('new UiSelector().text("朋友圈")').click()
        time.sleep(5)

    def pyq_content(self):
        # 获取朋友第一条文案
        self.wait_existence("com.tencent.mm:id/bmy")
        xpath = "//*[@resource-id='com.tencent.mm:id/hyd' and @index='1']//*[@resource-id='com.tencent.mm:id/bmy']"
        return self.driver.find_element_by_xpath(xpath).text

    def personal_information(self):
        # 进入发朋友圈的资料里面
        self.wait_existence("com.tencent.mm:id/iu")
        self.driver.find_element(By.ID, "com.tencent.mm:id/iu").click()
        time.sleep(3)

    def recommend(self):
        # 点击右上角三个点
        self.wait_existence("com.tencent.mm:id/d8")
        self.driver.find_element(By.ID, "com.tencent.mm:id/d8").click()

    def recommend_you(self):
        # 点击把他推荐给好友
        self.wait_existence("com.tencent.mm:id/h8z")
        self.driver.find_element_by_xpath('//*[contains(@text,"推荐给朋友")]').click()

    def search_friend(self):
        # 搜索好友
        self.wait_a_moment('new UiSelector().text("搜索")')
        self.driver.find_element_by_android_uiautomator('new UiSelector().text("搜索")').click()
        self.driver.find_element_by_android_uiautomator('new UiSelector().text("搜索")').send_keys("xilalala")

    def click_friend(self):
        # 点击搜索出来的好友
        self.wait_existence("com.tencent.mm:id/ir3")
        self.driver.find_element(By.ID, 'com.tencent.mm:id/ir3').click()

    def friend_msg(self, rest):
        # 给朋友留言
        self.wait_a_moment('new UiSelector().text("给朋友留言")')
        self.driver.find_element(By.ID, 'com.tencent.mm:id/b_h').send_keys(rest)

    def click_send_button(self):
        # 点击发送按钮
        self.wait_a_moment('new UiSelector().text("发送")')
        self.driver.find_element_by_android_uiautomator('new UiSelector().text("发送")').click()


desired_caps1 = {"platformName": "Android",
                 "platformVersion": "7.1.2",
                 "deviceName": "127.0.0.1:62001",
                 "appPackage": "com.tencent.mm",
                 "appActivity": ".ui.LauncherUI",
                 "noReset": "true",
                 "unicodeKeyboard": 'true',
                 "resetKeyboard": "true",
                 "udid": "127.0.0.1:62001"}

desired_caps2 = {"platformName": "Android",
                 "platformVersion": "7.1.2",
                 "deviceName": "127.0.0.1:62026",
                 "appPackage": "com.tencent.mm",
                 "appActivity": ".ui.LauncherUI",
                 "noReset": "true",
                 "unicodeKeyboard": 'true',
                 "resetKeyboard": "true",
                 "udid": "127.0.0.1:62026"}

w_list = []


class wx_app_test:
    def __init__(self, driver):
        self.wx = wx_app(driver)
        # self.wx = wx_app()

    def test01(self):
        self.wx.wx_find()
        self.wx.circle_of_friends()

    def test02(self):
        # 下滑屏幕-每隔30s滑动一下
        time.sleep(25)
        self.wx.swipe_down()
        time.sleep(5)
        rest = self.wx.pyq_content()
        if rest not in w_list:
            w_list.append(rest)
        else:
            return
        self.wx.personal_information()
        self.wx.recommend()
        self.wx.recommend_you()
        self.wx.search_friend()
        self.wx.click_friend()
        self.wx.friend_msg(rest)
        self.wx.click_send_button()
        for i in range(2):
            time.sleep(0.5)
            self.wx.back_button()


def task1():
    while True:
        try:
            app = wx_app_test(webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps1))
            app.test01()
            while True:
                app.test02()
        except Exception as e:
            print(repr(e))
            continue


def task2():
    while True:
        try:
            app = wx_app_test(webdriver.Remote('http://127.0.0.1:4725/wd/hub', desired_caps2))
            app.test01()
            while True:
                app.test02()
        except Exception as e:
            print(repr(e))
            continue


threads = []
t1 = threading.Thread(target=task1)
threads.append(t1)

t2 = threading.Thread(target=task2)
threads.append(t2)

if __name__ == '__main__':
    for t in threads:
        t.start()
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于使用Python和Appium夜神模拟器进行自动化测试,你可以按照以下步骤进行操作: 1. 安装Appium:使用pip命令安装Appium库。在命令行输入以下命令: ``` pip install Appium-Python-Client ``` 2. 安装夜神模拟器:你需要先在电脑上安装夜神模拟器,并确保它可以正常运行。 3. 配置Appium连接夜神模拟器:在代码中设置Appium连接夜神模拟器的相关参数,例如设备名称、设备版本、App包名和启动Activity等。 4. 编写测试脚本:使用Python编写Appium测试脚本。你可以使用Appium提供的API来控制模拟器的操作,如启动应用、查找元素、点击按钮等。 以下是一个简单的示例代码,用于启动夜神模拟器上的应用并点击按钮: ```python from appium import webdriver desired_caps = { 'platformName': 'Android', 'deviceName': '夜神模拟器', 'platformVersion': '夜神模拟器的版本号', 'appPackage': '应用的包名', 'appActivity': '应用的启动Activity' } driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) # 查找元素并点击按钮 button = driver.find_element_by_id('按钮的id') button.click() # 关闭模拟器连接 driver.quit() ``` 请确保你已经正确安装了Appium夜神模拟器,并且使用正确的参数来配置连接。这只是一个简单的示例,你可以根据自己的需求进行扩展和修改。 希望对你有所帮助!如果你还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值