def __init__(self, udid): self.imobile_cmder = IMobileDevice(udid) self.__iproxy = IProxyManager(udid) self.__engine_connector = None self.__wda_connector = None def __get_wdaport_by_udid(udid): outputlines = CmdExecuter.executeAndWait( "ps -ef|grep \"iproxy [0-9]\+ 8100 " + udid + "\"") for line in outputlines: ret = re.search("iproxy ([0-9]+) 8100 " + udid, line.decode("utf-8")) if ret: return ret.group(1) outputlines = CmdExecuter.executeAndWait( "ps -ef|grep \"iproxy [0-9]\+ 8100" + "\"") for line in outputlines: ret = re.search("iproxy ([0-9]+) 8100$", line.decode("utf-8")) if ret: return ret.group(1) logger.warn("No wda port detected for device.. " + udid) return None wdaport = os.environ.get("WDA_LOCAL_PORT", __get_wdaport_by_udid(udid)) if wdaport: logger.info("wda port detected: " + wdaport) self.__wda_connector = WdaManager( wdaport, os.environ.get("PLATFORM_IP", "127.0.0.1")) atexit.register(self.__cleanup) pass
def touch(self, x, y): if 0 < x < 1 and 0 < y < 1: display_size = self.display_size() x = x * display_size[0] y = y * display_size[1] logger.info("tap :" + str(x) + str(y)) self.__wda_connector.get_session().tap(x, y)
def __default_alert_callback(self, session): btns = set([u'稍后', u'稍后提醒我', u'不再提醒', u'无线局域网与蜂窝移动网络', u'允许', u'知道了', u'确定', u'好']).intersection( session.alert.buttons()) if len(btns) == 0: logger.warn("Alert not handled, buttons: " + ', '.join(session.alert.buttons())) return logger.info('alert handled:' + str(list(btns)[0])) session.alert.click(list(btns)[0]) pass
def start_alert_handler(self, handler=None): if self.__wda_connector is None: logger.error("wda connector is not inited... plsease make sure wda is connectable") return ERR_WDA_NOT_RUNNING if handler is None: handler = self.__default_alert_callback logger.info("setting alert handler...") self.__wda_connector.get_session().set_alert_callback(handler) return ERR_SUCCEED
def login_qq(self, account, password): account = os.environ.get("OTHERNAME", os.environ.get("QQNAME", account)) password = os.environ.get("OTHERPWD", os.environ.get("QQPWD", password)) session = self.device.wda_session() if session(className='Button', name=u'取消').exists and session( className='Button', nameContains=u'打开').exists: session(className='Button', name=u'打开').get().tap() logger.info("打开...") time.sleep(1) self.device.start_alert_handler() if session(className='NavigationBar', name=u'QQ一键登录').exists and session( className='Button', nameContains=u'登录').exists: session(className='Button', name=u'登录').get().tap() return ga2.ERR_SUCCEED if session(className='Button', name=u'授权并登录').exists: session(className='Button', name=u'授权并登录').get().tap() time.sleep(2) return ga2.ERR_SUCCEED #first time to login if session(className='Button', name=u'新用户').exists and session( className='Button', nameContains=u'登录').exists: session(className='Button', name=u'登录').get().tap() time.sleep(1) if session(className='TextField', name=u'帐号').exists: session(className='TextField', name=u'帐号').get().tap() time.sleep(1) session(className='TextField', name=u'帐号').get().set_text(account) time.sleep(1) session(className='SecureTextField', value=u'密码').tap() time.sleep(1) session(className='SecureTextField', value=u'密码').set_text(password) time.sleep(1) session(className='Button', name=u'登录按钮').get().tap() if session(className='StaticText', name=u'登录失败').exists: return ga2.ERR_LOGIN_FAILED if session(className='NavigationBar', name=u'绑定手机号码').exists: session(className='Button', name=u'关闭').get().tap() if session(className='StaticText', name=u'确定关闭吗?').exists: session(className='Button', name=u'关闭').get().tap() if session(className='Button', name=u'取消').exists and session( className='Button', nameContains=u'打开').exists: session(className='Button', name=u'打开').get().tap() logger.info("打开...") time.sleep(1) time.sleep(2) return ga2.ERR_SUCCEED return ga2.ERR_LOGIN_FAILED
def login_tencent(self, account, password): if self.device is None: logger.error("login_tencent: device is none...") return ga2.ERR_LOGIN_FAILED top_app = self.device.get_top_app() logger.info("top bundle id: " + self.device.get_top_app()) if top_app == TencentLoginHelper.QQ_BUNDLE_ID: return self.login_qq(account=account, password=password) elif top_app == TencentLoginHelper.WECHAT_BUNDLE_ID: return self.login_wechat(account=account, password=password) else: #sometimes there are alerts, try to login both qq and wechat if self.login_qq(account=account, password=password) == ga2.ERR_LOGIN_FAILED: return self.login_wechat(account=account, password=password) return ga2.ERR_SUCCEED
def launch(self, appid, timeout=60, alert_handler=None): if self.__wda_connector is None: logger.error("wda connector is not inited... plsease make sure wda is connectable") return ERR_WDA_NOT_RUNNING session = self.__wda_connector.new_session(bundleid=appid) if alert_handler is None: alert_handler = self.__default_alert_callback for i in range(0, 10): time.sleep(3) cur_top_app = self.get_top_app() logger.info("current top app is :" + cur_top_app + "target is: " + appid) if cur_top_app != appid: alert_handler(session) else: break return ERR_SUCCEED
def login_wechat(self, account, password): account = os.environ.get("OTHERNAME", os.environ.get("WECHATNAME", account)) password = os.environ.get("OTHERPWD", os.environ.get("WECHATPWD", password)) session = self.device.wda_session() if session(className='Button', name=u'取消').exists and session( className='Button', nameContains=u'打开').exists: session(className='Button', name=u'打开').get().tap() logger.info("打开...") time.sleep(1) self.device.start_alert_handler() if session(className='Button', name=u'注册').exists and session( className='Button', nameContains=u'登录').exists: logger.info(u'注册 found') session(className='Button', name=u'登录').get().tap() time.sleep(1) if session(className='Button', name=u'用微信号/QQ号/邮箱登录').exists: session(className='Button', name=u'用微信号/QQ号/邮箱登录').tap() time.sleep(1) if session(className='TextField', value=u'微信号/QQ号/邮箱').exists: session(className='TextField', value=u'微信号/QQ号/邮箱').get().tap() time.sleep(1) session(className='TextField', value=u'微信号/QQ号/邮箱').get().set_text(account) time.sleep(1) session(className='SecureTextField', value=u'请填写密码').tap() time.sleep(1) session(className='SecureTextField', value=u'请填写密码').set_text(password) time.sleep(1) session(className='Button', name=u'登录').get().tap() time.sleep(3) if session(className='Alert', name=u'帐号或密码错误,请重新填写。').exists: return ga2.ERR_LOGIN_FAILED if session(className='Button', name=u'取消').exists and session( className='Button', nameContains=u'打开').exists: session(className='Button', name=u'打开').get().tap() logger.info("打开...") time.sleep(1) return ga2.ERR_SUCCEED return ga2.ERR_LOGIN_FAILED