def closePassword(self): pwd = gl.get_value('tradepassword') if gl.get_value('platformName') == 'Android': status = getText(self.driver, getElement('密码管理', '开启状态')) if status == '交易密码: 关闭': loggerInfo(status) elif status == '交易密码: 开启': click(self.driver, getElement('密码管理', '开关按钮')) switchToWebView(self.driver) click(self.driver, getElement('存管交易密码', '密码输入框')) sendKeys(self.driver, getElement('存管交易密码', '密码输入框'), pwd) click(self.driver, getElement('存管交易密码', '确认按钮')) switchToNative(self.driver) reslut = getText(self.driver, getElement('密码管理', '结果标题')) if reslut == '关闭成功': click(self.driver, getElement('密码管理', '完成')) else: if isExistElement(self.driver, getElement('密码管理', 'ios关闭状态')) is False: # 开启的状态 click(self.driver, getElement('密码管理', '开关按钮')) switchToWebView(self.driver) click(self.driver, getElement('存管交易密码', '密码输入框')) time.sleep(2) sendKeys(self.driver, getElement('存管交易密码', '密码输入框'), pwd) click(self.driver, getElement('存管交易密码', '确认按钮')) switchToNative(self.driver) if isExistElement(self.driver, getElement('密码管理', 'ios关闭结果标题')) is True: click(self.driver, getElement('密码管理', '完成')) else: loggerInfo(getElement('密码管理', '开启状态'))
def getElement(ModeName, Element): page = getPage(ModeName) filePath = os.path.join(os.path.dirname(__file__), '../CsvFile/' + page + '.csv') file = os.path.abspath(filePath) element = [] try: data = pandas.read_csv(file, encoding='gbk') # [Name AndroidLocation AndroidElement IOSLocation IOSElement] for s in data.values: if s[0] == Element: if gl.get_value('platformName') == 'Android': if gl.get_value('platformVersion') == '4.4.4' or gl.get_value('platformVersion') == '4.4': if s[1] == 'android_uiautomator': s[1] = 'xpath' new_s = s[2][6:-2] new_ele = """//android.widget.TextView[@text='{0}']""".format(new_s) s[2] = new_ele element.append(s[1]) element.append(s[2]) element.append(s[0]) return element elif gl.get_value('platformName') == 'ios': element.append(s[3]) element.append(s[4]) element.append(s[0]) return element except Exception as e: loggerError(e)
def createPerformanceFile(ip): os.system( gl.get_value('adb') + ' -s ' + ip + ' shell rm -r -f sdcard/Prism') os.system(gl.get_value('adb') + ' -s ' + ip + ' shell mkdir sdcard/Prism') os.system( gl.get_value('adb') + ' -s ' + ip + ' shell touch sdcard/Prism/com.junte.txt')
def connectApp(ip): count = 0 max_count = 3600 while True: if count < max_count: time.sleep(1) # loggerInfo(gl.get_value('adb') + ' devices') adbDevice = getOutPut(gl.get_value('adb') + ' devices') loggerInfo(adbDevice) s_ip = ip + '\tdevice' s_error = ip + '\toffline' if s_ip in adbDevice: break else: if s_error in adbDevice: os.system(gl.get_value('adb') + ' disconnect ' + ip) # loggerInfo(gl.get_value('adb') + ' connect ' + ip) result = getOutPut(gl.get_value('adb') + ' connect ' + ip) loggerInfo(result) if 'already' in result: break elif 'failed' in result: loggerError('设备连接异常') break time.sleep(30) count = count + 30 else: assert count < max_count, 'adb连接测试设备超过1小时,脚本执行失败'
def DeviceStates(ip): if gl.get_value('stf') == 'true': # linux if gl.get_value('platformVersion') == '4.4.4': result = getOutPut( gl.get_value('adb') + ' -s ' + ip + ' shell dumpsys power | grep "mScreenOn"') else: result = getOutPut( gl.get_value('adb') + ' -s ' + ip + ' shell dumpsys power | grep "Display Power"') else: # windows # loggerInfo(gl.get_value('adb') + ' -s ' + ip + ' shell dumpsys power | findstr "Display Power:state="') result = getOutPut( gl.get_value('adb') + ' -s ' + ip + ' shell dumpsys power | findstr "Display Power:state="') if 'Display Power: state=ON' in result or 'mScreenOn=true' in result: # 屏幕已唤醒 loggerInfo('屏幕已唤醒') return True elif 'Display Power: state=OFF' in result: loggerInfo('屏幕未唤醒') return False else: loggerError('命令出错:' + result) return None
def choose(self, page, element): ele = getElement(page, element) if gl.get_value('platformName') == 'Android': click(self.driver, ele) elif gl.get_value('platformName') == 'ios': if element == '教育培训': ele_ios = getElement(page, '完成') click(self.driver, ele_ios)
def pullPerformanceFile(ip): result = getOutPut(gl.get_value('adb') + ' devices') if 'List of devices attached' == result: connectApp(gl.get_value('deviceName')) os.system( gl.get_value('adb') + ' -s ' + ip + ' pull /sdcard/Prism/com.junte.txt ../PrismReport/data') os.system( gl.get_value('adb') + ' -s ' + ip + ' shell rm -r -f sdcard/Prism')
def findElement(driver, loc): if gl.get_value('platformName') == 'Android': element = waitElement(driver, loc[0], loc[1]) elif gl.get_value('platformName') == 'ios': element = waitElement_ios(driver, loc[0], loc[1]) if element is not None: loggerInfo('已找到元素-->[' + loc[2] + ']') else: loggerInfo('未找到元素-->[' + loc[2] + ']') return element
def reloadLogin(self, telno): password = gl.get_value('password') click(self.driver, getElement('底部', '我')) if findElement(self.driver, getElement('我', '登录注册按钮')) is None: doSwipe(self, '上') click(self.driver, getElement('我', '设置按钮')) click(self.driver, getElement('设置', '退出账号')) if findElement(self.driver, getElement('设置', '确定退出账号')) is not None: click(self.driver, getElement('设置', '确定')) if findElement(self.driver, getElement('我', '登录注册按钮')) is not None: loggerInfo('退出登录成功') gl.set_value('login', False) login_telno(self, telno, password)
def ClickElement(self, page, element): ele = getElement(page, element) click(self.driver, ele) # 用于投资智享标时,点击确认承接,金额自动调整后,再次点击确认承接 if gl.get_value('platformName') == 'Android': if findElement( self.driver, ['id', 'com.junte:id/snackbar_text', '金额必须为X元']) is not None: time.sleep(2) click(self.driver, ele) elif gl.get_value('platformName') == 'ios': if findElement(self.driver, ['id', 'iosHLinvestButton', '金额必须为X元']) is not None: time.sleep(2) click(self.driver, ele)
def backHome(self): count = 0 while 7 > count: if findElement(self.driver, getElement('底部', '首页')) is None: if gl.get_value('platformName') == 'Android': goBack(self.driver) elif gl.get_value('platformName') == 'ios': if ios_goBack(self.driver) is False: break count = count + 1 loggerInfo('点击返回') else: loggerInfo('已回到首页') break assert findElement(self.driver, getElement('底部', '首页')) is not None, '回到首页失败'
def loginingOut(self): click(self.driver, getElement('底部', '我')) if findElement(self.driver, getElement('我', '登录注册按钮')) is None: doSwipe(self, '上') click(self.driver, getElement('我', '设置按钮')) click(self.driver, getElement('设置', '退出账号')) if findElement(self.driver, getElement('设置', '确定退出账号')) is not None: click(self.driver, getElement('设置', '确定')) if findElement(self.driver, getElement('我', '登录注册按钮')) is not None: loggerInfo('退出登录成功') gl.set_value('login', False) else: loggerError('退出登录失败') else: loggerInfo('未登录') gl.set_value('login', False)
def doLogin(self): if findElement(self.driver, getElement('登录', '标题')) is None: if findElement(self.driver, getElement('注册', '马上登录')) is not None: click(self.driver, getElement('注册', '马上登录')) username = gl.get_value('username') password = gl.get_value('password') # 登录操作 user = getElement('登录', '用户名输入框') click(self.driver, user) clear(self.driver, user) sendKeys(self.driver, user, username) pwd = getElement('登录', '密码输入框') click(self.driver, pwd) clear(self.driver, pwd) sendKeys(self.driver, pwd, password) login_two(self)
def cgtPwd(self): pwd = gl.get_value('tradepassword') click(self.driver, getElement('存管交易密码', '密码输入框')) time.sleep(2) sendKeys(self.driver, getElement('存管交易密码', '密码输入框'), pwd) if isExistElement(self.driver, getElement('存管交易密码', '我同意')) is True: click(self.driver, getElement('存管交易密码', '我同意')) click(self.driver, getElement('存管交易密码', '确认按钮'))
def doClickByXY(self, page, element): if gl.get_value('platformName') == 'ios': if element == '消息中心': se = getSize(self.driver) width = se[0] height = se[1] clickByXY(self.driver, width / 10, height / 20) else: click(self.driver, getElement(page, element)) else: click(self.driver, getElement(page, element))
def login_telno(self, telno, paswd): click(self.driver, getElement('我', '登录注册按钮')) username = telno password = paswd if findElement(self.driver, getElement('登录', '标题')) is None: if findElement(self.driver, getElement('注册', '马上登录')) is not None: click(self.driver, getElement('注册', '马上登录')) # 登录操作 user = getElement('登录', '用户名输入框') click(self.driver, user) clear(self.driver, user) sendKeys(self.driver, user, username) pwd = getElement('登录', '密码输入框') click(self.driver, pwd) clear(self.driver, pwd) sendKeys(self.driver, pwd, password) login_two(self) Gesture(self) if findElement(self.driver, getElement('我', '用户名')) is not None: gl.set_value('login', True) else: gl.set_value('login', False) loggerError('登录失败')
def Gesture(self): if gl.get_value('platformName') == 'Android': if findElement(self.driver, getElement('手势密码', '绘制图')) is not None: list_pwd = findElements( self.driver, ['className', 'android.widget.ImageView', '手势密码绘制点']) TouchAction(self.driver).press(list_pwd[1]).wait(300).move_to( list_pwd[1]).wait(300).move_to(list_pwd[2]).wait(100).move_to( list_pwd[3]).release().perform() loggerInfo('绘制手势密码') if findElement(self.driver, getElement('手势密码', '绘制图')) is not None: TouchAction(self.driver).press(list_pwd[1]).wait(300).move_to( list_pwd[1]).wait(300).move_to( list_pwd[2]).wait(100).move_to( list_pwd[3]).release().perform() loggerInfo('再次绘制手势密码') else: # ios点击 if findElement(self.driver, getElement('手势密码', '绘制图')) is not None: # start = findElement(self.driver, getElement('手势密码', '1')) # start_height = start.size['height'] # start_width = start.size['width'] # start_x = start.location['x'] # start_y = start.location['y'] # begin_x = start_x + start_width / 2 # begin_y = start_y + start_height / 2 # TouchAction(self.driver).press(x=start_x, y=start_y).wait(800).move_to(x=start_x + start_width * 2, # y=begin_y).wait(800).move_to( # x=start_x + start_width * 4, y=begin_y).perform().release() # loggerInfo('绘制手势密码') # if findElement(self.driver, getElement('手势密码', '绘制图')) is not None: # TouchAction(self.driver).press(x=start_x, y=start_y).wait(800).move_to(x=start_x + start_width * 2, # y=begin_y).wait(800).move_to( # x=start_x + start_width * 4, y=begin_y).perform().release() # loggerInfo('再次绘制手势密码') start = findElement(self.driver, getElement('手势密码', '1')) start2 = findElement(self.driver, getElement('手势密码', '2')) start3 = findElement(self.driver, getElement('手势密码', '3')) try: loggerInfo('绘制手势密码开始') TouchAction(self.driver).press(start).wait(500).move_to( start2).wait(500).move_to(start3).perform().release() except RuntimeError as e: loggerError(e) loggerInfo('绘制手势密码') if findElement(self.driver, getElement('手势密码', '绘制图')) is not None: TouchAction(self.driver).press(start).wait(500).move_to( start2).wait(500).move_to(start3).perform().release() loggerInfo('再次绘制手势密码')
def after_step(self, step): if step.status == "failed": if gl.get_value('platformName') == 'Android': if DeviceStates(gl.get_value('deviceName')) is True: unLockDevice(gl.get_value('deviceName')) elif DeviceStates(gl.get_value('deviceName')) is False: # 按电源键 os.system( gl.get_value('adb') + ' -s ' + gl.get_value('deviceName') + ' shell input keyevent 26') unLockDevice(gl.get_value('deviceName')) else: loggerError('测试设备已息屏,adb命令执行出错') if getWebView(self.driver) is True: switchToNative(self.driver)
def inputpwd(self): # switchToWebView(self.driver) time.sleep(3) count = 0 while 10 > count: if getWebView(self.driver) is True: switchToWebView(self.driver) break else: count = count + 1 time.sleep(3) pwd = gl.get_value('tradepassword') click(self.driver, getElement('存管交易密码', '密码输入框')) time.sleep(2) sendKeys(self.driver, getElement('存管交易密码', '密码输入框'), pwd) click(self.driver, getElement('存管交易密码', '确认按钮')) switchToNative(self.driver)
def readyLogin(self): if gl.get_value('login') is False: click(self.driver, getElement('底部', '我')) if findElement(self.driver, getElement('我', '登录注册按钮')) is not None: click(self.driver, getElement('我', '登录注册按钮')) doLogin(self) Gesture(self) if findElement(self.driver, getElement('我', '用户名')) is not None: gl.set_value('login', True) else: loggerError('登录失败') elif gl.get_value('login') is True: loggerInfo('已登录') gl.set_value('login', True)
def isDoneSometing(self, page, element, done): ele = getElement(page, element) if findElement(self.driver, ele) is not None: if done == '绘制手势密码': Gesture(self) elif done == '点击它': click(self.driver, ele) elif done == '进行登录': doLogin(self) Gesture(self) if isExistElement(self.driver, getElement('底部', '首页')) is True: gl.set_value('login', True) elif findElement(self.driver, getElement('我', '用户名')) is not None: gl.set_value('login', True) else: if done == '进行登录': gl.set_value('login', False)
def signData(self, mess): tel = gl.get_value('username') # 获取userId userId = getUserId(tel) # 修改UserSign signDate = getYesterDay() sql1 = "update UserSign set Days='" + mess + "',SignDate='" + signDate + "' where UserId=" + "'" + userId + "'" executeSQL(sql1) # 获取签到明细ID signId = selectOneSQL("select id from UserSign where UserId='" + userId + "'") newSignId = signId[0] # 删除签到明细表 sql = "delete from UserSignDetail where UserSignId='" + newSignId + "'" executeSQL(sql) # 插入数据 procedureSQL(mess, newSignId) # 清楚缓存 # clearCache('usersigncallist') .net缓存key # JAVA清除缓存key clearCache('J_UserSignCal') clearCache('J_UserSignInfo') time.sleep(2)
def unLockDevice(ip): if gl.get_value('stf') == 'true': # linux result = getOutPut( gl.get_value('adb') + ' -s ' + ip + ' shell dumpsys window policy | grep mShowingLockscreen') else: # windows result = getOutPut( gl.get_value('adb') + ' -s ' + ip + ' shell dumpsys window policy | findstr mShowingLockscreen') if 'mShowingLockscreen=false' in result: loggerInfo('屏幕已解锁') # # 按home键 os.system( gl.get_value('adb') + ' -s ' + ip + ' shell input keyevent 3') elif 'mShowingLockscreen=true' in result: loggerInfo('屏幕未解锁,进行解锁') os.system( gl.get_value('adb') + ' -s ' + ip + ' shell input swipe 700 900 700 70') time.sleep(2) os.system( gl.get_value('adb') + ' -s ' + ip + ' shell input keyevent 3')
def before_feature(self, feature): if gl.get_value('platformName') == 'Android': if gl.get_value('platformVersion') == '4.4.4' or gl.get_value( 'platformVersion') == '4.4': self.driver = webdriver.Remote(command_executor='http://' + gl.get_value('url') + '/wd/hub', desired_capabilities={ 'chromeOptions': { 'androidProcess': 'com.ju' }, 'recreateChromeDriverSessions': 'true', 'app': gl.get_value('app'), 'udid': gl.get_value('android_udid'), 'platformName': gl.get_value('platformName'), 'platformVersion': gl.get_value('platformVersion'), 'deviceName': gl.get_value('android_udid'), 'noReset': 'true', 'autoGrantPermissions': 'true', 'appPackage': 'com.ju', 'resetKeyboard': 'true', 'unicodeKeyboard': 'true', 'useKeystore': 'true', }) else: self.driver = webdriver.Remote( command_executor='http://' + gl.get_value('url') + '/wd/hub', desired_capabilities={ 'chromeOptions': { 'androidProcess': 'com.ju' }, 'recreateChromeDriverSessions': 'true', 'app': gl.get_value('app'), 'udid': gl.get_value('android_udid'), 'platformName': gl.get_value('platformName'), 'platformVersion': gl.get_value('platformVersion'), 'deviceName': gl.get_value('android_udid'), 'noReset': 'true', 'autoGrantPermissions': 'true', 'appPackage': 'com.ju', 'resetKeyboard': 'true', 'unicodeKeyboard': 'true', 'useKeystore': 'true', 'chromedriverExecutableDir': os.path.dirname(os.path.abspath(__file__)) + '/webview/', # 本地调试使用 'chromedriverChromeMappingFile': os.path.dirname(os.path.abspath(__file__)) + '/mapping.json', # 本地调试使用 'keystorePath': os.path.dirname(os.path.abspath(__file__)) + '/apps/test.keystore', # 本地调试使用 # 'chromedriverExecutableDir': '/root/webview/', # 'chromedriverChromeMappingFile': '/root/mapping.json', # 'keystorePath': '/root/test.keystore', 'keystorePassword': '******', 'keyAlias': 'td', 'keyPassword': '******', 'automationName': 'UiAutomator2', 'noSign': 'true', 'newCommandTimeout': 12000 }) elif gl.get_value('platformName') == 'ios': self.driver = webdriver.Remote( command_executor='http://' + gl.get_value('url') + '/wd/hub', desired_capabilities={ 'app': gl.get_value('app'), 'platformName': gl.get_value('platformName'), 'platformVersion': gl.get_value('platformVersion'), # 'autoAcceptAlerts': 'true', # 'autoDismissAlerts': 'true', 'noReset': 'true', 'bundleId': 'com.tuandai.client', 'deviceName': gl.get_value('deviceName'), 'udid': gl.get_value('udid'), 'automationName': 'XCUITest', 'wdaLocalPort': int(gl.get_value('port')) }) if gl.get_value('noReset') == 'false': gl.set_value('login', False)
def getIosDevice(): # 检查ios设备占用情况 count = 0 max_count = 7200 while True: if count < max_count: result = checkDevice(gl.get_value('udid')) status = result['testing'] if status == 'false': updateDevice(gl.get_value('udid'), 'true') gl.set_value('url', result['server']) gl.set_value('port', result['port']) gl.set_value('deviceName', result['name']) break else: if gl.get_value('performance') == 'false': device = getUseDevice('ios') if device is not None: loggerInfo('已获取新可用测试设备') new_udid = device['device'] gl.set_value('udid', new_udid) updateDevice(gl.get_value('udid'), 'true') gl.set_value('url', device['server']) gl.set_value('port', result['port']) gl.set_value('deviceName', result['name']) break loggerInfo('正在获取可用测试设备') loggerInfo('30S后重试') time.sleep(30) count = count + 30 else: assert count < max_count, '获取可用测试设备超过2小时,脚本执行失败'
def getStfDevice(): # 循环读取设备 # 添加最大值限制,2小时7200秒,超时默认失败 count = 0 max_count = 7200 while True: if count < max_count: list1 = getDevices() if list1 is not None: if gl.get_value('serial') == 'false': list_devices = [ 'AQ9SMB85RKKFPRFE', '7854d2c4', 'A5R7N17C22027488', 'BTYNW17324002120' ] list = [val for val in list1 if val in list_devices] # list = list1 else: if gl.get_value('serial') in list1: list = [gl.get_value('serial')] else: list = None if not list: # 如果可用设备集合为空 loggerInfo('正在获取可用测试设备,30S后重试') time.sleep(30) else: num = 4723 while num < 4823: list_use = dockerCmd('lsof -i:' + str(num)) if len(list_use): num = num + 1 else: gl.set_value('url', '10.100.97.250:' + str(num)) gl.set_value('appium_port', str(num)) ar.saveRecord(str(num)) break loggerInfo('已获取可用测试设备Device:' + list[0]) gl.set_value('android_udid', list[0]) gl.set_value('list', list[0]) # 获取IP deviceName = connectDevice(list[0]) if deviceName is not None: gl.set_value('deviceName', deviceName) s = getDeviceInfo(list[0]) gl.set_value('platformVersion', s["device"]["version"]) break loggerInfo(list[0] + '测试设备繁忙,30S后重试') time.sleep(30) else: loggerInfo('正在获取可用测试设备,30S后重试') time.sleep(30) count = count + 30 else: assert count < max_count, '获取可用测试设备超过2小时,脚本执行失败'
def installApp(ip, dir): loggerInfo(gl.get_value('adb') + ' -s ' + ip + ' install ' + dir) os.system(gl.get_value('adb') + ' -s ' + ip + ' install ' + dir)
def uninstallApp(ip): loggerInfo(gl.get_value('adb') + ' -s ' + ip + ' uninstall com.junte') os.system(gl.get_value('adb') + ' -s ' + ip + ' uninstall com.junte')
def disConnectApp(ip): time.sleep(2) os.system(gl.get_value('adb') + ' disconnect ' + ip)
def rechange(self, value): userdata = self.config.userdata if value == 'app': if userdata.get(value) is None: if gl.get_value('platformName') == 'Android': app = 'http://127.0.0.1/app-debug.apk' gl.set_value(value, app) else: app = '/Users/ijknode/workspace/uitest_package/TuanDaiV4/derivedData/Build/Products/Debug-iphonesimulator/TuanDaiV4.app' gl.set_value(value, app) else: gl.set_value(value, userdata.get(value)) else: if userdata.get(value) is None: gl.set_value(value, readConfig(value)) else: gl.set_value(value, userdata.get(value))