def tt1(self): log = ResultLog.ResultLog() try: br = webdriver.Firefox() br.find_element_by_id("wwg").click() raise NameError() except: print "except捕获异常"
def init_browser(self): """ 该函数主要是初始化浏览器对象并返回一个webdriver对象 """ log = ResultLog.ResultLog() try: br = webdriver.Firefox() except: log.info("浏览器初始化失败了") br.get("http://www.xebest.com:8000") return br
def init_browserByRegister(self): """ 该函数主要是初始化浏览器对象并返回一个webdriver对象 """ log = ResultLog.ResultLog() try: br = webdriver.Firefox() except: log.info("浏览器初始化失败了") br.get("https://user.xebest.com:8443/reg.action") return br
def testGetAndOperateData(self): u"""登录功能测试""" """ 此方法作用: 1、获得excel中存储的用户名和密码,每一行对应的用户名和密码作为一个小列表,所有的小列表组成一个大列表 2、将用户名和密码作为外层for循环,将需要定位的元素的名称的列表作为内层循环,拿到一个需要定位的元素的名称就去定位并进行sendkey或者click操作 """ class_list = self.instance_List() log = ResultLog.ResultLog() #拿到登录功能中需要定位的对象名称列表 object_name_list = self.getObjList() #拿到存储在excel中存储的用户名和密码列表对 username_password_list = self.getUsernameAndPasswordList() index = 1 for username, password in username_password_list: for m in range(object_name_list.__len__()): class_list[0].locateObject(class_list[2], username, password, object_name_list[m]) class_list[1].operate_alert(class_list[2]) log.info("第%d次功能参数化操作完成" % index) index += 1
def operatePayElement(self,element,located_element,send_data_list): log=ResultLog.ResultLog() excel=ReadExcel.ReadExcel() operate_excel_path="F:\\pytest\\xebest-autotest\\Data\\pay_by_balance.xls" operate_sheet=excel.getTableBySheetName(operate_excel_path,"operate_method") #得到操作方式所在列 operate_method=u"操作方式" operate_method_col_index=get_number_by_data.GetRowAndColNumber().getRowAndColNumber(operate_excel_path,"operate_method",operate_method)[1] #得到元素所在行 row_index=get_number_by_data.GetRowAndColNumber().getRowAndColNumber(operate_excel_path,"operate_method",element)[0] operate_method_flag=operate_sheet.cell_value(row_index,operate_method_col_index) if operate_method_flag=='click': try: located_element.click() time.sleep(3) except: print ("element:%s cannot operate" %element) raise ValueError("no element can operate") else: log.info("元素:%s 操作成功" %(element.encode('utf-8'))) elif operate_method_flag=='sendkey' and element==u'CheckCodeInput': checkcode=SmsCheckCode.SmsCheckCode().getCheckCode() time.sleep(2) if checkcode: log.info("验证码获取成功,获取到的验证码为:%s" %(checkcode.encode('utf-8'))) else: log.info("验证码获取失败") located_element.send_keys(checkcode) elif operate_method_flag=='sendkey' and element==u'PayPasswordInput': located_element.clear() located_element.send_keys(send_data_list[0])
def locateObject(self, browser, object_username, object_password, object_name): br = browser username = object_username password = object_password locate_element = object_name login_class2 = Login.Login() #实例化Login类 log = ResultLog.ResultLog() readexcel_class2 = ReadExcel.ReadExcel() objectname_locatemethod_locatedata_excelpath = "F:\\pytest\\testcase\\Data\\login_data.xls" objectname_locatemethod_locatedata_sheet = readexcel_class2.getTable( objectname_locatemethod_locatedata_excelpath) #得到excel操作实体 objectname_locatemethod_locatedata_sheet_rows = readexcel_class2.getExcelRows( objectname_locatemethod_locatedata_excelpath) #得到excel总行数 #定义一个字典去映射excel表中的元素定位方式 dic1 = { 'id': By.ID, 'css': By.CSS_SELECTOR, 'xpath': By.XPATH, 'linktext': By.LINK_TEXT } #循环excel中的每一行,判断定位方式是id、css、xpath,每一行是一个界面元素 for i in range(objectname_locatemethod_locatedata_sheet_rows): list3 = objectname_locatemethod_locatedata_sheet.row_values(i) if list3[0] == locate_element: #在该行中,如果定位方式是id,就使用id定位方式 if list3[1] == 'id': try: element = br.find_element(by=dic1['id'], value=list3[2]) log.info("元素%s 定位成功" % locate_element.encode('utf-8')) except: log.info("需要操作的元素%s 不存在" % locate_element.encode('utf-8')) login_class2.operate_element(br, username, password, locate_element, element) #在该行中,如果定位方式是css,就使用css定位方式 elif list3[1] == 'css': try: element = br.find_element(by=dic1['css'], value=list3[2]) log.info("元素%s 定位成功" % locate_element.encode('utf-8')) except: log.info("需要操作的元素%s 不存在" % locate_element.encode('utf-8')) login_class2.operate_element(br, username, password, locate_element, element) #在该行中,如果定位方式是xpath,就使用xpath定位方式 elif list3[1] == 'xpath': try: element = br.find_element(by=dic1['xpath'], value=list3[2]) log.info("元素%s 定位成功" % locate_element.encode('utf-8')) except: log.info("需要操作的元素%s 不存在" % locate_element.encode('utf-8')) login_class2.operate_element(br, username, password, locate_element, element) elif list3[1] == 'linktext': try: element = br.find_element(by=dic1['linktext'], value=list3[2]) log.info("元素%s 定位成功" % locate_element.encode('utf-8')) except: log.info("需要操作的元素%s 不存在" % locate_element.encode('utf-8')) login_class2.operate_element(br, username, password, locate_element, element)