def VerifyEnabled(cls, trueOrfalse): log.step_normal(u"Element [%s]: Verify Enabled = [%s]" % (cls.__name__, trueOrfalse)) cls.__wait() elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) is_disabled = elements[cls.index].get_attribute("disabled") log.step_normal(u"Element [%s]: attribute 'is_disabled' = [%s]" % (cls.__name__, is_disabled)) if is_disabled == "true": if trueOrfalse == False: log.step_pass("Pass...") else: log.step_fail("Fail...") elif elements[cls.index].is_enabled(): if trueOrfalse == True: log.step_pass("Pass") else: log.step_fail("Fail") else: log.step_fail("Not verified.") cls.__clearup()
def __wait_for_disappearing(cls): t = 0 while t < 120: t = t + 1 try: elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) except NoSuchElementException: log.step_normal("Element [%s]: NoSuchElementException." % cls.__name__) elements = [] except BadStatusLine: log.step_warning("Element [%s]: BadStatusLine." % cls.__name__) continue except UnexpectedAlertPresentException: log.step_warning("Element [%s]: UnexpectedAlertPresentException." % cls.__name__) if len(elements) == 0: return True else: time.sleep(0.5) log.step_normal("Element [%s]: WairForDisappearing... Found [%s] Element. Tried [%s] Times." % (cls.__name__, len(elements), t)) return False
def ClickList(cls): log.step_normal("Element [%s]: Do ClickList()" % cls.__name__) if env.RUNNING_BROWSER in ("Chrome", "Firefox", "IE", "Safari"): js1 = "var q = document.documentElement.scrollTop=0" env.driver.execute_script(js1) time.sleep(3) js2 = "var q = document.documentElement.scrollTop=100000" env.driver.execute_script(js2) # cls.__wait() WebDriverWait(env.driver, 10).until(lambda the_driver: the_driver.find_element(cls.by, cls.value).is_displayed()) i = 0 while i < 1: elements = env.driver.find_elements(cls.by, cls.value) rd = random.randint(0, len(elements)-1) action = webdriver.ActionChains(env.driver) action.move_to_element(elements[rd]) action.click(elements[rd]) action.perform() time.sleep(2) i += 1 env.driver.switch_to_window(env.driver.window_handles[-1]) env.driver.maximize_window() time.sleep(3) cls.__clearup()
def Refresh(cls, times=4): log.step_normal(u"Element [%s]: Browser Refresh" % (cls.__name__,)) for i in range(times): action = webdriver.ActionChains(env.threadlocal.BROWSER) action.key_down(Keys.CONTROL).send_keys(Keys.F5).key_up(Keys.CONTROL).perform() time.sleep(5)
def cell_by_colname(self, rowx, col_name): for colx in range(0, self.sheet.ncols): if self.data[0][colx] == col_name: log.step_normal("[%s][%s]=[%s]" % (colx, col_name, self.data[rowx][colx])) return self.data[rowx][colx] return "ERROR: NO DATA FETCHED!"
def Set(cls, value): if value == "": return if value == "SET_EMPTY": value = "" log.step_normal(u"Element [%s]: Set Value [%s]." % (cls.__name__, value)) # cls.__wait() WebDriverWait(env.driver, 10).until(lambda the_driver: the_driver.find_element(cls.by, cls.value).is_displayed()) elements = env.driver.find_elements(cls.by, cls.value) if elements[cls.index].tag_name == "select" or elements[cls.index].tag_name == "ul": cls.Select(value) time.sleep(3) else: elements[cls.index].clear() action = webdriver.ActionChains(env.driver) action.send_keys_to_element(elements[cls.index], value) action.perform() time.sleep(3) cls.__clearup()
def GetParentElement(cls): log.step_normal("Element [%s]: GetParentElement()" % (cls.__name__)) cls.__wait() elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) return elements[cls.index].parent()
def VerifyExistence(cls, trueORfalse): log.step_normal("Element [%s]: Verify Existence = [%s]." % (cls.__name__, trueORfalse)) if trueORfalse is True: cls.__wait_for_appearing() else: cls.__wait_for_disappearing() elements = env.driver.find_elements(cls.by, cls.value) log.step_normal("Element [%s]: Count = [%s]" % (cls.__name__, len(elements))) cls.__clearup() if len(elements) > 0: if trueORfalse is True: log.step_pass("Exist!") else: log.step_fail("Exist!") else: if trueORfalse is False: log.step_pass("Not Exist!") else: log.step_fail("Not Exist!")
def Select(cls, value): if value == "": return log.step_normal("Element [%s]: Do Select [%s]." % (cls.__name__, value)) cls.__wait() elements = env.BROWSER.find_elements(cls.by, cls.value) #### select ################ if elements[cls.index].tag_name == "select": options = elements[cls.index].find_elements_by_tag_name('option') for option in options: if option.text == value: option.click() break #### ul ################ elif elements[cls.index].tag_name == "ul": lis = elements[cls.index].find_elements_by_tag_name('li') for li in lis: if li.text == value: li.click() break #### NOT Supported ################ else: log.step_fail("Element [%s]: Tag Name [%s] Not Support [Select]." % (cls.__name__, elements[cls.index].tag_name)) cls.__clearup()
def ClickList(cls): log.step_normal("Element [%s]: Do ClickList()" % cls.__name__) if env.RUNNING_BROWSER in ("Chrome", "Firefox", "IE", "Safari"): js1 = "var q = document.documentElement.scrollTop=0" env.driver.execute_script(js1) time.sleep(3) js2 = "var q = document.documentElement.scrollTop=100000" env.driver.execute_script(js2) # cls.__wait() WebDriverWait(env.driver, 10).until(lambda the_driver: the_driver.find_element( cls.by, cls.value).is_displayed()) i = 0 while i < 1: elements = env.driver.find_elements(cls.by, cls.value) rd = random.randint(0, len(elements) - 1) action = webdriver.ActionChains(env.driver) action.move_to_element(elements[rd]) action.click(elements[rd]) action.perform() time.sleep(2) i += 1 env.driver.switch_to_window(env.driver.window_handles[-1]) env.driver.maximize_window() time.sleep(3) cls.__clearup()
def Set(cls, value): if value == "": return if value == "SET_EMPTY": value = "" log.step_normal(u"Element [%s]: Set Value [%s]." % (cls.__name__, value)) # cls.__wait() WebDriverWait(env.driver, 10).until(lambda the_driver: the_driver.find_element( cls.by, cls.value).is_displayed()) elements = env.driver.find_elements(cls.by, cls.value) if elements[cls.index].tag_name == "select" or elements[ cls.index].tag_name == "ul": cls.Select(value) time.sleep(3) else: elements[cls.index].clear() action = webdriver.ActionChains(env.driver) action.send_keys_to_element(elements[cls.index], value) action.perform() time.sleep(3) cls.__clearup()
def __clearup(cls): if cls.index != 0: log.step_normal( "Element [%s]: The Operation Element Index = [%s]." % (cls.__name__, cls.index)) cls.index = 0
def cell_by_rowname(self, row_name, colx): for rowx in range(0, self.sheet.nrows): if self.data[rowx][0] == row_name: log.step_normal("[%s][%s]=[%s]" % (row_name, colx, self.data[rowx][colx])) return self.data[rowx][colx] return "ERROR: NO DATA FETCHED!"
def AlertSendKeys(cls, value): log.step_normal("AlertSendKeys [%s]" % value) try: env.threadlocal.BROWSER.switch_to.alert.send_keys(value) env.threadlocal.BROWSER.switch_to.default_content() except: log.step_warning(str(sys.exc_info()))
def SwitchToDefaultWindow(cls): log.step_normal("SwitchToDefaultWindow()") log.step_normal("Switch To The Default Window of: %s" % str(env.driver.window_handles)) try: env.driver.switch_to.window(env.driver.window_handles[0]) except: pass
def isElementExist(xpath): try: log.step_normal("find_elements_by_xpath: {0} is existed, return True.".format(xpath)) element = driver.find_elements_by_xpath(xpath)[0] return True except Exception: log.step_normal("find_elements_by_xpath: {0} is not existed, return False.".format(xpath)) return False
def GetElementObj(cls): #### get real time obj counts, without waiting. log.step_normal("Element [%s]: GetElementObj." % (cls.__name__)) cls.__wait() elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) cls.__clearup() return elements[cls.index]
def SwitchToDefaultContent(cls): log.step_normal("SwitchToDefaultContent()") try: # env.threadlocal.BROWSER.switch_to_default_content() env.threadlocal.BROWSER.switch_to.default_content() except: log.step_warning("env.threadlocal.BROWSER.switch_to.default_content()") pass
def AlertTextHave(cls, txt_value): log.step_normal("AlertTextHave [%s]" % txt_value) alert_text = env.threadlocal.BROWSER.switch_to_alert().text() if txt_value in alert_text: log.step_pass("pass") else: log.step_fail("fail") env.threadlocal.BROWSER.switch_to_default_content()
def AlertTextHave(cls, txt_value): log.step_normal("AlertTextHave [%s]" % txt_value) alert_text = env.BROWSER.switch_to_alert().text() if txt_value in alert_text: log.step_pass("pass") else: log.step_fail("fail") env.BROWSER.switch_to_default_content()
def GetObjectsCount(cls): log.step_normal("Element [%s]: GetObjectsCount." % (cls.__name__)) cls.__wait_for_appearing() elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) log.step_normal("Element [%s]: GetObjectsCount = [%s]" % (cls.__name__, len(elements))) cls.__clearup() return len(elements)
def SwitchToDefaultWindow(cls): log.step_normal("SwitchToDefaultWindow()") log.step_normal("Switch To The Default Window of: %s" % str(env.threadlocal.BROWSER.window_handles)) try: env.threadlocal.BROWSER.switch_to.window(env.threadlocal.BROWSER.window_handles[0]) except: log.step_warning("env.threadlocal.BROWSER.switch_to.window(env.threadlocal.BROWSER.window_handles[0])") pass
def GetInnerHTML(cls): log.step_normal(u"Element [%s]: GetInnerHTML()" % (cls.__name__, )) cls.__wait() elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) log.step_normal(u"Element [%s]: InnerHTML = [%s]" % (cls.__name__, elements[cls.index].get_attribute('innerHTML'))) cls.__clearup() return elements[cls.index].get_attribute('innerHTML')
def VerifyAttribute(cls, name, value, method='equal'): ''' Example: NewClaim.Dates.ReminderDate.VerifyAttribute('ng-model', 'hello', method='equal') NewClaim.Dates.ReminderDate.VerifyAttribute('ng-model', 'hello', method='contain') NewClaim.Dates.ReminderDate.VerifyAttribute('ng-model', 'hello', method='in') NewClaim.Dates.ReminderDate.VerifyAttribute('ng-model', 'hello', method='not equal') Description for "method": equal => real value is 'hello' has => real value contains 'hello', e.g. 'hello world'. in => real value in 'hello', e.g. 'he' differ => real value not equal to 'hello', e.g. 'hehe' ''' log.step_normal("Element [%s]: VerifyAttribute [%s] <%s> [%s]." % (cls.__name__, name, method, value)) cls.__wait() elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) realvalue = elements[cls.index].get_attribute(name) if method.lower() == 'equal': if value == realvalue: log.step_pass("real value=[%s]" % realvalue) else: log.step_fail("real value=[%s]" % realvalue) elif method.lower() == 'not equal': if not value == realvalue: log.step_pass("real value=[%s]" % realvalue) else: log.step_fail("real value=[%s]" % realvalue) elif method.lower() == 'contain': if value in realvalue: log.step_pass("real value=[%s]" % realvalue) else: log.step_fail("real value=[%s]" % realvalue) elif method.lower() == 'not contain': if not value in realvalue: log.step_pass("real value=[%s]" % realvalue) else: log.step_fail("real value=[%s]" % realvalue) elif method.lower() == 'in': if realvalue in value: log.step_pass("real value=[%s]" % realvalue) else: log.step_fail("real value=[%s]" % realvalue) else: log.step_fail("code error.") cls.__clearup()
def ClickAndHold(cls): log.step_normal("Element [%s]: Do ClickAndHold()" % (cls.__name__)) cls.__wait() elements = env.BROWSER.find_elements(cls.by, cls.value) action = webdriver.ActionChains(env.BROWSER) action.click_and_hold(elements[cls.index]) action.perform() cls.__clearup()
def SendEnter(cls): log.step_normal(u"Element [%s]: SendEnter()" % (cls.__name__, )) cls.__wait() elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) action = webdriver.ActionChains(env.threadlocal.BROWSER) action.send_keys_to_element(elements[cls.index], Keys.ENTER) action.perform() cls.__clearup()
def ReleaseClick(cls): log.step_normal("Element [%s]: ReleaseClick()" % (cls.__name__)) cls.__wait() elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) action = webdriver.ActionChains(env.threadlocal.BROWSER) action.release(elements[cls.index]) action.perform() cls.__clearup()
def DoubleClick(cls): log.step_normal("Element [%s]: DoubleClick()" % (cls.__name__)) cls.__wait() elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) action = webdriver.ActionChains(env.threadlocal.BROWSER) action.double_click(elements[cls.index]) action.perform() cls.__clearup()
def GetInnerHTML(cls): log.step_normal(u"Element [%s]: GetInnerHTML." % (cls.__name__, )) # cls.__wait() WebDriverWait(env.driver, 10).until(lambda the_driver: the_driver.find_element(cls.by, cls.value).is_displayed()) elements = env.driver.find_elements(cls.by, cls.value) log.step_normal(u"Element [%s]: InnerHTML = [%s]" % (cls.__name__, elements[cls.index].get_attribute('innerHTML'))) cls.__clearup() return elements[cls.index].get_attribute('innerHTML').strip()
def GetAttribute(cls, attr): log.step_normal(u"Element [%s]: GetAttribute [%s]." % (cls.__name__, attr)) cls.__wait() elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) attr_value = elements[cls.index].get_attribute(attr) log.step_normal(u"Element [%s]: Attribute Value = [%s]." % (cls.__name__, attr_value)) cls.__clearup() return attr_value
def DoubleClick(cls): log.step_normal("Element [%s]: Do DoubleClick()" % (cls.__name__)) cls.__wait() elements = env.BROWSER.find_elements(cls.by, cls.value) action = webdriver.ActionChains(env.BROWSER) action.double_click(elements[cls.index]) action.perform() cls.__clearup()
def get_urlIndex(tagurl, urls): i = 0 has = -1 for url in urls: if tagurl.split('/')[2] in url: has = True log.step_normal('url在搜索结果第[%s]个' % i) return i i = i + 1 log.step_normal('url不在当前页') return has
def Click_No_Switch(cls): log.step_normal("Element [%s]: Do Click_No_Switch()" % cls.__name__) # cls.__wait() WebDriverWait(env.driver, 10).until(lambda the_driver: the_driver.find_element(cls.by, cls.value).is_displayed()) elements = env.driver.find_element(cls.by, cls.value) elements.click() time.sleep(3) cls.__clearup()
def SendEnter(cls): log.step_normal(u"Element [%s]: SendEnter()" % (cls.__name__, )) cls.__wait() elements = env.BROWSER.find_elements(cls.by, cls.value) action = webdriver.ActionChains(env.BROWSER) action.send_keys_to_element(elements[cls.index], Keys.ENTER) action.perform() cls.__clearup()
def ReleaseClick(cls): log.step_normal("Element [%s]: Do ReleaseClick()" % (cls.__name__)) cls.__wait() elements = env.BROWSER.find_elements(cls.by, cls.value) action = webdriver.ActionChains(env.BROWSER) action.release(elements[cls.index]) action.perform() cls.__clearup()
def IsVisible(cls): log.step_normal("Element [%s]: IsVisible?" % (cls.__name__)) cls.__wait() elements = env.BROWSER.find_elements(cls.by, cls.value) if elements[cls.index].is_displayed(): cls.__clearup() return True else: cls.__clearup() return False
def send_keys_to_dialog(title=r".*Upload.*", key_valus=r""): log.step_normal("send_keys_to_dialogue(%s, %s)" % (title, key_valus)) win_dialog = WindowFinder() win_dialog.find_window_wildcard(title) win_dialog.set_foreground() time.sleep(2) SendKeys.SendKeys(key_valus) SendKeys.SendKeys("{ENTER}")
def Click_No_Switch(cls): log.step_normal("Element [%s]: Do Click_No_Switch()" % cls.__name__) # cls.__wait() WebDriverWait(env.driver, 10).until(lambda the_driver: the_driver.find_element( cls.by, cls.value).is_displayed()) elements = env.driver.find_element(cls.by, cls.value) elements.click() time.sleep(3) cls.__clearup()
def IsVisible(cls): log.step_normal("Element [%s]: IsVisible?" % (cls.__name__)) cls.__wait() elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) if elements[cls.index].is_displayed(): cls.__clearup() return True else: cls.__clearup() return False
def movetoelement(cls): log.step_normal("Element [%s]: Do movetoelement()" % cls.__name__) # cls.__wait() WebDriverWait(env.driver, 10).until(lambda the_driver: the_driver.find_element(cls.by, cls.value).is_displayed()) element = env.driver.find_element(cls.by, cls.value) action = webdriver.ActionChains(env.driver) action.move_to_element(element) action.perform() time.sleep(3)
def ClickList_App(cls): log.step_normal("Element [%s]: Do ClickList_App()" % cls.__name__) # cls.__wait() WebDriverWait(env.driver, 10).until(lambda the_driver: the_driver.find_element(cls.by, cls.value).is_displayed()) elements = env.driver.find_elements(cls.by, cls.value) rd = random.randint(0, len(elements)-1) elements[rd].click() time.sleep(3) cls.__clearup()
def Right_Click(cls): log.step_normal("Element [%s]: Do Right_Click()" % cls.__name__) # cls.__wait() WebDriverWait(env.driver, 10).until(lambda the_driver: the_driver.find_element(cls.by, cls.value).is_displayed()) elements = env.driver.find_element(cls.by, cls.value) action = webdriver.ActionChains(env.driver) action.context_click(elements).perform() time.sleep(3) cls.__clearup()
def IsAttribute(cls, name, value, method="contain"): log.step_normal("Element [%s]: IsAttribute [%s] <%s> [%s]." % (cls.__name__, name, method, value)) cls.__wait() elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) realvalue = elements[cls.index].get_attribute(name) if method.lower() == 'equal': if value == realvalue: cls.__clearup() return True else: cls.__clearup() return False elif method.lower() == 'not equal': if not value == realvalue: cls.__clearup() return True else: cls.__clearup() return False elif method.lower() == 'contain': if value in realvalue: cls.__clearup() return True else: cls.__clearup() return False elif method.lower() == 'not contain': if not value in realvalue: cls.__clearup() return True else: cls.__clearup() return False elif method.lower() == 'in': if realvalue in value: cls.__clearup() return True else: cls.__clearup() return False else: log.step_fail("code error.") cls.__clearup()