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 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 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 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))
def before_all(self): # 设置首次默认未登录 gl.set_value('login', False) getSettingValue(self) if gl.get_value('platformName') == 'Android': if gl.get_value('stf') == 'false': rechange(self, 'deviceName') rechange(self, 'platformVersion') gl.set_value('android_udid', gl.get_value('deviceName')) elif gl.get_value('stf') == 'true': # 获取stf设备 getStfDevice() # 重启Appium服务器的Docker dockerCmd('docker rm -f appium-' + gl.get_value('appium_port')) dockerRestart(gl.get_value('appium_port')) connectApp(gl.get_value('deviceName')) # 获取测试机屏幕是否已唤醒,执行唤醒操作 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: assert True == False, 'adb命令执行出错' if gl.get_value('noReset') == 'false': # 卸载APK # connectApp(gl.get_value('deviceName')) uninstallApp(gl.get_value('deviceName')) if gl.get_value('performance') == 'true': # 创建性能监控文件 connectApp(gl.get_value('deviceName')) createPerformanceFile(gl.get_value('deviceName')) elif gl.get_value('platformName') == 'ios': rechange(self, 'deviceName') rechange(self, 'platformVersion') getIosDevice() # ios卸载重装 if gl.get_value('noReset') == 'false': iosUninstallApp(gl.get_value('udid')) pass
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 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 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 loginStatus(self, status): if status == '已登录': gl.set_value('login', True) elif status == '未登录': gl.set_value('login', False)
def matchElementText(self, page, element1, element2): """主要匹配产品进行申购""" gl.set_value('matchProduct', False) ele = getElement(page, element1) loggerInfo(str(ele)) text = getTexts(self.driver, ele) if gl.get_value('platformName') == 'Android': loggerInfo('获取到的产品-->' + str(text)) for i in range(len(text)): if element2 in text[i]: loggerInfo('点击产品-->' + text[i]) try: xpath = '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.support.v4.view.ViewPager/android.widget.FrameLayout/android.widget.RelativeLayout/android.support.v4.view.ViewPager/android.widget.FrameLayout/android.widget.LinearLayout/android.view.ViewGroup/android.support.v7.widget.RecyclerView/android.view.ViewGroup[{}]'.format( i + 4) # loggerInfo('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.support.v4.view.ViewPager/android.widget.FrameLayout/android.view.ViewGroup/android.support.v4.view.ViewPager/android.widget.FrameLayout/android.widget.LinearLayout/android.view.ViewGroup/android.support.v7.widget.RecyclerView/android.view.ViewGroup[{}]'.format(i+4)) self.driver.find_element_by_xpath(xpath).click() gl.set_value('matchProduct', True) return except Exception as e: loggerError(str(e)) if gl.get_value('matchProduct') is False: swipeUp(self.driver) loc = ['id', 'com.junte:id/snackbar_text', '最后一页'] if findElement(self.driver, loc) is None: # 判断是否已经滑动到最后一页 matchElementText(self, page, element1, element2) else: loggerError('未找到产品-->' + element2) assert True == False return elif gl.get_value('platformName') == 'ios': if text is None: swipeUp(self.driver) text = getTexts(self.driver, ele) del text[0:5] loggerInfo('获取到的产品-->' + str(text)) for i in range(len(text)): if element2 in text[i]: loggerInfo('点击产品-->' + text[i]) try: xpath = "//XCUIElementTypeApplication[1]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeScrollView[1]/XCUIElementTypeOther[1]/XCUIElementTypeTable[1]/XCUIElementTypeCell[{}]".format( i + 1) self.driver.find_element_by_xpath(xpath).click() gl.set_value('matchProduct', True) return except Exception as e: loggerError(str(e)) if gl.get_value('matchProduct') is False: productList = [] for i in range(len(text)): if element2 in text[i]: productList.append(text[i]) if len(productList) > 0: swipeUp(self.driver) matchElementText(self, page, element1, element2) else: loggerError('未找到产品-->' + element2) assert True == False return