Best Python code snippet using lettuce_webdriver_python
util.py
Source:util.py
...20 nose_assert_true(exp)21def assert_false(step, exp, msg=None):22 with AssertContextManager(step):23 nose_assert_false(exp, msg)24def element_id_by_label(browser, label):25 """Return the id of a label's for attribute"""26 for_id = browser.find_elements_by_xpath('//label[contains(., "%s")]' % label)27 if not for_id:28 return False29 return for_id[0].get_attribute('for')30## Field helper functions to locate select, textarea, and the other31## types of input fields (text, checkbox, radio)32def field_xpath(field, attribute):33 if field in ['select', 'textarea']:34 return './/%s[@%s="%%s"]' % (field, attribute)35 elif field == 'button':36 if attribute == 'value':37 return './/%s[contains(., "%%s")]' % (field, )38 else:39 return './/%s[@%s="%%s"]' % (field, attribute)40 elif field == 'option':41 return './/%s[@%s="%%s"]' % (field, attribute)42 else:43 return './/input[@%s="%%s"][@type="%s"]' % (attribute, field)44def find_button(browser, value):45 return find_field_with_value(browser, 'submit', value) or \46 find_field_with_value(browser, 'reset', value) or \47 find_field_with_value(browser, 'button', value) or \48 find_field_with_value(browser, 'image', value)49def find_field_with_value(browser, field, value):50 return find_field_by_id(browser, field, value) or \51 find_field_by_name(browser, field, value) or \52 find_field_by_value(browser, field, value)53def find_option(browser, select_name, option_name):54 # First, locate the select55 select_box = find_field(browser, 'select', select_name)56 assert select_box57 58 # Now locate the option59 option_box = find_field(select_box, 'option', option_name)60 if not option_box:61 # Locate by contents62 option_box = select_box.find_element_by_xpath('./option[contains(., "%s")]' % option_name)63 return option_box64def find_field(browser, field, value):65 """Locate an input field of a given value66 67 This first looks for the value as the id of the element, then68 the name of the element, then a label for the element.69 70 """71 return find_field_by_id(browser, field, value) or \72 find_field_by_name(browser, field, value) or \73 find_field_by_label(browser, field, value)74def find_field_by_id(browser, field, id):75 xpath = field_xpath(field, 'id')76 elems = browser.find_elements_by_xpath(xpath % id)77 return elems[0] if elems else False78def find_field_by_name(browser, field, name):79 xpath = field_xpath(field, 'name')80 elems = browser.find_elements_by_xpath(xpath % name)81 return elems[0] if elems else False82def find_field_by_value(browser, field, name):83 xpath = field_xpath(field, 'value')84 elems = browser.find_elements_by_xpath(xpath % name)85 return elems[0] if elems else False86def find_field_by_label(browser, field, label):87 """Locate the control input that has a label pointing to it88 89 This will first locate the label element that has a label of the given90 name. It then pulls the id out of the 'for' attribute, and uses it to91 locate the element by its id.92 93 """94 for_id = element_id_by_label(browser, label)95 if not for_id:96 return False...
utils.py
Source:utils.py
...20 nose_assert_true(exp)21def assert_false(step, exp, msg=None):22 with AssertContextManager(step):23 nose_assert_false(exp, msg)24def element_id_by_label(browser, label):25 """Return the id of a label's for attribute"""26 for_id = browser.find_elements_by_xpath('//label[contains(., "%s")]' % label)27 if not for_id:28 return False29 return for_id[0].get_attribute('for')30## Field helper functions to locate select, textarea, and the other31## types of input fields (text, checkbox, radio)32def field_xpath(field, attribute):33 if field in ['select', 'textarea']:34 return './/%s[@%s="%%s"]' % (field, attribute)35 elif field == 'button':36 if attribute == 'value':37 return './/%s[contains(., "%%s")]' % (field, )38 else:39 return './/%s[@%s="%%s"]' % (field, attribute)40 elif field == 'option':41 return './/%s[@%s="%%s"]' % (field, attribute)42 else:43 return './/input[@%s="%%s"][@type="%s"]' % (attribute, field)44def find_button(browser, value):45 return find_field_with_value(browser, 'submit', value) or \46 find_field_with_value(browser, 'reset', value) or \47 find_field_with_value(browser, 'button', value) or \48 find_field_with_value(browser, 'image', value)49def find_field_with_value(browser, field, value):50 return find_field_by_id(browser, field, value) or \51 find_field_by_name(browser, field, value) or \52 find_field_by_value(browser, field, value)53def find_option(browser, select_name, option_name):54 # First, locate the select55 select_box = find_field(browser, 'select', select_name)56 assert select_box57 58 # Now locate the option59 option_box = find_field(select_box, 'option', option_name)60 if not option_box:61 # Locate by contents62 option_box = select_box.find_element_by_xpath('./option[contains(., "%s")]' % option_name)63 return option_box64def find_field(browser, field, value):65 """Locate an input field of a given value66 67 This first looks for the value as the id of the element, then68 the name of the element, then a label for the element.69 70 """71 return find_field_by_id(browser, field, value) or \72 find_field_by_name(browser, field, value) or \73 find_field_by_label(browser, field, value)74def find_field_by_id(browser, field, id):75 xpath = field_xpath(field, 'id')76 elems = browser.find_elements_by_xpath(xpath % id)77 return elems[0] if elems else False78def find_field_by_name(browser, field, name):79 xpath = field_xpath(field, 'name')80 elems = browser.find_elements_by_xpath(xpath % name)81 return elems[0] if elems else False82def find_field_by_value(browser, field, name):83 xpath = field_xpath(field, 'value')84 elems = browser.find_elements_by_xpath(xpath % name)85 return elems[0] if elems else False86def find_field_by_label(browser, field, label):87 """Locate the control input that has a label pointing to it88 89 This will first locate the label element that has a label of the given90 name. It then pulls the id out of the 'for' attribute, and uses it to91 locate the element by its id.92 93 """94 for_id = element_id_by_label(browser, label)95 if not for_id:96 return False...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!