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 test03(self):
while True:
try:
self.test01()
while True:
self.test02()
except Exception as e:
continue
def task1():
try:
wx_app_test(webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps1)).test03()
except Exception as e:
wx_app_test(webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps1)).test03()
def task2():
try:
wx_app_test(webdriver.Remote('http://127.0.0.1:4725/wd/hub', desired_caps2)).test03()
except Exception as e:
wx_app_test(webdriver.Remote('http://127.0.0.1:4725/wd/hub', desired_caps2)).test03()
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()