Best Python code snippet using lettuce_webdriver_python
test_steps.py
Source:test_steps.py
...11from datetime import datetime, timedelta12from tools import *13import unittest, time, re14import requests15def find_field_by_id(browser, attribute):16 xpath = "//input[@id='%s']" % attribute17 elems = browser.find_elements_by_xpath(xpath)18 return elems[0] if elems else False19def find_textarea_by_id(browser, attribute):20 xpath = "//textarea[@id='%s']" % attribute21 elems = browser.find_elements_by_xpath(xpath)22 return elems[0] if elems else False23def find_textarea_by_class(browser, attribute):24 xpath = "//textarea[@class='%s']" % attribute25 elems = browser.find_elements_by_xpath(xpath)26 return elems[0] if elems else False27def find_cell_by_class(browser, attribute):28 xpath = "//td[@class='%s']" % attribute29 elems = browser.find_elements_by_xpath(xpath)30 return elems[0] if elems else False31def find_field_by_type(browser, attribute):32 xpath = "//input[@type='%s']" % attribute33 elems = browser.find_element_by_xpath(xpath)34 return elems35def find_input_by_name(browser, attribute):36 xpath = "//input[@name='%s']" % attribute37 elems = browser.find_element_by_xpath(xpath)38 return elems39@step('I fill in field with xpath "(.*?)" with "(.*?)"')40def fill_in_field_with_xpath(step, xpath, value):41 with AssertContextManager(step):42 elems = world.browser.find_element_by_xpath(xpath)43 elems.clear()44 elems.send_keys(value)45@step('I fill in field with id "(.*?)" with "(.*?)"')46def fill_in_field_with_id(step, field_id, value):47 with AssertContextManager(step):48 elems = find_field_by_id(world.browser, field_id)49 elems.clear()50 elems.send_keys(value)51@step('I fill in textarea with id "(.*?)" with "(.*?)"')52def fill_in_textarea_with_id(step, textarea_id, value):53 with AssertContextManager(step):54 elems = find_textarea_by_id(world.browser, textarea_id)55 elems.clear()56 elems.send_keys(value)57@step('I fill in textarea with class "(.*?)" with "(.*?)"')58def fill_in_textarea_with_class(step, textarea_class, value):59 with AssertContextManager(step):60 elems = find_textarea_by_class(world.browser, textarea_class)61 elems.clear()62 elems.send_keys(value)63@step('I fill in field with class "(.*?)" with "(.*?)"')64def fill_in_textfield_by_class(step, field_name, value):65 with AssertContextManager(step):66 text_field = find_field_by_id(world.browser, field_name)67 assert_false(step, text_field is False,'Can not find a field named "%s"' % field_name)68 text_field.clear()69 text_field.send_keys(value)70@step('I fill in field with name "(.*?)" with "(.*?)"')71def fill_in_textfield_by_name(step, name, value):72 with AssertContextManager(step):73 text_field = find_field_by_name(world.browser, name)74 assert_false(step, text_field is False,'Cannot find a field named "%s"' % name)75 text_field.clear()76 text_field.send_keys(value)77@step('I fill in field with type "(.*?)" with "(.*?)"')78def fill_in_textfield_by_type(step, field_type, value):79 with AssertContextManager(step):80 text_field = find_field_by_type(world.browser, field_type)81 assert_false(step, text_field is False,'Can not find a field with type "%s"' % field_type)82 text_field.clear()83 text_field.send_keys(value)84@step('I select "(.*?)" from the dropdown "(.*?)"')85def select_from_dropdown(step, name, dropdown):86 with AssertContextManager(step):87 Select(world.browser.find_element_by_css_selector("select")).select_by_visible_text("Memorial Exhibition")88 world.browser.find_element_by_css_selector("option[value=\"memex\"]").click()89@step('I fill in "(.*?)" with tomorrow')90def fill_in_field_with_tomorrow(step, field_name):91 now = datetime.now()92 future = datetime.now() + timedelta(days=1)93 dateFuture = future.strftime("%m/%d/%Y")94 fill_in_textfield_by_class(step, field_name, dateFuture)95@step('I click element with id "(.*?)"')96def click_element_with_id(step, element_id):97 world.browser.find_element_by_id(element_id).click()98@step('I click element with class "(.*?)"')99def click_element_with_class(step, element_class):100 world.browser.find_cell_by_class(element_class).click()101@step('I fill in search-box "(.*?)" with "(.*?)"')102def fill_in_search_box_with_text(step, search_box, text):103 searchBox = world.browser.find_element_by_xpath("//div[@class='%s']" % search_box)104 searchBox.find_element_by_css_selector("input[type=\"text\"]").clear()105 searchBox.find_element_by_css_selector("input[type=\"text\"]").send_keys(text)106@step('I click link with xpath "(.*?)"')107def click_link_with_xpath(step, xpath):108 world.browser.find_element_by_xpath(xpath).click()109@step('I see an image with source "(.*?)"')110def see_image_with_source(step, source):111 element = world.browser.find_element_by_xpath('//img[@src="%s"]' % source)112 assert_true(step, element is not None)113@step('I should see an image with source "(.*?)"')114def should_see_image_with_source(step, source):115 element = world.browser.find_element_by_xpath('//img[@src="%s"]' % source)116 assert_true(step, element is not None)117@step('I see the browser title is "(.*?)"')118def see_browser_title_is(step, title):119 element = world.browser.title120 assert_true(step, title in element)121@step('I see a link that contains the text "(.*?)" and the url "(.*?)"')122def see_include_link_text(step, link_text, link_url):123 return world.browser.find_element_by_xpath('//a[@href="%s"][contains(., %s)]' %124 (link_url, link_text))125@step('I click the link that contains the url "(.*?)"')126def click_link_url(step, link_url):127 element = world.browser.find_element_by_xpath('//a[@href="%s"]' % link_url)128 element.click()129@step('I fill in field with id "(.*?)" with a random password identified by "(.*?)"')130def fill_in_textfield_by_class(step, field_name, identifier):131 with AssertContextManager(step):132 text_field = find_field_by_id(world.browser, field_name)133 assert_false(step, text_field is False,'Can not find a field named "%s"' % field_name)134 text_field.clear()135 identifier = identifier.replace(' ', '-')136 if not hasattr(world, identifier):137 setattr(world, identifier, password_generator())138 value = getattr(world, identifier)139 text_field.clear()140 text_field.send_keys(value)141@step('I fill in field with id "(.*?)" with a random password identified by "(.*?)" and length "(.*?)"')142def fill_in_textfield_by_class(step, field_name, identifier, length):143 with AssertContextManager(step):144 text_field = find_field_by_id(world.browser, field_name)145 assert_false(step, text_field is False,'Can not find a field named "%s"' % field_name)146 text_field.clear()147 identifier = identifier.replace(' ', '-')148 if not hasattr(world, identifier):149 setattr(world, identifier, password_generator(int(length)))150 value = getattr(world, identifier)151 text_field.clear()152 text_field.send_keys(value)153@step('I fill in field with id "(.*?)" with random text identified by "(.*?)" and length "(.*?)"')154def fill_in_textfield_by_class(step, field_name, identifier, length):155 with AssertContextManager(step):156 text_field = find_textarea_by_id(world.browser, field_name)157 assert_false(step, text_field is False,'Can not find a field named "%s"' % field_name)158 text_field.clear()159 identifier = identifier.replace(' ', '-')160 if not hasattr(world, identifier):161 setattr(world, identifier, text_generator(int(length)))162 value = getattr(world, identifier)163 text_field.clear()164 text_field.send_keys(value)165@step('I fill in field with id "(.*?)" with a random name identified by "(.*?)"')166def fill_in_textfield_by_class(step, field_name, identifier):167 with AssertContextManager(step):168 text_field = find_field_by_id(world.browser, field_name)169 assert_false(step, text_field is False,'Can not find a field named "%s"' % field_name)170 text_field.clear()171 identifier = identifier.replace(' ', '-')172 if not hasattr(world, identifier):173 setattr(world, identifier, name_generator())174 value = getattr(world, identifier)175 text_field.clear()176 text_field.send_keys(value)177@step('I fill in field with id "(.*?)" with a random name identified by "(.*?)" and length "(.*?)"')178def fill_in_textfield_by_class(step, field_name, identifier, length):179 with AssertContextManager(step):180 text_field = find_field_by_id(world.browser, field_name)181 assert_false(step, text_field is False,'Can not find a field named "%s"' % field_name)182 text_field.clear()183 identifier = identifier.replace(' ', '-')184 if not hasattr(world, identifier):185 setattr(world, identifier, name_generator(int(length)))186 value = getattr(world, identifier)187 text_field.clear()188 text_field.send_keys(value)189@step('I fill in field with id "(.*?)" with a random email identified by "(.*?)"')190def fill_in_textfield_by_class(step, field_name, identifier):191 with AssertContextManager(step):192 text_field = find_field_by_id(world.browser, field_name)193 assert_false(step, text_field is False,'Can not find a field named "%s"' % field_name)194 text_field.clear()195 identifier = identifier.replace(' ', '-')196 if not hasattr(world, identifier):197 setattr(world, identifier, email_generator())198 value = getattr(world, identifier)199 text_field.clear()200 text_field.send_keys(value)201@step('I fill in field with id "(.*?)" with a random email identified by "(.*?)" and length "(.*?)"')202def fill_in_textfield_by_class(step, field_name, identifier, length):203 with AssertContextManager(step):204 text_field = find_field_by_id(world.browser, field_name)205 assert_false(step, text_field is False,'Can not find a field named "%s"' % field_name)206 text_field.clear()207 identifier = identifier.replace(' ', '-')208 if not hasattr(world, identifier):209 setattr(world, identifier, email_generator(int(length)))210 value = getattr(world, identifier)211 text_field.clear()212 text_field.send_keys(value)213@step('I fill in field with id "(.*?)" with random unicode identified by "(.*?)"')214def fill_in_textfield_by_class(step, field_name, identifier):215 with AssertContextManager(step):216 text_field = find_field_by_id(world.browser, field_name)217 assert_false(step, text_field is False,'Can not find a field named "%s"' % field_name)218 text_field.clear()219 identifier = identifier.replace(' ', '-')220 if not hasattr(world, identifier):221 setattr(world, identifier, unicode_generator())222 value = getattr(world, identifier)223 text_field.clear()224 text_field.send_keys(value)225@step('I fill in textarea with id "(.*?)" with random unicode identified by "(.*?)"')226def fill_in_textfield_by_class(step, field_name, identifier):227 with AssertContextManager(step):228 text_field = find_textarea_by_id(world.browser, field_name)229 assert_false(step, text_field is False,'Can not find a field named "%s"' % field_name)230 text_field.clear()231 identifier = identifier.replace(' ', '-')232 if not hasattr(world, identifier):233 setattr(world, identifier, unicode_generator())234 value = getattr(world, identifier)235 text_field.clear()236 text_field.send_keys(value)237@step('I fill in field with id "(.*?)" with a random unicode email identified by "(.*?)"')238def fill_in_textfield_by_class(step, field_name, identifier):239 with AssertContextManager(step):240 text_field = find_field_by_id(world.browser, field_name)241 assert_false(step, text_field is False,'Can not find a field named "%s"' % field_name)242 text_field.clear()243 identifier = identifier.replace(' ', '-')244 if not hasattr(world, identifier):245 setattr(world, identifier, unicode_email_generator())246 value = getattr(world, identifier)247 text_field.clear()248 text_field.send_keys(value)249@step('I see the text identified by "(.*?)"')250def see(step, identifier):251 with AssertContextManager(step):252 identifier = identifier.replace(' ', '-')253 assert_true(step, hasattr(world, identifier))254 text = getattr(world, identifier)...
utils.py
Source:utils.py
...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!!