class UploadScormPage(KngManagement): def __init__(self, driver, logger): self.logger = logger super().__init__(driver, logger) self.get_ele = GetElement(driver, logger) def get_upload_html_file_btn(self): return self.get_ele.get_element("upload_html_btn", "upload_html_element") def get_course_name(self): return self.get_ele.get_element("course_name", "upload_html_element") def get_submit_btn(self): return self.get_ele.get_element("submit_btn", "upload_html_element") def get_upload_status(self): return self.get_ele.get_element("upload_status", "upload_html_element") def upload_html(self): self.enter_upload_html_page() upload_html_element = self.get_upload_html_file_btn() time.sleep(2) self.click_element(upload_html_element) cmd = settings.AUTOIT3_SCRIPT_PATH + " " + args[0] + " " + kwargs[ "filename"] self.execute_autoit_script(cmd) self.send_msg(self.get_course_name(), settings.HTML_COURSE_NAME) if self.get_upload_status(): time.sleep(1) self.click_element(self.get_submit_btn()) self.switch_window(0)
class LoginPage(BasePage): def __init__(self,driver,url): super(LoginPage,self).__init__(driver,url) self.get_ele = GetElement(driver) def get_username_element(self): return self.get_ele.get_element("username","login_element") def get_password_element(self): return self.get_ele.get_element("password","login_element") def get_login_btn_element(self): return self.get_ele.get_element("login_btn","login_element") def get_frame1_element(self): return self.get_ele.get_element("iframe1","login_element") def get_frame2_element(self): return self.get_ele.get_element("iframe2","login_element") def login(self,*args): self.open() self.switch_iframe(self.get_frame1_element()) self.switch_iframe(self.get_frame2_element()) self.send_msg(self.get_username_element(),args[0]) self.send_msg(self.get_password_element(),args[1]) self.click_element(self.get_login_btn_element()) self.switch_iframe()
class HomePage(BasePage): def __init__(self, driver, url): super(HomePage, self).__init__(driver, url) self.get_ele = GetElement(driver) def get_enter_album_element(self): return self.get_ele.get_element("enter_aibum", "home_element") def enter_album(self): self.click_element(self.get_enter_album_element())
class RegisterPage(BasePage): url = settings.URL def __init__(self, driver): self.driver = driver self.get_e = GetElement(self.driver) super(RegisterPage, self).__init__(self.driver, self.url) # def get_value(self): # self.url = settings.URL def get_email_element(self): email_element = self.get_e.get_element("email", "register_element") return email_element def get_username_element(self): return self.get_e.get_element("username", "register_element") def get_password_element(self): return self.get_e.get_element("password", "register_element") def get_code_image_element(self): return self.get_e.get_element("code_image", "register_element") def get_code_text_element(self): return self.get_e.get_element("code_text", "register_element") def get_register_btn_element(self): return self.get_e.get_element("register_btn", "register_element") def get_email_error_element(self): return self.get_e.get_element("email_error", "register_element") def get_email_error_text(self): try: text = self.get_email_error_element().text return text except: return None def register(self, *args, **kwargs): self.open() self.send_msg(self.get_email_element(), args[0]) self.send_msg(self.get_username_element(), args[1]) self.send_msg(self.get_password_element(), args[2]) self.send_msg(self.get_code_text_element(), args[3]) self.click_element(self.get_register_btn_element()) def assert_result(self, expect, expect_method): func = getattr(self, expect_method) res = func() if expect == res: return True else: return False
class UploadPage(KngManagement): url = settings.KNG_URL def __init__(self,driver,logger): self.logger = logger super(UploadPage,self).__init__(driver,self.logger) self.get_ele = GetElement(driver,self.logger) def get_upload_file_btn(self): return self.get_ele.get_element("upload_file_btn","upload_element") def get_continue_upload_btn(self): return self.get_ele.get_element("continue_upload_btn","upload_element") def get_submit_file_btn(self): return self.get_ele.get_element("submit_file_btn","upload_element") def get_upload_status(self): return self.get_ele.get_element("upload_status","upload_element",200) def upload_file(self,*args,**kwargs): self.enter_upload_page() upload_btn = self.get_upload_file_btn() time.sleep(2) self.click_element(upload_btn) #平均cmd语句,args[0]是指浏览器类型 cmd = settings.AUTOIT3_SCRIPT_PATH + " " + args[0] + " "+ kwargs["filename"] self.execute_autoit_script(cmd) if self.get_upload_status(): time.sleep(1) self.click_element(self.get_submit_file_btn()) self.switch_window(0) if self.check_file(kwargs["file_type"]): s = ShowPage(self.driver,self.logger) if s.check_encode_status(kwargs["file_type"]): s.delete_download_file(**kwargs) s.download() self.close() self.switch_window(0)
def __init__(self, driver): self.driver = driver self.get_e = GetElement(self.driver) super(RegisterPage, self).__init__(self.driver, self.url)
class AlbumPage(BasePage): def __init__(self, driver): super(AlbumPage, self).__init__(driver) self.get_ele = GetElement(driver) def get_create_album_element(self): return self.get_ele.get_element("create_album_btn", "album_element") def get_frame1_element(self): return self.get_ele.get_element("iframe1", "album_element") def get_album_name_element(self): return self.get_ele.get_element("album_name", "album_element") def get_confirm_btn_element(self): return self.get_ele.get_element("confirm_btn", "album_element") def get_sort_btn_element(self): return self.get_ele.get_element("sort_btn", "album_element") def get_self_def_btn_element(self): return self.get_ele.get_element("self_def_sort_btn", "album_element") def get_album_A_element(self): return self.get_ele.get_element("album_A", "album_element") def get_album_B_element(self): return self.get_ele.get_element("album_B", "album_element") def get_album_C_element(self): return self.get_ele.get_element("album_C", "album_element") def get_save_btn_element(self): return self.get_ele.get_element("save_btn", "album_element") def get_album_test_element(self): return self.get_ele.get_element("album_test", "album_element") def get_delete_btn_element(self): return self.get_ele.get_element("delete_btn", "album_element") def get_del_confirm_element(self): return self.get_ele.get_element("del_confirm_btn", "album_element") def create_album(self, album_name=None, album_desc=None, album_privilege=None): self.click_element(self.get_create_album_element()) self.switch_iframe(self.get_frame1_element()) if album_name: self.send_msg(self.get_album_name_element(), album_name) self.click_element(self.get_confirm_btn_element()) #self.switch_iframe() def alert_accept(self, msg): alert = self.switch_alert() if alert.text == msg: return True else: return False alert.accept() def move(self): self.click_element(self.get_sort_btn_element()) self.click_element(self.get_self_def_btn_element()) self.drag_and_drop_element(al.get_album_A_element(), al.get_album_C_element()) self.click_element(self.get_save_btn_element()) def delete_album(self, album): self.move_to_target(album) self.click_element(self.get_delete_btn_element()) self.click_element(self.get_del_confirm_element())
def __init__(self, driver): super(AlbumPage, self).__init__(driver) self.get_ele = GetElement(driver)
def __init__(self, driver, logger): self.logger = logger super().__init__(driver, logger) self.get_ele = GetElement(driver, logger)
class LoginPage(BasePage): url = settings.BASE_URL _instance_lock = threading.Lock() def __init__(self, driver, logger): self.logger = logger super(LoginPage, self).__init__(driver, self.logger, self.url) self.get_ele = GetElement(driver, self.logger) def __new__(cls, *args, **kwargs): ''' 实现单例模式 :param args: :param kwargs: :return: ''' if not hasattr(cls, "_instance"): with cls._instance_lock: if not hasattr(cls, "_instance"): cls._instance = super().__new__(cls) return cls._instance def get_username_element(self): return self.get_ele.get_element("username", "login_element") def get_password_element(self): return self.get_ele.get_element("password", "login_element") def get_login_btn_element(self): return self.get_ele.get_element("login_btn", "login_element") def get_login_type_element(self): return self.get_ele.get_element("login_type", "login_element") def get_sms_login_type_element(self): return self.get_ele.get_element("sms_login_type", "login_element") def get_phone_no_element(self): return self.get_ele.get_element("phone_no_text", "login_element") def get_code_text_element(self): return self.get_ele.get_element("code_text", "login_element") def get_code_img_element(self): return self.get_ele.get_element("code_img", "login_element") def get_dcode_text_element(self): return self.get_ele.get_element("dcode_text", "login_element") def get_dcode_btn_element(self): return self.get_ele.get_element("dcode_btn", "login_element") def get_sms_login_btn_element(self): return self.get_ele.get_element("sms_login_btn", "login_element") def login(self, *args, **kwargs): self.open() self.click_element(self.get_login_type_element()) self.send_msg(self.get_username_element(), kwargs["username"]) self.send_msg(self.get_password_element(), kwargs["password"]) self.click_element(self.get_login_btn_element()) def login_sms(self, *args, **kwargs): self.open() self.click_element(self.get_sms_login_type_element()) self.send_msg(self.get_phone_no_element(), kwargs["phone_no"]) #点击验证码输入框 code_text_element = self.get_code_text_element() self.logger.info("点击验证码输入框(点击之后验证码会发生变化,所以要先点击后截图)") self.click_element(code_text_element) #识别图片验证码 self.logger.info("识别图片验证码") capt = HandleCaptcha(self.driver, self.logger) code = capt.handle_captcha(self.get_code_img_element()) #输入图片验证码 self.logger.info("输入图片验证码") self.send_msg(code_text_element, code) #点击获取动态码 self.logger.info("点击获取手机动态验证码") self.click_element(self.get_dcode_btn_element())
settings.PIC_RATIO * right, settings.PIC_RATIO * bottom)) img.save(settings.CODE_LOC) def distinguish_with_pytesseract(self): im = Image.open(settings.CODE_LOC) text = pytesseract.image_to_string(im) return text def distinguish(self): r = ShowapiRequest("http://route.showapi.com/184-4", "94789", "6de63c760b684ad1a928d0158cfaef95") r.addBodyPara("typeId", "34") r.addBodyPara("convert_to_jpg", "0") r.addBodyPara("needMorePrecise", "0") r.addFilePara("image", settings.CODE_LOC) #文件上传时设置 res = r.post() return res.json()["showapi_res_body"]["Result"] if __name__ == "__main__": driver = BrowserEngine("chrome").start_browser() log = LogRecord() logger = log.log_record() h = HandleCaptcha(driver, logger) driver.get("http://jgtest.qida.yunxuetang.com.cn/login.htm") get_ele = GetElement(driver, logger) get_ele.get_element("sms_login_type", "login_element").click() code = get_ele.get_element("code_img", "login_element") h.handle_captcha(code) #print(h.distinguish())
class ShowPage(KngManagement): url = settings.KNG_URL def __init__(self, driver, logger): self.logger = logger super(ShowPage, self).__init__(driver, self.logger) self.get_ele = GetElement(driver, self.logger) def get_pic_or_doc_show_element(self): return self.get_ele.get_element("pic_or_doc_show", "show_element") def get_video_show_element(self): return self.get_ele.get_element("video_show", "show_element") def get_audio_show_element(self): return self.get_ele.get_element("audio_show", "show_element") def get_scorm_show_element(self): return self.get_ele.get_element("scorm_show", "show_element") def get_html_show_element(self): return self.get_ele.get_element("html_show", "show_element") def get_fresh_btn_element(self): return self.get_ele.get_element("fresh_btn", "show_element") def get_download_element(self): return self.get_ele.get_element("download_btn", "show_element") def download(self): ele = self.get_download_element() self.click_element(ele) def delete_download_file(self, *args, **kwargs): #如果本地已有文件,则删除 OperaFile(kwargs["filename"], kwargs["file_type"], self.logger).delete_file() def check_download_status(self, *args, **kwargs): #这里之所以用上传文件路径,是因为上传路径是可以控制的,而下载路径可能随环境改变而变动(此处下载路径没地方设置) #下载路径拼接上传文件名称,则构成下载之后的文件名称 op_file = OperaFile(kwargs["filename"], kwargs["file_type"], self.logger) for i in range(10): if op_file.check_file_status(): return "true" else: time.sleep(10) return "false" def check_pic_encode_status(self): # 在列表页点击图片名称 self.get_ele.wait_element_disappear("loading_status", "kng_element") jpg_file_element = self.get_jpg_file_element() self.click_element(jpg_file_element) self.switch_window() ele = self.get_pic_or_doc_show_element() if ele: self.logger.info("图片转换成功") return True else: self.logger.error("图片转换失败") return False def check_doc_encode_status(self): self.get_ele.wait_element_disappear("loading_status", "kng_element") self.click_element(self.get_doc_file_element()) self.switch_window() for i in range(10): if self.get_ele.wait_element_disappear("doc_encode_status", "show_element"): break else: #整个页面刷新,局部刷新按钮点击之后有bug self.refresh() ele = self.get_pic_or_doc_show_element() if ele: self.logger.info("文档转换成功") return True else: self.logger.error("文档转换失败") return False def check_video_encode_status(self): self.get_ele.wait_element_disappear("loading_status", "kng_element") self.click_element(self.get_video_file_element()) self.switch_window() for i in range(20): if self.get_ele.wait_element_disappear("video_encode_status", "show_element"): break else: # 整个页面刷新,局部刷新按钮点击之后有bug self.refresh() ele = self.get_video_show_element() if ele: self.logger.info("视频转换成功") return True else: self.logger.error("视频转换失败") return False def check_audio_encode_status(self): self.get_ele.wait_element_disappear("loading_status", "kng_element") self.click_element(self.get_audio_file_element()) self.switch_window() for i in range(20): if self.get_ele.wait_element_disappear("audio_encode_status", "show_element"): break else: # 整个页面刷新,局部刷新按钮点击之后有bug self.refresh() ele = self.get_audio_show_element() if ele: self.logger.info("音频转换成功") return True else: self.logger.error("音频转换失败") return False def check_scorm_encode_status(self): self.get_ele.wait_element_disappear("loading_status", "kng_element") self.click_element(self.get_scorm_file_element()) self.switch_window() for i in range(20): if self.get_ele.wait_element_disappear("scorm_encode_status", "show_element"): break else: # 整个页面刷新,局部刷新按钮点击之后有bug self.refresh() ele = self.get_scorm_show_element() self.close() self.switch_window(0) if ele: self.logger.info("scorm转换成功") return "true" else: self.logger.error("scorm转换失败") return "false" def check_html_encode_status(self): self.get_ele.wait_element_disappear("loading_status", "kng_element") self.click_element(self.get_scorm_file_element()) self.switch_window() for i in range(20): if self.get_ele.wait_element_disappear("html_encode_status", "show_element"): break else: # 整个页面刷新,局部刷新按钮点击之后有bug self.refresh() ele = self.get_html_show_element() self.close() self.switch_window(0) if ele: self.logger.info("html转换成功") return "true" else: self.logger.error("html转换失败") return "false" def check_encode_status(self, file_type): if file_type == "pic": #在播放页查看图片是否正常 return self.check_pic_encode_status() elif file_type == "doc": return self.check_doc_encode_status() elif file_type == "video": return self.check_video_encode_status() elif file_type == "audio": return self.check_audio_encode_status() elif file_type == "scorm": return self.check_scorm_encode_status() else: return self.check_html_encode_status()
def __init__(self,driver,logger): self.logger = logger super(UploadPage,self).__init__(driver,self.logger) self.get_ele = GetElement(driver,self.logger)
def __init__(self, driver, logger): self.logger = logger super(KngManagement, self).__init__(driver, self.logger, self.url) self.get_ele = GetElement(driver, self.logger)
def __init__(self,driver,url): super(LoginPage,self).__init__(driver,url) self.get_ele = GetElement(driver)
class KngManagement(BasePage): url = settings.KNG_URL def __init__(self, driver, logger): self.logger = logger super(KngManagement, self).__init__(driver, self.logger, self.url) self.get_ele = GetElement(driver, self.logger) def get_share_kng_btn_element(self): return self.get_ele.get_element("share_kng_btn", "kng_element") def get_jpg_file_element(self): return self.get_ele.get_element("jpg_file_element", "kng_element") def get_doc_file_element(self): return self.get_ele.get_element("doc_file_element", "kng_element") def get_video_file_element(self): return self.get_ele.get_element("video_file_element", "kng_element") def get_audio_file_element(self): return self.get_ele.get_element("audio_file_element", "kng_element") def get_doc_file_element(self): return self.get_ele.get_element("doc_file_element", "kng_element") def get_video_file_element(self): return self.get_ele.get_element("video_file_element", "kng_element") def get_audio_file_element(self): return self.get_ele.get_element("audio_file_element", "kng_element") def get_scorm_file_element(self): return self.get_ele.get_element("scorm_file_element", "kng_element") def get_html_file_element(self): return self.get_ele.get_element("html_file_element", "kng_element") def check_jpg_file(self): if self.get_jpg_file_element(): return True else: return False def check_doc_file(self): if self.get_doc_file_element(): return True else: return False def check_video_file(self): if self.get_video_file_element(): return True else: return False def check_audio_file(self): if self.get_audio_file_element(): return True else: return False def check_scorm_file(self): if self.get_scorm_file_element(): return True else: return False def check_html_file(self): if self.get_html_file_element(): return True else: return False def check_file(self, file_type): if file_type == "pic": #在播放页查看图片是否正常 return self.check_jpg_file() elif file_type == "doc": return self.check_doc_file() elif file_type == "video": return self.check_video_file() elif file_type == "audio": return self.check_audio_file() elif file_type == "scorm": return self.check_scorm_file() else: return self.check_html_file() # check_file_dict = {"pic":self.check_jpg_file, # "doc":self.check_doc_file, # "video":self.check_video_file, # "audio":self.check_audio_file(), # "scorm":self.check_scorm_file(), # "html":self.check_html_file()} # func = check_file_dict.get(file_type,default="类型错误") # if func != "类型错误": # return func() # else: # self.logger.warning("传入的文件类型错误,无法检测其是否在知识列表里") def get_first_edit_btn_element(self): return self.get_ele.get_element("first_file_edit_btn", "kng_element") def get_first_delete_btn_element(self): return self.get_ele.get_element("first_file_delete_btn", "kng_element") def get_loading_status(self): return self.get_ele.get_element("loading_status", "kng_element") def get_scorm_upload_btn(self): return self.get_ele.get_element("upload_scorm_btn", "kng_element") def get_html_upload_btn(self): return self.get_ele.get_element("upload_html_btn", "kng_element") def enter_upload_page(self): self.open() self.click_element(self.get_share_kng_btn_element()) self.switch_window() def enter_upload_scorm_page(self): self.open() self.move_to_target(self.get_share_kng_btn_element()) self.click_element(self.get_scorm_upload_btn()) self.switch_window() def enter_upload_html_page(self): self.open() self.move_to_target(self.get_share_kng_btn_element()) self.click_element(self.get_html_upload_btn()) self.switch_window() def delete_file(self): self.move_to_target(self.get_first_edit_btn_element()) self.click_element(self.get_first_delete_btn_element()) al = self.switch_alert() al and al.accept() self.get_ele.wait_element_disappear("loading_status", "kng_element")
def __init__(self, driver, logger): self.logger = logger super(LoginPage, self).__init__(driver, self.logger, self.url) self.get_ele = GetElement(driver, self.logger)