def back_home(self): try: self.driver.keyevent(3) return True except Exception as e: ExceptionInfo(e) return False
def photo_share(self): try: self.press_by_text('微信') time.sleep(1) self.press_by_text('发现') time.sleep(1) self.press_by_text('朋友圈') time.sleep(0.5) # resource-id="com.tencent.mm:id/iw" self.press_attribute('content-desc', '拍照分享') time.sleep(0.8) self.press_by_text('从相册选择') time.sleep(0.5) x = self.driver.page_source d = Inspector(xmlstring=x).get_attributes() print(d) # 选择图片 self.press_attribute('content-desc', '图片 1, 2018-09-25 22:48') time.sleep(0.5) self.press_attribute('text', '完成(1/9)') time.sleep(0.5) self.press_attribute('text', '这一刻的想法...') time.sleep(1) self.input_text('text', '这一刻的想法...', '这是在测试程序,不要在意') time.sleep(2) # self.press_attribute('text', '发表') return True except Exception as e: ExceptionInfo(e) return False
def modify_json(path, data): try: with open(path, 'w') as file: file.write(json.dumps(data)) return True except Exception as e: ExceptionInfo(e) return False
def back(self): try: # resource-id="com.tencent.mm:id/j8" self.driver.find_element_by_xpath("//*[contains(@content-desc, '返回')]").click() return True except Exception as e: ExceptionInfo(e) return False
def press_attribute(self, attr, value): try: self.driver.find_element_by_xpath( "//*[contains(@{0}, '{1}')]".format(attr, value)).click() return True except Exception as e: ExceptionInfo(e) return False
def open_app_by_activity(self, app_package, app_activity): try: self.driver.start_activity(app_package, app_activity) return True pass except Exception as e: ExceptionInfo(e) return False pass
def read_json(path): """读取交易参数""" try: with open(path, encoding='utf-8') as file: content = json.load(file) return content except Exception as e: ExceptionInfo(e) return None
def open_notifications(self): """ :return: """ try: self.driver.open_notifications() return True except Exception as e: ExceptionInfo(e) return False
def add_contactors(self): """ 添加联系人 :return: """ try: # 打开联系人 self.open_app_by_activity( 'com.android.contacts', 'com.android.contacts.activities.PeopleActivity') # print(self.kwargs['contactors']) time.sleep(1) # TODO(leung): 右上角那个+号:NAF=true # d = Inspector(xmlstring=self.driver.page_source).get_attributes() # print(d) for d in self.kwargs['contactors']: try: self.press_attribute('NAF', 'true') time.sleep(1) self.press_attribute('text', '手机') time.sleep(0.5) self.input_text('text', '姓名', d['联系人']) time.sleep(1) # 联系人输入之后可能会出现询问是否合并的对话框 # 这时候点击顶部的‘新建联系人’忽略提示‘ self.press_attribute('text', '新建联系人') time.sleep(0.5) self.input_text('text', '电话', d['联系电话']) time.sleep(1) self.press_attribute('text', '完成') time.sleep(1) logger.success_info_print( self.udid + ': 联系人({0},{1}), 创建成功.'.format(d['联系人'], d['联系电话'])) except Exception as e: ExceptionInfo(e) logger.error_info_print( self.udid + ': 联系人({0},{1}), 创建失败.'.format(d['联系人'], d['联系电话'])) pass except Exception as e: ExceptionInfo(e)
def more(self): """ 点击右上角更多功能那个+号 :return: """ try: self.driver.find_element_by_xpath("//*[contains(@content-desc, '更多功能按钮')]").click() return True except Exception as e: ExceptionInfo(e) return False
def remove_file(cls, file): """ :param file:like "f:zjf/love.png" :return: """ try: os.remove(file) except Exception as e: # by:modify leungjian==print(e)->ExceptionInfo(e) # The same place behind is the same ExceptionInfo(e)
def __init__(self, host='127.0.0.1', port=4327): """ 通过appium命令行创建appium服务 """ try: cmd = '' # 创建appium的命令 self.appium = '' # appium服务的实例 pass except Exception as e: self.appium = None ExceptionInfo(e)
def open(self, name='微信'): """ 打开微信 微信有可能存在多开,那么桌面上可能存在多个微信应用, 此时就通过,不同的微信程序名称,打开微信 :return: """ try: return self.open_app_by_name(name) except Exception as e: ExceptionInfo(e) return False
def swipe_down(self, duration=1000): # 屏幕向上走 try: self.driver.swipe(start_x=self.size['width'] / 2, start_y=self.size['height'] / 3, end_x=self.size['width'] / 2, end_y=self.size['height'] * 2 / 3, duration=duration) return True except Exception as e: ExceptionInfo(e) return False
def sound_notice(sound_name): """ 以多线程的方式播放一段音频文件 :param sound_name: :return: """ try: t = threading.Thread(target=play_music, args=(sound_name, )) return t except Exception as e: ExceptionInfo(e) pass
def input_text(self, attr, value, text): """ 通过属性键值对,选择输入框进行文字输入 :return: """ try: self.driver.find_element_by_xpath( "//*[contains(@{0}, '{1}')]".format(attr, value)).send_keys(text) return True except Exception as e: ExceptionInfo(e) return False
def get_attributes(self): try: data = [] for element in self.tree.getiterator(): dict_keys = {} if element.keys(): for name, value in element.items(): dict_keys[name] = value # print(dict_keys) data.append(dict_keys) data = pd.DataFrame(data) return data except Exception as e: ExceptionInfo(e)
def search_button(self): """ 微信各个页面上的放大镜搜索按钮都可以通过这个函数点击 点击右上角的搜索,搜索输入框下面那个提示性文字都是text ‘搜索’提示为text属性,可点 :return: """ try: # 点击桌面上的“搜索”那个图片 self.driver.find_element_by_xpath("//*[contains(@content-desc, '搜索')]").click() return True except Exception as e: ExceptionInfo(e) return False
def get_current_text_element(self): """ 获取当前页面上的文字元素, 在xml中一般以属性列示的,包括text... 获取这些元素是为了,判断当前处在哪个页面 :return: """ try: # 当前页面的xml描述 xml = self.driver.page_source pass except Exception as e: ExceptionInfo(e) pass
def home_search(self, text): """ 在微信主页上的那个放大镜下搜索 :param text: :return: """ try: # 一般进入搜索页面后,搜索输入框都是已经 # 获得焦点的, self.driver.find_element_by_xpath("//*[contains(@content-desc, '搜索')]").send_keys('text') pass except Exception as e: ExceptionInfo(e) return False
def get_device_mac(cls, udid): """ 获取设备的MAC地址 :param udid: :return: """ try: cmd = 'adb -s {udid} shell cat /sys/class/net/wlan0/address'.\ format(udid=udid) r = os.popen(cmd).read().strip().replace('\n', '') return r pass except Exception as e: ExceptionInfo(e) return None
def press_by_text(self, txt): """ 找到当前页面上的某个文字,然后点击它 1.可能会找不到,报错 2.可能找到了多个,会点击第一个 :param txt: :return: """ try: self.driver.find_element_by_xpath( "//*[contains(@text, '{}')]".format(txt)).click() return True except Exception as e: ExceptionInfo(e) return False
def swipe_right(self, duration=1000): """ 向右滑动屏幕的二分之一, 持续时间1秒 -> :return: """ try: self.driver.swipe(start_x=self.size['width'] / 2, start_y=self.size['height'] / 2, end_x=self.size['width'] - 1, end_y=self.size['height'] / 2, duration=duration) return True except Exception as e: ExceptionInfo(e) return False
def get_wifi_password(cls, udid): """ 获取设备的MAC地址 :param udid: :return: """ try: cmd = 'adb -s {udid} shell cat /data/misc/wifi/*.conf'. \ format(udid=udid) r = os.popen(cmd).read().strip().replace('\n', '') return r pass except Exception as e: ExceptionInfo(e) return None
def swipe_up(self, duration=1000): """ 向上滑动屏幕的三分之一, 持续时间1秒 :return: """ try: self.driver.swipe(start_x=self.size['width'] / 2, start_y=self.size['height'] * 2 / 3, end_x=self.size['width'] / 2, end_y=self.size['height'] / 3, duration=duration) return True except Exception as e: ExceptionInfo(e) return False
def find_attribute_key(self, value): """ 通过值找到属性名 :param value: :return: """ try: data = [] for element in self.tree.getiterator(): if element.keys(): for k, v in element.items(): if v == value: data.append({k: v}) return data except Exception as e: ExceptionInfo(e)
def find_attribute_value(self, key): """ 通过属性名找到值 :param value: :return: """ try: data = [] for element in self.tree.getiterator(): if element.keys(): for k, v in element.items(): if k == key: data.append({k: v}) return data except Exception as e: ExceptionInfo(e)
def page_diff(page1, page2): """ 计算两个页面的相似程度 :param page1: 页面1的xml字符串 :param page2: 页面2的xml字符串 :return: ratio """ try: if not isinstance(page1, str) or not isinstance(page2, str): raise TypeError('page1 and page2 must be a string') else: return difflib.SequenceMatcher(None, page1, page2).ratio() pass except Exception as e: ExceptionInfo(e) return 0
def connect_wifi(self, name, password): """ 连接WiFi :param name: :param password: :return: """ try: # 手机已经打开WiFi了 if self.open_app_by_activity('com.android.settings', 'com.android.settings.Settings'): self.press_attribute('text', self.button['WLAN']) time.sleep(1) self.press_attribute('text', name) time.sleep(0.5) d = Inspector( xmlstring=self.driver.page_source).get_attributes() # print(d) keys = d.text.tolist() if isin('已连接', keys): # 已连接 self.press_attribute('text', '取消') time.sleep(0.5) self.back_home() return True elif isin(['忘记网络', '连接'], keys): # 以前连接过 self.press_attribute('text', '连接') time.sleep(0.5) self.back_home() return True else: self.input_text('text', '密码', password) time.sleep(2) self.press_attribute('text', '连接') time.sleep(5) self.back_home() # 不会检查是否连接成功了没有 return True else: return False pass except Exception as e: ExceptionInfo(e) self.back_home() return False
def add_pyq(self, text, image=None): """ 发朋友圈 :param text: :param image: :return: """ try: self.press_by_text('微信') time.sleep(1) self.press_by_text('发现') time.sleep(1) self.press_by_text('朋友圈') time.sleep(0.5) self.photo_share() except Exception as e: ExceptionInfo(e)