def setUpClass(cls): # global test global logg logg = LogHandler().getlog() cls.test = aiologinpage.AioLogin(webdriver.Chrome()) getandcheck = cls.test.get_conf_url() if not getandcheck: TestLoginCheck.tearDownClass() logg.error("进入主页失败,err:14001") return SystemExit(14001)
class TestLogin(unittest.TestCase): global logg logg = LogHandler().getlog() @classmethod def setUpClass(cls): cls.test = aiologinpage.AioLogin(webdriver.Chrome()) cls.test.get_conf_url() # print('start TestSearch') @classmethod def tearDownClass(cls): # TestLogin().logg.info("brower quit") TestLogin().test.brower_close() pass logindata = ReadExcel().getValue('login') @data(*logindata) @unpack def testcase2(self, username, passwd, result): logg.info(username + " " + passwd + " " + str(result)) self.test.set_username(username) self.test.set_password(passwd) self.test.type_login_btn() # 断言登录结果和预期结果是否一致 # self.assertEqual(self.test.checkLoginResult(result),True,msg="login_test fail") self.assertTrue( self.test.check_login_result(result), msg="\r login_test fail \r username :%s \r passwd : %s " % (username, passwd))
class SettingPage(HomePage): global logg logg = LogHandler().getlog() # 系统设置,设置菜单:基础设置、存储管理设置、虚拟云设置 e_setting = GlobalElements.setting def init_setting(self): # 登录+点击设置 self.init_home_page() self.click_setting_menu() logg.info("init and click setting menu") pass def click_base_setting(self): # 点击设置 self.click_btn(*self.e_setting.base_setting_menu) logg.info("click setting menu ") pass def click_store_setting(self): # 存储管理设置 self.click_btn(*self.e_setting.store_setting_menu) logg.info("click store setting menu ") pass def click_layout_setting(self): self.click_btn(*self.e_setting.layout_setting_menu) logg.info("clicj layout setting menu") pass def set_cltport(self, portnum): logg.info("set client port:" + portnum) self.click_btn(*self.e_setting.cltport_setting_menu) self.clear_text(*self.e_setting.cltport_num) self.type_text(self.e_setting.cltport_num, portnum) self.click_btn(*self.e_setting.cltport_save_btn) sleep(1) if self.check_dialog_success("设置端口"): return True else: return False def click_cloud_setting(self): self.click_btn(*self.e_setting.cloud_setting_menu) logg.info("click cloud setting menu ") pass
class HomePage(AioLogin): global logg logg = LogHandler().getlog() e_main = GlobalElements.mainpage e_common = GlobalElements.common # devgrp_name_click = By.CSS_SELECTOR,"td.has-split[title="+groupname+"]" def init_home_page(self): self.correct_login() logg.info("init home page") # self.click_btn(*self.check_default) def click_service_overview(self): self.click_btn(*self.e_main.service_overview_menu) sleep(1) def click_store_menu(self): ''' 点击 云存储 ''' self.click_btn(*self.e_main.store_menu) sleep(1) def click_setting_menu(self): """ 点击 设置 :return: """ self.click_btn(*self.e_main.setting_menu) sleep(1) def click_service_manager_menu(self): self.click_service_overview() self.click_store_menu() return self.click_btn(*self.e_main.service_manager_menu) def click_client_manager_menu(self): self.click_service_overview() self.click_store_menu() return self.click_btn(*self.e_main.client_manager_menu) def click_devgrp_manager_menu(self): self.click_service_overview() self.click_store_menu() return self.click_btn(*self.e_main.devgrp_manager_menu) def click_block_manager_menu(self): """ 点击 块设备管理界面 :return: """ logg.info("块设备管理 页面") self.click_service_overview() self.click_store_menu() return self.click_btn(*self.e_main.block_manager_menu) def click_file_manager_menu(self): self.click_service_overview() self.click_store_menu() return self.click_btn(*self.e_main.file_manager_menu) def click_perm_manager_menu(self): logg.info("权限管理 页面") self.click_service_overview() self.click_store_menu() return self.click_btn(*self.e_main.perm_manager_menu) def click_senior_manager_menu(self): self.click_service_overview() self.click_store_menu() return self.click_btn(*self.e_main.senior_manager_menu) def click_log_menu(self): logg.info("日志管理 页面") self.click_service_overview() self.click_store_menu() return self.click_btn(*self.e_main.log_menu)
from selenium import webdriver from common.logpy import LogHandler from time import sleep from selenium.webdriver.support.select import Select from bs4 import BeautifulSoup import sys import re WAIT_TIME = 0.5 mylog = LogHandler(logger="yqfz").getlog() #输入项目名 def project_name_input(pro_name): wd.find_element_by_id("ctl00_MainContent_txt_Pro").send_keys(pro_name) mylog.error("输入项目:" + pro_name) sleep(WAIT_TIME) #房产公司名称 def business_name_input(business_name): wd.find_element_by_id("ctl00_MainContent_txt_Com").send_keys(business_name) sleep(WAIT_TIME) #选择 区域 def domain_name_input(domain_name): if domain_name == 1: Select(wd.find_element_by_id( "ctl00_MainContent_ddl_qy")).select_by_value("RD002") sleep(WAIT_TIME)
class AioLogin(BasePage): global logg logg = LogHandler().getlog() e_login = GlobalElements.login def get_and_check_server_url(self, OEMtitle=None): self.get_conf_url() sleep(3) if not OEMtitle: if "" != self.get_title(): return False elif OEMtitle != self.get_title(): return False return True def set_username(self, username): # self.find_element(*self.username_loc).clear() # self.find_element(*self.username_loc).send_keys(username) self.clear_text(*self.e_login.username_loc) self.type_text(self.e_login.username_loc, username) logg.info('Enter username: '******'Enter password: '******'check_login_ok') return ok_class_result def _check_login_fail(self): fail_class_result = False fail_class_result = self.check_element_isexist(self.e_login.login_loc) logg.debug("__check not login" + str(fail_class_result)) sleep(0.1) return fail_class_result def check_login_result(self, testcase_login_expected_result): flag = None if testcase_login_expected_result: if self._check_login_ok(): flag = True self.insert_success_img("登录成功") else: flag = False self.insert_error_img("登录失败") else: if self._check_login_fail(): flag = True self.insert_success_img('登录用例 检查成功') else: flag = False self.insert_error_img('登录用例 检查失败') return flag def __correct_userpasswd_conf(self): self.tusername = readconfig.ReadConfig().get_configinfo( 'user', 'tuser') self.tpassword = readconfig.ReadConfig().get_configinfo( 'user', 'tpassword') self.urlvalue = (self.tusername, self.tpassword) return self.urlvalue def correct_login(self, OEM=None): self.get_conf_url() self.userpasswd = self.__correct_userpasswd_conf() self.set_username(self.userpasswd[0]) self.set_password(self.userpasswd[1]) login_result = self.type_login_btn(OEM=OEM) return login_result
class BasePage(): ''' 页面的BasePage driver : self.wd ''' global logg logg = LogHandler().logger e_common = GlobalElements.common # def __init__(self,driver=webdriver.Chrome(),url=None): def __init__(self, driver=None, url=None): ''' 初始化 webdriver 并启动 :param driver: webdriver.Chrome() ''' if driver: self.wd = driver else: self.wd = webdriver.Chrome() # self.wd.implicitly_wait(5) self.wd.implicitly_wait(3) self.actions = ActionChains(self.wd) if url: self.url = url else: self.url = self.server_url_conf() # 浏览器行为 def get_conf_url(self): ''' 获取配置文件中的测试url :return: ''' self.wd.get(self.url) self.wd.maximize_window() logg.debug("enter conf_url : " + str(self.url)) try: self.wd.find_element_by_id("details-button").click() self.wd.find_element_by_id("proceed-link").click() except: pass finally: time.sleep(2) return True def brower_close(self): '''关闭一个窗口''' return self.wd.close() def brower_quit_all(self): '''退出浏览器''' return self.wd.quit() def brower_setwindowssize(self, width, height, windowHandle='current'): self.wd.set_window_size(width, height, windowHandle) def brower_refresh(self): self.wd.refresh() def brower_forward(self): self.wd.forward() def brower_backward(self): self.wd.back() # excel,字符串型 转换成 webdriver by def _transf_bykey(self, loc): if loc[0] == "By.NAME": return (By.NAME, loc[1]) if loc[0] == "By.CLASS_NAME": return (By.CLASS_NAME, loc[1]) if loc[0] == "By.CSS_SELECTOR": return (By.CSS_SELECTOR, loc[1]) if loc[0] == "By.XPATH": return (By.XPATH, loc[1]) logg.info('transf elements : ' + str(loc)) return loc # 定位 def find_web_element(self, *loc): # self.wd.find_element(*loc) # loc = loc[0] # print('-------',loc) loc = self._transf_bykey(loc) # print('*****',loc) return self.wd.find_element(*loc) # 元素操作 def type_text(self, loc, text): return self.wd.find_element(*loc).send_keys(text) def clear_text(self, *loc): return self.wd.find_element(*loc).clear() def submit_func(self, *loc): ''' 提交 loc : (By.ID,"id") ''' return self.wd.find_element(*loc).submit() def click_btn(self, *loc): '''点击按钮''' try: self.wd.find_element(*loc).click() return True except Exception as e: logg.error("click_btn error: %s" % (e)) return False # def keyboard_send_f5(self): # loc = (By.CSS_SELECTOR,"[name='log']") # self.find_web_element(*loc).send_keys(Keys.F5) # 鼠标操作 def mouse_right_click(self, *loc): elem = self.find_web_element(*loc) self.actions.click(elem).perform() def mouse_left_click(self, *loc): elem = self.find_web_element(*loc) self.actions.context_click(elem).perform() def mouse_double_click(self, *loc): elem = self.find_web_element(*loc) self.actions.double_click(elem) def mouse_move_to_element(self, *loc): loc = self._transf_bykey(loc) elem = self.find_web_element(*loc) self.actions.move_to_element(elem).perform() def mouse_drag_and_drop(self): pass # 获取信息行为 def get_web_url(self): return self.wd.current_url def get_title(self): return self.wd.title def get_element_text(self, *loc): return self.find_web_element(*loc).text # 元素是否存在 是 True def check_element_isexist(self, loc): ''' 检查元素是否存在 :param loc: tuple (By.ID,"id") :return: True or False ''' isexist = False try: sleep(2) EC.presence_of_element_located(self._transf_bykey(loc))(self.wd) # EC.presence_of_element_located(loc)(self.wd) isexist = True logg.debug('isexist : True ') except Exception as e: isexist = False # logg.debug(' isexist or not :',exc_info = True) logg.debug(' isexist or not :', exc_info=True) return isexist def check_element_has_text(self, loc, text): ''' 检查元素文本信息是否存在 :param loc: tuple(By.ID,"id") :param text: text value :return: True or False ''' try: text = EC.text_to_be_present_in_element(loc, text)(self.wd) finally: return text def check_element_isdisplayed(self, *loc): flag = True try: self.find_web_element(*loc).is_displayed() except: flag = False return flag # if EC.visibility_of_element_located(*loc)(self.wd) : # isdisable = True # else: # isdisable = False # return isdisable def __inser_img(self, passorfailed, imgname): time_loc = time.strftime("%m%d_%H%M%S", time.localtime()) file_path = os.path.abspath(__file__) file_path = os.path.join(file_path + "/../../log/%s_%s_%s.png" % (passorfailed, imgname, time_loc)) self.wd.get_screenshot_as_file(file_path) logg.debug('Insert— %s_img %s ' % (passorfailed, (file_path))) def insert_warning_img(self, imgname): sleep(0.5) logg.warn(imgname) self.__inser_img("waring", imgname) def insert_error_img(self, imgname): sleep(0.2) logg.error(imgname) self.__inser_img("error", imgname) def insert_success_img(self, imgname): sleep(0.2) logg.info(imgname) self.__inser_img("success", imgname) def insert_debug_img(self, imgname): sleep(0.2) logg.debug(imgname) self.__inser_img("debug", imgname) def check_dialog_success(self, value): if self.check_element_isexist(self.e_common.success_dialog): flag = True self.insert_success_img(value + "_dialog_success") else: flag = False self.insert_error_img(value + "_dialog_fail") return flag @classmethod def server_url_conf(cls): cls.host = readconfig.ReadConfig().getserver('host') cls.port = readconfig.ReadConfig().getserver('port') urlvalue = cls.host + ":" + cls.port return urlvalue
class DevgrpPage(HomePage): global logg logg = LogHandler().getlog() e_common = GlobalElements.common e_devgrp = GlobalElements.devgrp def dev_list_elems(self, num): self.dev_list = (By.CSS_SELECTOR, "tr[data-index]:nth-child(%d)" % (num)) # devgrp_name_click = By.CSS_SELECTOR,"td.has-split[title="+groupname+"]" def init_web(self): flag = True self.correct_login() self.click_devgrp_manager_menu() logg.info("devgroup wait 3s") sleep(3) # self.click_btn(*self.check_default) if not self.check_groupname_result("default"): logg.error('initweb check default grp : error') flag = False return flag # self.brower_close() def create_devgrp(self, devgrpname): flag = True if self.check_groupname_result(devgrpname): logg.warning('Dev group %s already exist' % devgrpname) return False logg.info("Dev group %s is not exist ,create it." % devgrpname) self.click_btn(*self.e_devgrp.create_devgrp_loc) self.type_text(self.e_devgrp.input_devgrp_name_loc, devgrpname) self.click_btn(*self.e_devgrp.input_devgrp_name_confirm) logg.debug(print(*self.e_devgrp.input_devgrp_name_confirm)) if self.check_groupname_result(devgrpname): logg.info("create devgrp %s success ." % devgrpname) else: self.insert_error_img('** check %s grp : error ' % (devgrpname)) flag = False return flag def click_devgroup_name(self, groupname): try: self.click_btn(By.CSS_SELECTOR, "td.has-split[title=" + groupname + "]") sleep(0.4) return True except Exception as e: logg.error('%s error : %s' % (sys._getframe().f_code.co_name, e)) return False def _get_firstdevID(self): value = self.get_element_text( *self.e_devgrp.get_bynode_devlist_firstdevID) logg.info("firstdevID : %s " % (value)) return value def check_dialog_error(self): if self.check_element_isexist(self.e_common.error_dialog): flag = True self.insert_success_img("devgrp_dialog_error") else: flag = False self.insert_error_img("devgrp_dialog_error") sleeptime = 5 logg.info("** dialog check_dialog_error sleep %ss... " % (sleeptime)) sleep(sleeptime) return flag def check_dialog_warning(self, content=None): if self.check_element_isexist(self.e_common.waring_dialog): flag = True self.insert_success_img("devgrp_dialog_warning_%s" % (content)) else: flag = False self.insert_error_img("devgrp_dialog_warning_%s" % (content)) sleeptime = 5 logg.info("** dialog check_dialog_warning sleep %ss... " % (sleeptime)) sleep(sleeptime) return flag def check_dialog_success(self): if self.check_element_isexist(self.e_common.success_dialog): flag = True self.insert_success_img("devgrp_dialog_success") else: flag = False self.insert_error_img("devgrp_dialog_fail") # sleeptime = 5 # logg.info("** dialog check_dialog_success sleep %ss... "%(sleeptime)) # sleep(sleeptime) return flag def move_to_add_dev(self): self.mouse_move_to_element(*self.e_devgrp.bind_dev) def add_dev_by_node_noclick_devgrp(self): ''' 不主动选择设备分组,直接添加 bynode''' self.move_to_add_dev() self.click_btn(*self.e_devgrp.add_dev_bynode) sleep(0.5) if not self.check_dialog_warning("add_dev_by_node_noclick_devgrp"): logg.error("add_dev_by_node_noclick_devgrp error") return False logg.debug("add_dev_by_node_noclick_devgrp pass") return True def add_dev_by_id_noclick_devgrp(self): ''' 不主动选择设备分组,直接添加 byid''' self.move_to_add_dev() self.click_btn(*self.e_devgrp.add_dev_byid) sleep(0.5) if not self.check_dialog_warning(): logg.error("add_dev_by_devid_noclick_devgrp error") return False logg.debug("add_dev_by_devid_noclick_devgrp pass") return True def check_searchbynodeip_devlist_result(self): # 搜索ip后,应该会展示有 /dev try: c = self.check_element_isexist(self.e_devgrp.check_bynode_devlist) return True except Exception as e: logg.error("check_searchbynodeip_devlist_result error : %s" % (e)) return False def enter_node_ip_and_click(self, nodeip): self.type_text(self.e_devgrp.add_dev_input_ip, nodeip) if self.click_btn(*self.e_devgrp.add_dev_select_ip): logg.info("enter_node_ip_and_click okkkk") else: logg.error("enter_node_ip_and_click error ") return False waittime = 5 logg.info("wait enter_node_ip_and_click %ss " % (waittime)) sleep(waittime) if self.check_searchbynodeip_devlist_result(): return True else: return False def select_firstonedevice_from_nodeipdevlist(self): if self.click_btn(*self.e_devgrp.bynode_devlist_firstdev): # 获取一下当时的devID value = self._get_firstdevID() with open('.tmp', 'w') as f: f.write(value) f.close() self.click_btn(*self.e_devgrp.dev_list_add_submit) sleep(2) if self.check_dialog_success(): logg.info("select_firstonedevice_from_nodeipdevlist check ok") return True else: logg.error("check_dialog_success error") return False def add_dev_by_nodeip(self): self.move_to_add_dev() if self.click_btn(*self.e_devgrp.add_dev_bynode): sleep(0.5) logg.info("通过nodeip增加设备 成功") return True else: self.insert_error_img("通过nodeip增加设备 失败") return False def add_dev_to_devgrp_by_nodeip(self, devgrpname, nodeip): """仅仅添加第一个设备到devgrpname""" sleep(1) if self.click_devgroup_name(devgrpname): if self.add_dev_by_nodeip(): if self.enter_node_ip_and_click(nodeip): if self.select_firstonedevice_from_nodeipdevlist(): logg.info( "select_firstonedevice_from_nodeipdevlist pass") return True else: return False else: return False else: return False else: return False pass def add_dev_to_devgrp_by_deviceid(self): pass def delete_devgrp_noclickfirst_devgrp(self): ''' 不主动选择设备分组,直接删除 ''' pass def delete_devgrp_by_select_devgrp(self): pass def check_limit_switch(self): pass def open_limit_switch(self): pass def close_limit_switch(self): pass def set_devgrp_limit(self): pass def check_groupname_result(self, groupname): flag = False if self.check_element_isexist( (By.CSS_SELECTOR, "td.has-split[title=" + groupname + "]")): flag = True logg.info("%s group exist is : %s" % (groupname, str(flag))) return flag