def __wait(cls): t = 0 while t < 5: t += 1 try: print(u"Element: %s " % cls.value) elements = env.driver.find_elements(cls.by, cls.value) # print("__wait_elements= %s " % elements) print("There are [%s] Elements" % len(elements)) print("------------------------------------------------------------") except NoSuchElementException: log.step_normal("Element [%s]: NoSuchElementException." % cls.__name__) elements = [] # print(u"[Find Element Fail !] => ") if len(elements) == 0: # time.sleep(2) log.step_normal("Element [%s] Find Fail, Wait 2 Seconds, By [%s :: %s :: %s]" % (cls.__name__, cls.by, cls.value, cls.index)) else: if len(elements) > 1: log.step_normal("Element [%s]: There are [%s] Elements!" % (cls.__name__, len(elements))) # print(u"[Find Element Success !] => ") break if len(elements) < cls.index + 1: log.step_fail("Element [%s]: Element Index Issue! There are [%s] Elements! Index=[%s]" % (cls.__name__, len(elements), cls.index))
def __wait(cls): t = 0 while t < 5: t += 1 try: print(u"Element: %s " % cls.value) elements = env.driver.find_elements(cls.by, cls.value) # print("__wait_elements= %s " % elements) print("There are [%s] Elements" % len(elements)) print( "------------------------------------------------------------" ) except NoSuchElementException: log.step_normal("Element [%s]: NoSuchElementException." % cls.__name__) elements = [] # print(u"[Find Element Fail !] => ") if len(elements) == 0: # time.sleep(2) log.step_normal( "Element [%s] Find Fail, Wait 2 Seconds, By [%s :: %s :: %s]" % (cls.__name__, cls.by, cls.value, cls.index)) else: if len(elements) > 1: log.step_normal("Element [%s]: There are [%s] Elements!" % (cls.__name__, len(elements))) # print(u"[Find Element Success !] => ") break if len(elements) < cls.index + 1: log.step_fail( "Element [%s]: Element Index Issue! There are [%s] Elements! Index=[%s]" % (cls.__name__, len(elements), cls.index))
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 __wait(cls): t = 0 while t < 300: 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__) except UnexpectedAlertPresentException: log.step_warning("Element [%s]: UnexpectedAlertPresentException." % cls.__name__) if len(elements) == 0: time.sleep(0.5) log.step_normal("Element [%s]: Wait 0.5 second, By [%s :: %s :: %s]" % (cls.__name__, cls.by, cls.value, cls.index)) else: if len(elements) > 1: log.step_normal("Element [%s]: There are [%s] Elements!" % (cls.__name__, len(elements))) break if len(elements) < cls.index + 1: log.step_fail("Element [%s]: Element Index Issue! There are [%s] Elements! Index=[%s]" % (cls.__name__, len(elements), cls.index))
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 __wait(cls): t = 0 while t < 60: t = t + 1 try: elements = env.BROWSER.find_elements(cls.by, cls.value) except NoSuchElementException: log.step_normal("Element [%s]: NoSuchElementException." % cls.__name__) elements = [] if len(elements) == 0: time.sleep(3) log.step_normal( "Element [%s]: Wait 3 Seconds, By [%s :: %s :: %s]" % (cls.__name__, cls.by, cls.value, cls.index)) else: if len(elements) > 1: log.step_normal("Element [%s]: There are [%s] Elements!" % (cls.__name__, len(elements))) break if len(elements) < cls.index + 1: log.step_fail( "Element [%s]: Element Index Issue! There are [%s] Elements! Index=[%s]" % (cls.__name__, len(elements), cls.index))
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 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 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()
def VerifyInnerHTMLContains(cls, contain_content): if contain_content == "": return log.step_normal("Element [%s]: VerifyInnerHTMLContains [%s]." % (cls.__name__, contain_content)) cls.__wait() elements = env.BROWSER.find_elements(cls.by, cls.value) inner_html = elements[cls.index].get_attribute('innerHTML') if contain_content in inner_html: log.step_pass("Real inner_hmtl=[%s]" % inner_html) else: log.step_fail("Real inner_hmtl=[%s]" % inner_html) cls.__clearup()
def VerifyAttribute(cls, attr, contain_content): if contain_content == "": return log.step_normal("Element [%s]: Verify Attribute [%s] == [%s]." % (cls.__name__, attr, contain_content)) cls.__wait() elements = env.BROWSER.find_elements(cls.by, cls.value) attr_value = elements[cls.index].get_attribute(attr) if contain_content == attr_value: log.step_pass("Real attr_value=[%s]" % attr_value) else: log.step_fail("Real attr_value=[%s]" % attr_value) cls.__clearup()
def VerifyAttribute(cls, attr, contain_content): if contain_content == "": return log.step_normal("Element [%s]: Verify Attribute [%s] == [%s]." % (cls.__name__, attr, contain_content)) # 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) attr_value = elements[cls.index].get_attribute(attr) if contain_content == attr_value: log.step_pass("Real attr_value=[%s]" % attr_value) else: log.step_fail("Real attr_value=[%s]" % attr_value) cls.__clearup()
def VerifyInnerHTMLNotContains(cls, contain_content): if contain_content == "": return log.step_normal("Element [%s]: VerifyInnerHTMLNotContains [%s]." % (cls.__name__, contain_content)) # 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) inner_html = elements[cls.index].get_attribute('innerHTML') if contain_content in inner_html: log.step_fail("Real inner_hmtl=[%s]" % inner_html) else: log.step_pass("Real inner_hmtl=[%s]" % inner_html) cls.__clearup()
def VerifyEnabled(cls, trueOrfalse): log.step_normal(u"Element [%s]: Verify Enabled = [%s]" % (cls.__name__, trueOrfalse)) cls.__wait() elements = env.BROWSER.find_elements(cls.by, cls.value) if elements[cls.index].is_enabled(): if trueOrfalse == True: log.step_pass("Pass") else: log.step_fail("Fail") else: if trueOrfalse == True: log.step_fail("Fail") else: log.step_pass("Pass") cls.__clearup()
def Select(cls, value): log.step_normal("Element [%s]: Select [%s]." % (cls.__name__, value)) cls.__wait() elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) is_selected = False #### select ################ if elements[cls.index].tag_name == "select": options = elements[cls.index].find_elements_by_tag_name('option') for option in options: # log.step_normal("Element [%s]: option [%s]" % (cls.__name__, option.text)) if option.text == value: option.click() is_selected = True break #### ul ################ elif elements[cls.index].tag_name == "ul": lis = elements[cls.index].find_elements_by_tag_name('li') for li in lis: # log.step_normal("Element [%s]: li [%s]" % (cls.__name__, li.text)) if li.text == value: li.click() is_selected = True break #### NOT Supported ################ else: log.step_fail("Element [%s]: Tag [%s] NOT support [Select] method" % (cls.__name__, elements[cls.index].tag_name)) if is_selected is False: log.step_fail("No item selected!") cls.__clearup()
def VerifyInnerHTMLNotContains(cls, contain_content): if contain_content == "": return log.step_normal("Element [%s]: VerifyInnerHTMLNotContains [%s]." % (cls.__name__, contain_content)) # 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) inner_html = elements[cls.index].get_attribute('innerHTML') if contain_content in inner_html: log.step_fail("Real inner_hmtl=[%s]" % inner_html) else: log.step_pass("Real inner_hmtl=[%s]" % inner_html) cls.__clearup()
def VerifyVisible(cls, trueORfalse): log.step_normal("Element [%s]: Verify Visible = [%s]." % (cls.__name__, trueORfalse)) cls.__wait() elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) log.step_normal("Element [%s]: Count = [%s]" % (cls.__name__, len(elements))) if elements[cls.index].is_displayed(): if trueORfalse == True: log.step_pass("Visible!") else: log.step_fail("Visible!") else: if trueORfalse == False: log.step_pass("Not Visible!") else: log.step_fail("Not Visible!") cls.__clearup()
def VerifyEnabled(cls, trueOrfalse): log.step_normal(u"Element [%s]: Verify Enabled = [%s]" % (cls.__name__, trueOrfalse)) # 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].is_enabled(): if trueOrfalse is True: log.step_pass("Pass") else: log.step_fail("Fail") else: if trueOrfalse is True: log.step_fail("Fail") else: log.step_pass("Pass") cls.__clearup()
def VerifyAttribute(cls, attr, contain_content): if contain_content == "": return log.step_normal("Element [%s]: Verify Attribute [%s] == [%s]." % (cls.__name__, attr, contain_content)) # 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) attr_value = elements[cls.index].get_attribute(attr) if contain_content == attr_value: log.step_pass("Real attr_value=[%s]" % attr_value) else: log.step_fail("Real attr_value=[%s]" % attr_value) cls.__clearup()
def VerifyInnerHTMLContains(cls, contain_content): log.step_normal("Element [%s]: VerifyInnerHTMLContains [%s]." % (cls.__name__, str(contain_content))) cls.__wait() elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) inner_html = elements[cls.index].get_attribute('innerHTML') if isinstance(contain_content, list): for content in contain_content: if content in inner_html: log.step_pass("Real inner_hmtl=[%s]" % inner_html) else: log.step_fail("Real inner_hmtl=[%s]" % inner_html) else: if contain_content in inner_html: log.step_pass("Real inner_hmtl=[%s]" % inner_html) else: log.step_fail("Real inner_hmtl=[%s]" % inner_html) cls.__clearup()
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 VerifyEnabled(cls, trueOrfalse): log.step_normal(u"Element [%s]: Verify Enabled = [%s]" % (cls.__name__, trueOrfalse)) # 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].is_enabled(): if trueOrfalse is True: log.step_pass("Pass") else: log.step_fail("Fail") else: if trueOrfalse is True: log.step_fail("Fail") else: log.step_pass("Pass") cls.__clearup()
def Select(cls, value): if value == "": return log.step_normal("Element [%s]: Do Select [%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) # 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() time.sleep(3) 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() time.sleep(3) 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 __wait(cls): t = 0 while t < 60: t = t + 1 try: elements = env.BROWSER.find_elements(cls.by, cls.value) except NoSuchElementException: log.step_normal("Element [%s]: NoSuchElementException." % cls.__name__) elements = [] if len(elements) == 0: time.sleep(3) log.step_normal("Element [%s]: Wait 3 Seconds, By [%s :: %s :: %s]" % (cls.__name__, cls.by, cls.value, cls.index)) else: if len(elements) > 1: log.step_normal("Element [%s]: There are [%s] Elements!" % (cls.__name__, len(elements))) break if len(elements) < cls.index + 1: log.step_fail("Element [%s]: Element Index Issue! There are [%s] Elements! Index=[%s]" % (cls.__name__, len(elements), cls.index))
def VerifyExistence(cls, trueORfalse): log.step_normal("Element [%s]: Verify Existence = [%s]." % (cls.__name__, trueORfalse)) if trueORfalse == True: cls.__wait_for_appearing() else: cls.__wait_for_disappearing() elements = env.threadlocal.BROWSER.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 == True: log.step_pass("Exist!") else: log.step_fail("Exist!") else: if trueORfalse == 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() 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) # 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() time.sleep(3) 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() time.sleep(3) 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 SelectByOrder(cls, order): if order == "": return log.step_normal("Element [%s]: Do Select by Order [%s]" % (cls.__name__, order)) order = int(order) # 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) # ul if elements[cls.index].tag_name == "ul": lis = elements[cls.index].find_elements_by_tag_name('li') if order > 0: # Wait and try more times if NO item found. t = 0 while len(lis) == 0: lis = elements[cls.index].find_elements_by_tag_name('li') time.sleep(3) t += 1 log.step_normal("Element [%s]: Wait 3 Seconds for [li]" % cls.__name__) if t == 8 and len(lis) == 0: log.step_fail("Element [%s]: List Count = [%s]." % (cls.__name__, len(lis))) return log.step_normal("Element [%s]: List Count = [%s]." % (cls.__name__, len(lis))) if order > len(lis): log.step_normal("Element [%s]: Not so many lists. [%s]" % (cls.__name__, len(lis))) else: log.step_normal("Element [%s]: Do Click [%s]" % (cls.__name__, order)) action = webdriver.ActionChains(env.driver) action.click(lis[order-1]) action.perform() time.sleep(3) else: log.step_fail("Order = [%s], Value Error." % order) # select if elements[cls.index].tag_name == "select": options = elements[cls.index].find_elements_by_tag_name('option') if order > 0: # Wait and try more times if NO item found. t = 0 while len(options) == 0: options = elements[cls.index].find_elements_by_tag_name('option') time.sleep(3) t += 1 log.step_normal("Element [%s]: Wait 3 Seconds for [option]" % cls.__name__) if t == 8 and len(options) == 0: log.step_fail("Element [%s]: options Count = [%s]." % (cls.__name__, len(options))) return log.step_normal("Element [%s]: options Count = [%s]." % (cls.__name__, len(options))) if order > len(options): log.step_normal("Element [%s]: Not so many options. [%s]" % (cls.__name__, len(options))) else: log.step_normal("Element [%s]: Do Click [%s]" % (cls.__name__, order)) action = webdriver.ActionChains(env.driver) action.click(options[order-1]) action.perform() time.sleep(3) else: log.step_fail("Order = [%s], Value Error." % order) cls.__clearup()
def SelectByOrder(cls, order): if order == "": return log.step_normal("Element [%s]: Do Select by Order [%s]" % (cls.__name__, order)) order = int(order) # 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) # ul if elements[cls.index].tag_name == "ul": lis = elements[cls.index].find_elements_by_tag_name('li') if order > 0: # Wait and try more times if NO item found. t = 0 while len(lis) == 0: lis = elements[cls.index].find_elements_by_tag_name('li') time.sleep(3) t += 1 log.step_normal("Element [%s]: Wait 3 Seconds for [li]" % cls.__name__) if t == 8 and len(lis) == 0: log.step_fail("Element [%s]: List Count = [%s]." % (cls.__name__, len(lis))) return log.step_normal("Element [%s]: List Count = [%s]." % (cls.__name__, len(lis))) if order > len(lis): log.step_normal("Element [%s]: Not so many lists. [%s]" % (cls.__name__, len(lis))) else: log.step_normal("Element [%s]: Do Click [%s]" % (cls.__name__, order)) action = webdriver.ActionChains(env.driver) action.click(lis[order - 1]) action.perform() time.sleep(3) else: log.step_fail("Order = [%s], Value Error." % order) # select if elements[cls.index].tag_name == "select": options = elements[cls.index].find_elements_by_tag_name('option') if order > 0: # Wait and try more times if NO item found. t = 0 while len(options) == 0: options = elements[cls.index].find_elements_by_tag_name( 'option') time.sleep(3) t += 1 log.step_normal( "Element [%s]: Wait 3 Seconds for [option]" % cls.__name__) if t == 8 and len(options) == 0: log.step_fail("Element [%s]: options Count = [%s]." % (cls.__name__, len(options))) return log.step_normal("Element [%s]: options Count = [%s]." % (cls.__name__, len(options))) if order > len(options): log.step_normal("Element [%s]: Not so many options. [%s]" % (cls.__name__, len(options))) else: log.step_normal("Element [%s]: Do Click [%s]" % (cls.__name__, order)) action = webdriver.ActionChains(env.driver) action.click(options[order - 1]) action.perform() time.sleep(3) else: log.step_fail("Order = [%s], Value Error." % order) cls.__clearup()
def SelectByOrder(cls, order): if order == "": return log.step_normal("Element [%s]: Do Select by Order [%s]" % (cls.__name__, order)) order = int(order) cls.__wait() elements = env.BROWSER.find_elements(cls.by, cls.value) #### ul ################ if elements[cls.index].tag_name == "ul": lis = elements[cls.index].find_elements_by_tag_name('li') if order > 0: ### Wait and try more times if NO item found. ### t = 0 while (len(lis) == 0): lis = elements[cls.index].find_elements_by_tag_name('li') time.sleep(3) t = t + 1 log.step_normal("Element [%s]: Wait 3 Seconds for [li]" % cls.__name__) if t == 8 and len(lis) == 0: log.step_fail("Element [%s]: List Count = [%s]." % (cls.__name__, len(lis))) return log.step_normal("Element [%s]: List Count = [%s]." % (cls.__name__, len(lis))) if (order > len(lis)): log.step_normal("Element [%s]: Not so many lists. [%s]" % (cls.__name__, len(lis))) else: log.step_normal("Element [%s]: Do Click [%s]" % (cls.__name__, order)) action = webdriver.ActionChains(env.BROWSER) action.click(lis[order - 1]) action.perform() else: log.step_fail("Order = [%s], Value Error." % order) #### select ################ if elements[cls.index].tag_name == "select": options = elements[cls.index].find_elements_by_tag_name('option') if order > 0: ### Wait and try more times if NO item found. ### t = 0 while (len(options) == 0): options = elements[cls.index].find_elements_by_tag_name( 'option') time.sleep(3) t = t + 1 log.step_normal( "Element [%s]: Wait 3 Seconds for [option]" % cls.__name__) if t == 8 and len(lis) == 0: log.step_fail("Element [%s]: options Count = [%s]." % (cls.__name__, len(options))) return log.step_normal("Element [%s]: options Count = [%s]." % (cls.__name__, len(options))) if (order > len(options)): log.step_normal("Element [%s]: Not so many options. [%s]" % (cls.__name__, len(options))) else: log.step_normal("Element [%s]: Do Click [%s]" % (cls.__name__, order)) action = webdriver.ActionChains(env.BROWSER) action.click(options[order - 1]) action.perform() else: log.step_fail("Order = [%s], Value Error." % order) cls.__clearup()
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 SelectByOrder(cls, order): log.step_normal("Element [%s]: Select by Order [%s]" % (cls.__name__, order)) order = int(order) cls.__wait() elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) #### ul ################ if elements[cls.index].tag_name == "ul": lis = elements[cls.index].find_elements_by_tag_name('li') if order > 0: ### Wait and try more times if NO item found. ### t = 0 while (len(lis) == 0): lis = elements[cls.index].find_elements_by_tag_name('li') time.sleep(3) t = t + 1 log.step_normal("Element [%s]: Wait 3 Seconds for [li]" % cls.__name__) if t == 20 and len(lis) == 0: log.step_fail("Element [%s]: List Count = [%s]." % (cls.__name__, len(lis))) return log.step_normal("Element [%s]: List Count = [%s]." % (cls.__name__, len(lis))) if (order > len(lis)): log.step_fail("Element [%s]: Not so many lists. [%s]" % (cls.__name__, len(lis))) else: log.step_normal("Element [%s]: Do Click [%s]" % (cls.__name__, order)) action = webdriver.ActionChains(env.threadlocal.BROWSER) # Added to avoid error: "Element is no longer attached to the DOM" elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) lis = elements[cls.index].find_elements_by_tag_name('li') action.click(lis[order-1]) action.perform() else: log.step_fail("Order = [%s], Value Error." % order) #### select ################ #### if elements[cls.index].tag_name == "select": else: options = elements[cls.index].find_elements_by_tag_name('option') if order > 0: ### Wait and try more times if NO item found. ### t = 0 while (len(options) == 0): options = elements[cls.index].find_elements_by_tag_name('option') time.sleep(3) t = t + 1 log.step_normal("Element [%s]: Wait 3 Seconds for [option]" % cls.__name__) if t == 20 and len(lis) == 0: log.step_fail("Element [%s]: options Count = [%s]." % (cls.__name__, len(options))) return log.step_normal("Element [%s]: options Count = [%s]." % (cls.__name__, len(options))) if (order > len(options)): log.step_fail("Element [%s]: Not so many options. [%s]" % (cls.__name__, len(options))) else: log.step_normal("Element [%s]: Do Click [%s]" % (cls.__name__, order)) action = webdriver.ActionChains(env.threadlocal.BROWSER) action.click(options[order-1]) action.perform() else: log.step_fail("Order = [%s], Value Error." % order) cls.__clearup()
def WaitForAttribute(cls, name, value, method="equal"): ''' Example: NewClaim.Dates.ReminderDate.WaitForAttribute('ng-model', 'hello', method='equal') NewClaim.Dates.ReminderDate.WaitForAttribute('ng-model', 'hello', method='contain') NewClaim.Dates.ReminderDate.WaitForAttribute('ng-model', 'hello', method='not contain') NewClaim.Dates.ReminderDate.WaitForAttribute('ng-model', 'hello', method='in') NewClaim.Dates.ReminderDate.WaitForAttribute('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]: WaitForAttribute [%s] <%s> [%s]." % (cls.__name__, name, method, value)) i = 0 while True: 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.lower() == realvalue.lower(): log.step_normal("Yes! real value=[%s]" % realvalue) break else: log.step_normal("No! real value=[%s]" % realvalue) elif method.lower() == 'contain': if value.lower() in realvalue.lower(): log.step_normal("Yes! real value=[%s]" % realvalue[:150]) break else: log.step_normal("No! real value=[%s]" % realvalue[:150]) elif method.lower() == 'not contain': if value.lower() in realvalue.lower(): log.step_normal("Yes! real value=[%s]" % realvalue[:150]) else: log.step_normal("No! real value=[%s]" % realvalue[:150]) break elif method.lower() == 'in': if realvalue.lower() in value.lower(): log.step_normal("Yes! real value=[%s]" % realvalue[:150]) break else: log.step_normal("No! real value=[%s]" % realvalue[:150]) elif method.lower() == 'not equal': if value.lower() == realvalue.lower(): log.step_normal("No! real value=[%s]" % realvalue) else: log.step_normal("Yes! real value=[%s]" % realvalue) break else: log.step_fail("code error.") i = i + 1 if i > 90: log.step_fail("Not Found Expected Value! real value=[%s]" % realvalue) break time.sleep(1) cls.__clearup()