def findElements(self, by, value): by_map = { 'id': By.ID, 'name': By.NAME, 'class': By.CLASS_NAME, 'tag': By.TAG_NAME, 'link': By.LINK_TEXT, 'plink': By.PARTIAL_LINK_TEXT, 'css': By.CSS_SELECTOR, 'xpath': By.XPATH } if by in by_map.keys(): try: elements = WebDriverWait( self.driver, 10, ignored_exceptions=None).until( EC.presence_of_all_elements_located( (by_map[by], value))) log1.info(by + " Query Element: " + value) return elements except NoSuchElementException: log1.error("Not Found Element Or Timeout", exc_info=1) self.getImage("Not Found Element Or Timeout") else: log1.error(by + " Variable Error", exc_info=1) self.getImage(by + " Variable Error")
def getImage(self, imageName): img = ReadConfig().getValue(section='located', name='image') try: self.driver.get_screenshot_as_file(img + imageName + ".png") log1.info("Screenshot Image") except BaseException: log1.error("Screenshot Image Fail", exc_info=1)
def open_excel(): try: excelPath = ReadConfig().getValue(section='located', name='excel') log1.info("Open Excel") return xlrd.open_workbook(excelPath) except BaseException: log1.error("Open Excel Error", exc_info=1) raise IOError("Open Excel Error")
def __init__(self): driver = self.getBrowsers() try: log1.info("-------------------- test start --------------------") self.driver = driver log1.info("Load Web Driver Success") except Exception: log1.error("Load Web Driver Fail", exc_info=1) self.getImage("Load Web Driver Fail")
def sendKeys(self, element, text): element.clear() log1.info("Element Clear Text") try: element.send_keys(text) log1.info("Element Input Text: " + text) except BaseException: log1.error("Not Found Element Or Input Error", exc_info=1) self.getImage("Not Found Element Or Input Error")
def click(self, element): try: element.click() log1.info("Element Click") except BaseException: if self.isDisplayed(element) is True: self.sleepWait(3) element.click() log1.info("Element Click") else: log1.error('Not Found Element', exc_info=1)
def run_case_radio_button_demo(self, sex): optradio = self.driver.findElements("name", "optradio") try: for i in optradio: if self.driver.getAttribute(i, "value") == sex: self.driver.click(i) button = self.driver.findElement("id", "buttoncheck") self.driver.click(button) except BaseException: log1.error("Not Found Element", exc_info=1) radiobutton = self.driver.findElement("class", "radiobutton") return self.driver.getText(radiobutton)
def select(type, element, value): try: if type == "index": Select(element).select_by_index(value) log1.info("Select Element Index") elif type == "value": Select(element).select_by_value(value) log1.info("Select Element Value") elif type == "text": Select(element).select_by_visible_text(value) log1.info("Select Element text") else: log1.error('please input type', exc_info=1) except BaseException: log1.error('Not Found Element', exc_info=1)
def run_case_group_radio_buttons_demo(self, sex, age): gender = self.driver.findElements("name", "gender") ageGroup = self.driver.findElements("name", "ageGroup") try: for i in gender: if self.driver.getAttribute(i, "value") == sex: self.driver.click(i) for j in ageGroup: if self.driver.getAttribute(j, "value") == age: self.driver.click(j) button = self.driver.findElement("xpath", "//*[@id='easycont']/div/div[2]/div[2]/div[2]/button") self.driver.click(button) except BaseException: log1.error("Not Found Element", exc_info=1) groupradiobutton = self.driver.findElement("class", "groupradiobutton") return self.driver.getText(groupradiobutton)
def deselect(type, element, value=""): try: if type == "index" and value != "": Select(element).deselect_by_index(value) log1.info("Deselect Element Index") elif type == "value" and value != "": Select(element).deselect_by_value(value) log1.info("Deselect Element Value") elif type == "text" and value != "": Select(element).deselect_by_visible_text(value) log1.info("Deselect Element Text") elif type == "all" and value == "": Select(element).deselect_all() log1.info("Deselect All Element") else: log1.error('please input type', exc_info=1) except BaseException: log1.error("Not Found Select")
def getAllSelect(element): try: log1.info("Get All Select") return Select(element).all_selected_options except BaseException: log1.error("Not Found All Select")