def select(self, driver, para_list): """ Select a option for sepecified select type web element. :param driver: :param para_list: [method, value, option_method, option_value] eg : ['id','country_list','text','China'] index: select the option by it's index. value: select the option by it's value. text: select the option by it's text(full text). text_part: select the option by it's text(part of the option). e.g: option text is 'autotest15865524', you can found the option by 'autotest' :return: """ method, value, option_method, option_value = para_list[0], para_list[ 1], para_list[2], para_list[3] from selenium.webdriver.support.select import Select if option_method == 'index': comd = 'Select(driver.find_element_by_%s("%s")).select_by_%s(%s)' % ( method, value, option_method, option_value) else: if option_method == 'text_part': self.select_by_visible_text( driver.find_element(by=method, value=value), option_value) else: if option_method == 'text': option_method = 'visible_text' comd = 'Select(driver.find_element_by_%s("%s")).select_by_%s("%s")' % ( method, value, option_method, option_value) log.log().logger.debug(comd) exec(comd) time.sleep(2)
def run(self): list0 = util.util().getTeseCasesATX(self.j, isRunning=True) if len(list0): log.log().logger.info('case still running !') else: list = util.util().getTeseCasesATX(self.j) if len(list): log.log().logger.info('start test by single devices :%s ' % list[0][0]) self.q.put(u"打点我是第%d个线程,ip: %s, 测试用例:%s" % (self.t, self.j, list[0][0])) log.log().logger.info(u"打点我是第%d个线程,ip: %s, 测试用例:%s" % (self.t, self.j, list[0][0])) process().main(list[0]) time.sleep(2) else: list2 = util.util().getTeseCasesATX(all=True) log.log().logger.info('current list lenth is: %s' % len(list2)) if len(list2): try: case = list2[self.t] except: case = list2[0] log.log().logger.info('current ip is : %s' % str(self.j))
def readPublic(self, caseList0): # print(caseList0) resultList = [] isRead = True while (isRead): case2 = caseList0[0].split('|') if case2[0] == "公共方法": caseList0.remove(caseList0[0]) # log.log().logger.info(case2) case0 = self.read_public_case(case2[1]) if len(case0): resultList0 = [] case0 = case0[0] for casei in caseList0: case0.append(casei) try: case0.remove('') except ValueError as e: # log.log().logger.info('error: %s' %e) pass caseList0 = case0 else: log.log().logger.error('public function is not found!') resultList = [] isRead = False else: resultList = caseList0 isRead = False # print(resultList) return resultList
def screenshot(self, screenShotType, caseNo): nowTime = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime()) if platform.system() == 'Windows': screen_shot_path = config.screen_shot_path screen_shot_path1 = 'static\\screenshot\\' log.log().logger.info(screen_shot_path) normalfilename = screen_shot_path + 'normalScreenShot\\' + str( caseNo) + '_success' + nowTime + '.jpg' errorfilename = screen_shot_path + 'errorScreenShot\\' + str( caseNo) + '_error' + nowTime + '.jpg' normalfilename1 = '\\' + screen_shot_path1 + '\\' + 'normalScreenShot' + '\\' + str( caseNo) + '_success' + nowTime + '.jpg' errorfilename1 = '\\' + screen_shot_path1 + '\\' + 'errorScreenShot' + '\\' + str( caseNo) + '_error' + nowTime + '.jpg' else: screen_shot_path = config.screen_shot_path screen_shot_path1 = 'static/screenshot' normalfilename = screen_shot_path + 'normalScreenShot' + '/' + str( caseNo) + '_success' + nowTime + '.jpg' errorfilename = screen_shot_path + 'errorScreenShot' + '/' + str( caseNo) + '_error' + nowTime + '.jpg' normalfilename1 = '/' + screen_shot_path1 + '/' + 'normalScreenShot' + '/' + str( caseNo) + '_success' + nowTime + '.jpg' errorfilename1 = '/' + screen_shot_path1 + '/' + 'errorScreenShot' + '/' + str( caseNo) + '_error' + nowTime + '.jpg' if screenShotType == 'error': return errorfilename, errorfilename1 else: return normalfilename, normalfilename1
def assert_element_text(self, driver, para_list, isNot=False, isUpper=False): """ Assert whether the specified web element contains the given text. :param driver: :param para_list: [method, value] eg : ['id','add_btn'] :param isNot: If you want to assert that target element doesn't contain the given text, isNot should be set to True. :param isUpper: if isUpper is false, will convert the target text to upper mode. :return: """ text0 = '' if len(para_list) == 3: method, value, text = para_list[0], para_list[1], para_list[2] element = self.find_element(driver, [method, value]) text0 = element.text if isUpper: text = text.upper() text0 = text0.upper() if not len(text0): text0 = element.get_attribute('value') if isNot: log.log().logger.info('目标文本:%s, 期待不包含文本:%s' % (text0, text)) assert (text in str(text0)) == False else: log.log().logger.info('目标文本:%s, 期待包含文本:%s' % (text0, text)) assert (text in str(text0))
def run_step(self, u, step_name, detail, caseNo, screenFileList): # print(u, step_name, detail,caseNo,screenFileList) result = '1' if step_name == 'Android': time.sleep(1) elif step_name == '点击': u, result = self.click(u, detail[0], detail[1]) elif step_name == '尝试点击': self.click(u, detail[0], detail[1]) elif step_name == '等待': time.sleep(int(detail[0])) elif step_name == '发送': u, result = self.sendData(u, detail[0]) elif step_name == '填写': u, result = self.type_text(u, detail[0], detail[1], detail[2]) elif step_name == '返回': u.adb_shell('input', 'keyevent', 'BACK') time.sleep(1) elif step_name == '截图': screenFileList = self.take_screenshot(u, 'normal', caseNo, screenFileList) else: result = '2' log.log().logger.error('method is not defined : %s' % step_name) if result == '0': log.log().logger.error('package is not fould!') elif result == '2': screenFileList = self.take_screenshot(u, 'fail', caseNo, screenFileList) return u, result, screenFileList
def assert_title(self, driver, text): if text in driver.title: result = '1' else: result = '2' log.log().logger.info('verify result is : %s' % result) return result
class MyThread(threading.Thread): def __init__(self, q, t, j): super(MyThread, self).__init__() self.q = q self.t = t self.j = j def run(self): list0 = util.util().getTeseCasesATX(self.j,isRunning=True) if len(list0): log.log().logger.info('case still running !') else: list = util.util().getTeseCasesATX(self.j) if len(list): log.log().logger.info('start test by single devices :%s ' %list[0][0] ) self.q.put(u"打点我是第%d个线程,ip: %s, 测试用例:%s" % (self.t, self.j,list[0][0])) log.log().logger.info(u"打点我是第%d个线程,ip: %s, 测试用例:%s" % (self.t, self.j,list[0][0])) process().main(list[0]) time.sleep(2) else: list2 = util.util().getTeseCasesATX(all=True) log.log().logger.info('current list lenth is: %s'%len(list2)) if len(list2): try: case = list2[self.t] except: case=list2[0] log.log().logger.info('current ip is : %s'%str(self.j)) log.log().logger.info('start test by multiple devices :%s ' % case[0]) self.q.put(u"我是第%d个线程,ip: %s, 测试用例:%s" % (self.t, self.j, case[0])) log.log().logger.info(u"打点我是第%d个线程,ip: %s, 测试用例:%s" % (self.t, self.j, case[0])) process().main(case,deviceList=[str(self.j)]) time.sleep(3)
def runmain(self, test_suite_id, threadNum, runType): Hubs = hubs.hubs().showHubs(runType) if len(Hubs) == 0: log.log().logger.error('cannot run for no available hubs!') else: self.multipleRun(util.util().getTeseCases(test_suite_id), threadNum) test_task_manage.test_task_manage().update_test_suite_check()
def serverUrl(self,servers): server_url = {} import random i = random.randint(0,100)%len(servers) log.log().logger.info('%s, %s' %(i,len(servers))) server_url['hostname']=servers[i][0] server_url['port'] = servers[i][1] return "http://%s:%s/wd/hub" %(server_url['hostname'],server_url['port'])
def click_description(self, u, id): if u(description=id).exists: u(description=id).click() time.sleep(1) result = 1 else: log.log().logger.error(u"出错了,没有找到元素! by %s , %s" % ('description', id)) result = 2 return u, result
def click_id(self, u, id): if u(resourceId=id).exists: u(resourceId=id).click() time.sleep(1) result = 1 else: log.log().logger.error(u"出错了,没有找到元素! by %s , %s" % ('id', id)) result = 2 return u, result
def click_text(self, u, text): if u(text=text).exists: u(text=text).click() time.sleep(1) result = 1 else: log.log().logger.error(u"出错了,没有找到元素! by %s , %s" % ('text', text)) result = 2 return u, result
def click_class(self, u, id): if u(className=id).exists: u(className=id).click() time.sleep(1) result = 1 else: log.log().logger.error(u"出错了,没有找到元素! by %s , %s" % ('className', id)) result = 2 return u, result
def click_text(self, driver, text): elements = driver.find_elements(by='xpath', value="//*[contains(.,'" + text + "')]") for element in elements: try: element.click() except NoSuchElementException as e: log.log().logger.info(e) time.sleep(2)
def click_id(self, u, id): if u(resourceId=id).exists: u(resourceId=id).click() time.sleep(1) result = '1' else: log.log().logger.error(u"出错了,没有找到元素! by %s , %s" % ('id', id)) u.swipe_points([(0.509, 0.601), (0.503, 0.149)], 0.2) result = '2' return u, result
def assert_title(self, driver, text): """ Assert whether current web page's title contains the given text. :param driver: :param text: :return: """ log.log().logger.info('目标文本:%s, 期待包含文本:%s' % (driver.title, text)) assert text in driver.title
def init(self, device=''): log.log().logger.info('trying to connect device : %s' % device) try: u = ut2.connect(device) log.log().logger.info(u.device_info) isConnected = 1 except: isConnected = 0 u = '' return isConnected, u
def click_text(self, u, text): if u(text=text).exists: u(text=text).click() time.sleep(1) result = '1' else: log.log().logger.error(u"出错了,没有找到元素! by %s , %s" % ('text', text)) u.swipe_points([(0.509, 0.601), (0.503, 0.149)], 0.2) result = '2' return u, result
def is_app_exist0(self, package): import subprocess cmd = 'dumpsys package %s | grep version' % package cmds = [cmd, "exit", ] pipe = subprocess.Popen("adb shell", stdin=subprocess.PIPE, stdout=subprocess.PIPE) code = pipe.communicate(("\n".join(cmds) + "\n").encode()) log.log().logger.info(code) return 'versionCode' in str(code)
def click_index(self, driver, para_list): method, value, index = para_list[0], para_list[1], para_list[2] elements = self.find_elements(driver, [method, value]) if len(elements): for i in range(3): try: elements[int(index)].click() except NoSuchElementException as e: log.log().logger.info(e) time.sleep(2)
def getDevices(self): url = config.ATXHost + '/list' response, content = util.util().send(url) content = json.loads(content) deviceList = [] for device in content: if device['present']: deviceList.append(device['ip'] + ':7912') else: log.log().logger.debug(device['ip'] + ' is not ready!') return deviceList
def screenshot(self, driver, id, screenFileList, isError=False): result = '2' if isError: fileName, fileName1 = util.util().screenshot('error', id) else: fileName, fileName1 = util.util().screenshot('normal', id) log.log().logger.debug(fileName) driver.save_screenshot(fileName) screenFileList.append(fileName1) result = '1' return result, screenFileList
def getPara(self, keyword): result = 0 log.log().logger.info(keyword) sql = string.Template( "select paraCount, template, elementTemplate from `test_keyword` where `keyword`= '$index' limit 1;" ) sql = sql.substitute(index=keyword) result = useDB.useDB().search(sql) if len(result): return result[0][0], result[0][1], result[0][2] else: return '', '', ''
def getPara1(self, keyword): # print(self.keywords) keywords = self.keywords result = 0 log.log().logger.info(keyword) for list in keywords: if list['index'] == keyword: result = 1 return list['paraCount'], list['template'] break if result == 0: return '', ''
def IsOpen(self, ip, port): import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect((ip, int(port))) s.shutdown(2) # 利用shutdown()函数使socket双向数据传输变为单向数据传输。shutdown()需要一个单独的参数, # 该参数表示了如何关闭socket。具体为:0表示禁止将来读;1表示禁止将来写;2表示禁止将来读和写。 # log.log().logger.info('%s is open' % port) return True except: log.log().logger.info('%s is down' % port) return False
def runmain(self,test_suite_id,threadNum, runType ): if runType == 'Android' and isUseATX: Hubs = hubs.hubs().getDevices() log.log().logger.debug('Run type is ATX and usable devices are %s' %Hubs) else: Hubs = hubs.hubs().showHubs(runType) if len(Hubs) ==0: log.log().logger.debug('cannot run for no available hubs!') elif runType == 'Android' and isUseATX: self.atxMain() else: self.multipleRun(util.util().getTeseCases(test_suite_id),threadNum) test_task_manage.test_task_manage().update_test_suite_check()
def try_click(self, driver, para_list): # para_list=str(para_list).split(',') if len(para_list) == 2: method, value = para_list[0], para_list[1] # print(value) element = self.find_element(driver, [method, value]) if element: for i in range(3): try: element.click() except NoSuchElementException as e: log.log().logger.info(e) time.sleep(2)
def getDevicesList(self): url = config.ATXHost + '/list' response, content = util.util().send(url) content = json.loads(content) deviceLists = [] for device in content: deviceList = {} if device['present']: deviceList["ip"] = device['ip'] + ':7912' deviceList["model"] = device['model'] deviceLists.append(deviceList) else: log.log().logger.info(device['ip'] + ' is not ready!') return deviceLists
def type_text(self, u, method, resource_id, text): if method == 'id': if u(resourceId=resource_id).exists: u(resourceId=resource_id).send_keys(text) time.sleep(1) result = 1 u.adb_shell('input', 'keyevent', 'BACK') time.sleep(1) else: log.log().logger.error(u"出错了,没有找到元素! by %s , %s" % (method, resource_id)) result = 2 elif method == 'name': if u(text=resource_id).exists: u(text=resource_id).send_keys(text) time.sleep(1) result = 1 u.adb_shell('input', 'keyevent', 'BACK') time.sleep(1) else: log.log().logger.error(u"出错了,没有找到元素! by %s , %s" % (method, resource_id)) result = 2 elif method == 'class': if u(className=resource_id).exists: u(className=resource_id).send_keys(text) time.sleep(1) result = 1 u.adb_shell('input', 'keyevent', 'BACK') time.sleep(1) else: log.log().logger.error(u"出错了,没有找到元素! by %s , %s" % (method, resource_id)) result = 2 else: log.log().logger.error(u"元素方法未定义! %s" % method) result = 2 return u, result