def WaitElement(webDriver, method, timeout=settings.ELEMENTTIMEOUT): ''' set find elements auto wait :param webDriver: the start webdriver :param method: the method of check page element :param timeout: Timeout :return: element Example: from appo.specialUi import WaitElement \n element = WaitElement(driver, lambda x: x.find_element_by_id("someId"), 10) ''' try: elements = WebDriverWait(webDriver, timeout).until(method) except TimeoutException, e: appiumautolog().error(u"页面加载超时 [%s] %s" % (method, e))
def WaitElement(webDriver, method, timeout = settings.ELEMENTTIMEOUT): ''' set find elements auto wait :param webDriver: the start webdriver :param method: the method of check page element :param timeout: Timeout :return: element Example: from appo.specialUi import WaitElement \n element = WaitElement(driver, lambda x: x.find_element_by_id("someId"), 10) ''' try: elements = WebDriverWait(webDriver, timeout).until(method) except TimeoutException, e: appiumautolog().error(u"页面加载超时 [%s] %s" %(method, e))
class appload: def __init__(self, device): if isinstance(device, str): self.device = device else: raise ValueError(u'驱动编号必须是一个字符串!') def startapp(self, activity=apkmess(settings.APKPATH).launchable_activity): ''' :param activityName: android activity name :return: driver ''' desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = mobilehard( self.device).mobile_android_version desired_caps['deviceName'] = self.device app_info = apkmess(settings.APKPATH) desired_caps['appPackage'] = app_info.package_name desired_caps['appActivity'] = activity try: appiumautolog().debug(u'appium启动信息 %s' % desired_caps) self.driver = webdriver.Remote(settings.APPIUMSERVER, desired_caps) except URLError, e: appiumautolog().error(u'请检果appium服务器地址是否正确! \t %s ' % e) except WebDriverException, err: appiumautolog().error(u'appium服务器被占用!, \t %s' % err)
def startapp(self, activity=apkmess(settings.APKPATH).launchable_activity): ''' :param activityName: android activity name :return: driver ''' desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = mobilehard(self.device).mobile_android_version desired_caps['deviceName'] = self.device app_info = apkmess(settings.APKPATH) desired_caps['appPackage'] = app_info.package_name desired_caps['appActivity'] = activity try: appiumautolog().debug(u'appium启动信息 %s' % desired_caps) self.driver = webdriver.Remote(settings.APPIUMSERVER, desired_caps) except URLError, e: appiumautolog().error(u'请检果appium服务器地址是否正确! \t %s ' % e)
def execadbshell(self, shell): p = subprocess.Popen(shell, universal_newlines=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True,) while True: if p.poll() == 0: break elif p.poll() == -1: appiumautolog.error('执行adb命令[%s],系统中adb环境存在问题!' % shell) break else: time.sleep(0.5) rData = p.stdout.readlines() appiumautolog().debug('执行adb命令[%s],返回结果 %s' % (shell, rData)) return rData
def startapp(self, activity=apkmess(settings.APKPATH).launchable_activity): ''' :param activityName: android activity name :return: driver ''' desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = mobilehard( self.device).mobile_android_version desired_caps['deviceName'] = self.device app_info = apkmess(settings.APKPATH) desired_caps['appPackage'] = app_info.package_name desired_caps['appActivity'] = activity try: appiumautolog().debug(u'appium启动信息 %s' % desired_caps) self.driver = webdriver.Remote(settings.APPIUMSERVER, desired_caps) except URLError, e: appiumautolog().error(u'请检果appium服务器地址是否正确! \t %s ' % e)
def execadbshell(self, shell): p = subprocess.Popen( shell, universal_newlines=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, ) while True: if p.poll() == 0: break elif p.poll() == -1: appiumautolog.error('执行adb命令[%s],系统中adb环境存在问题!' % shell) break else: time.sleep(0.5) rData = p.stdout.readlines() appiumautolog().debug('执行adb命令[%s],返回结果 %s' % (shell, rData)) return rData
def devices(): ''' :return:成功连接手机的可用驱动编号 ''' devices_command = "adb devices" command_out = os.popen(devices_command).readlines() if len(command_out) >= 3: devices_all = command_out[1:-1] devices = [] for device_info in devices_all: device = device_info.strip("\n").split("\t") if len(device) == 2: device_name = device[0] device_status = device[1].strip("\n") if device_status == "device": devices.append(device_name) else: appiumautolog().error(u"连接编号为%s的手机未获得与电脑连接的权限!" % device_name) raise Exception(u"连接编号为%s的手机未获得与电脑连接的权限!" % device_name) return devices else: appiumautolog().error(u"系统中adb命令不能正常使用或手机未正常连接!") raise AttributeError(u"系统中adb命令不能正常使用或手机未正常连接!")
def coverinstall(self, apkpath = settings.APKPATH): ''' :param apkpath: 需要被安装的app的路径,默认为配置文件路径 :return:Ture or False ''' ins_command = AdbCommand(self.device).shell.get("coverInd") + ' ' + apkpath run = mobilehard(self.device).execadbshell(ins_command) if run: result = [x for x in run if x!='\n'][1] if re.match(r"^Failure*", result): appiumautolog().error(u"此版本不能覆盖比自己高的版本!") return False elif re.match(r"^Success*", result): return True else: appiumautolog().error(u"adb命令运行有误!") raise Exception(u"adb命令运行有误!") else: appiumautolog().error(u"adb命令运行有误!") raise Exception(u"adb命令运行有误!")
def coverinstall(self, apkpath=settings.APKPATH): ''' :param apkpath: 需要被安装的app的路径,默认为配置文件路径 :return:Ture or False ''' ins_command = AdbCommand( self.device).shell.get("coverInd") + ' ' + apkpath run = mobilehard(self.device).execadbshell(ins_command) if run: result = [x for x in run if x != '\n'][1] if re.match(r"^Failure*", result): appiumautolog().error(u"此版本不能覆盖比自己高的版本!") return False elif re.match(r"^Success*", result): return True else: appiumautolog().error(u"adb命令运行有误!") raise Exception(u"adb命令运行有误!") else: appiumautolog().error(u"adb命令运行有误!") raise Exception(u"adb命令运行有误!")