Best Python code snippet using SeleniumBase
main.py
Source:main.py
...111 """Plant selected vegetable."""112 log('Planting...', 'white')113 to_plant = css('.plant-hint')114 for place in to_plant:115 js_click(place)116 time.sleep(0.5)117 log('[DONE]')118def total_harvest_able():119 """Total harvest capacity."""120 return len(css('.harvest'))121def collect_plant():122 """Check collectible plant."""123 all_collectible = css('.harvest')124 log('Places founded: %s. \nCollecting...' % len(all_collectible), 'white')125 for idx, collect in enumerate(all_collectible):126 js_click(collect)127 time.sleep(0.5)128def save():129 """Save harvest."""130 save_btn = xpath('//*[@id="timer"]/img')131 js_click(save_btn)132 js_click(xpath('//*[@id="save-error-buttons"]/div'))133 main_window_handle = None134 while not main_window_handle:135 main_window_handle = DRIVER.current_window_handle136 signin_window_handle = None137 while not signin_window_handle:138 for handle in DRIVER.window_handles:139 if handle != main_window_handle:140 signin_window_handle = handle141 break142 DRIVER.switch_to.window(signin_window_handle)143 metamask_btn = xpath('//*[@id="app-content"]/div/div[2]/div/div[4]/div[3]/footer/button[2]')144 metamask_btn.click()145 DRIVER.switch_to.window(main_window_handle)146def select_basket():147 """Select fruit on basket."""148 js_click(xpath('//*[@id="basket"]/img[2]'))149 time.sleep(2)150 js_click(xpath(plants_type[SELECTED_PLANT]))151 time.sleep(2)152 js_click(xpath('/html/body/div[3]/div/div/div/div/div/div/img'))153 time.sleep(2)154def count_progress_plants():155 """Count all plants in progress."""156 progress_plants = css('span.progress-text')157 for idx, plant in enumerate(progress_plants):158 log('Plants slot %s: %s left.' % (idx + 1, plant.text), 'white')159 return len(progress_plants)160def close_unused_tabs():161 """Close initial metamask plugin tab."""162 main_window_handle = None163 while not main_window_handle:164 main_window_handle = DRIVER.current_window_handle165 for handle in DRIVER.window_handles:166 if handle != main_window_handle:167 DRIVER.switch_to.window(handle)168 DRIVER.close()169 break170 DRIVER.switch_to.window(main_window_handle)171def is_loading():172 """Show Fruit Farm loading modal."""173 try:174 return 'loading' in xpath('//*[@id="welcome"]/h1', False).text.lower()175 except:176 return None177def is_saving():178 """Check if is saving your farm."""179 try:180 return 'saving' in xpath('//*[@id="saving"]/h4').text.lower()181 except:182 return None183def js_click(element):184 """Execute a JS click on browser."""185 DRIVER.execute_script("arguments[0].click();", element)186def xpath(path, raise_error=True):187 """Find by xpath."""188 found_element = False189 tries = 5190 while not found_element and tries > 0:191 try:192 found_element = DRIVER.find_element(By.XPATH, path)193 return found_element194 except common.exceptions.NoSuchElementException:195 tries -= 1196 time.sleep(1)197 if not raise_error:...
begin.py
Source:begin.py
...35 36 def switch_window(self, window_number):37 self.driver.switch_to.window(self.driver.window_handles[window_number])38 39 def js_click(self, element):40 self.driver.execute_script('arguments[0].click();', element)41 42 def eadas_setting(self):43 # eadasã®mapãã¼ã¸ã«ç§»å44 self.driver.find_element_by_id('ui-id-2').click()45 self.driver.find_element_by_id('btn_seemap').click()46 access_btn = self.driver.find_element_by_class_name('ui-dialog-buttonset')47 access_btn.find_elements_by_tag_name('button')[0].click()48 49 # ãã¼ã¸é·ç§» & change driver.current_url50 self.driver.close()51 self.switch_window(-1)52 53 # bookmark click54 WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.ID, 'button-1088')))55 self.driver.find_element_by_id('button-1088').click()56 information_image_list = self.driver.find_elements_by_class_name('x-grid-item-container')57 self.driver.find_element_by_id('tool-1090').click()58 # add laws59 book_mark_laws = information_image_list[1].find_elements_by_tag_name('table')60 61 # è¦ç´ ãåãforloopã¨clickããloopãåããªãã¨é«ç¢ºçã§ã¨ã©ã¼ãèµ·ãã62 btns = []63 for i in range(len(book_mark_laws)):64 btn_wrapper = book_mark_laws[i].find_elements_by_tag_name('td')[2]65 btn = btn_wrapper.find_element_by_class_name('x-action-col-0')66 btns.append(btn)67 for btn in btns:68 try:69 self.js_click(btn)70 except StaleElementReferenceException:71 self.driver.execute_script('arguments[0].click();', btn)72 73 # close bookmark popup74 WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.ID, 'tool-1102')))75 self.js_click(self.driver.find_element_by_id('tool-1102'))76 # change background image77 self.js_click(self.driver.find_element_by_id('button-1031'))78 self.js_click(self.driver.find_element_by_id('button-1061-btnWrap'))79 self.js_click(self.driver.find_element_by_id('tool-1264'))80 81 def web_page_open(self, url, window_number):82 self.driver.get(url)83 84 if url != self.EADAS_URL:85 self.driver.execute_script("window.open()")86 87 if url == self.GOOGLE_MAPS_URL:88 self.google_map_focus()89 90 if url == self.EADAS_URL:91 self.eadas_setting()92 else:93 self.switch_window(window_number)...
Steps.py
Source:Steps.py
...21 Selenium.wait_element(self, locator)22 Selenium.send_key_text(self, locator, text)23 @step("Hacer click sobre el botón (.*)")24 def step_impl(self, locator):25 Selenium.js_click(self, locator)26 @then('Verificar el error (.*) con el texto "(.*)"')27 def step_impl(self, locator, text):28 Selenium.assert_text(self, locator, text)29 @step("Informar el valor (.*) en el campo (.*)")30 def step_impl(self, text, locator):31 Selenium.send_key_text(self, locator, text)32 @step("Seleccionar el valor (.*) en el campo (.*)")33 def step_impl(self, text, entity):34 Selenium.select_by_text(self, entity, text)35 @step("Seleccionar la opción (.*) en el campo (.*)")36 def step_impl(self, value, entity):37 Selenium.select_by_value(self, entity, value)38 @step("Hacer click sobre la opción (.*)")39 def step_impl(self, locator):40 Selenium.js_click(self, locator)41 @then("Cerrar la APP")42 def step_impl(self):43 Selenium.tearDown(self)44 @given("Eliminar el mensaje de (.*)")45 def step_impl(self, locator):46 Selenium.mouse_over(self, locator)47 @step("Esperar (.*) segundos")48 def step_impl(self, timeLoad):49 timeLoad = int(timeLoad)50 Selenium.wait_time(self, timeLoad)51 @step("Hacer click sobre la imagen (.*)")52 def step_impl(self, locator):53 Selenium.js_click(self, locator)54 @then("Esperar que se haya cargado la página y volver al registro")55 def step_impl(self):56 Selenium.page_has_loaded(self)57 self.driver.back()58 print("Se ha vuelto al registro con éxito")59 @then("Cambiar a la nueva ventana (.*)")60 def step_impl(self, window):61 Selenium.switch_to_windows_name(self, window)62 Selenium.page_has_loaded(self)63 @step("Cerrar el driver y hacer foco en la ventana (.*)")64 def step_impl(self, window):65 self.driver.close()66 Selenium.switch_to_windows_name(self, window)67 @step('Verificar el (.*) con el texto "(.*)"')68 def step_impl(self, locator, text):69 Selenium.assert_text(self, locator, text)70 @step("Hacer click sobre el Check box (.*)")71 def step_impl(self, locator):72 Selenium.js_click(self, locator)73 @step('Hacer click sobre el link "(.*)"')74 def step_impl(self, locator):75 Selenium.js_click(self, locator)76 @step("Hacer click sobre el link (.*)")77 def step_impl(self, entity):78 Selenium.get_elements(self, entity).click()79 @then("Hacer screenshot (.*)")80 def step_impl(self, TestCase):81 Selenium.screenshot(self, TestCase)82 @step("Esperar que el campo (.*) se haya cargado y sea clickable")83 def step_impl(self, locator):84 Selenium.wait_element(self, locator)85 Selenium.check_element(self, locator)86 @then("Hacer skip de la prueba")87 def step_impl(self):88 Selenium.tearDown(self)89 pytest.skip("Eliminar de la prueba automática el paso 3")90 @given("Verificar la presencia de la imagen (.*)")91 def step_impl(self, locator):92 Selenium.check_element(self, locator)93 @given("Verificar la lista de (.*) disponible y pulsar (.*)")94 def step_impl(self, locator1, locator2):95 Selenium.mouse_over(self, locator1)96 Selenium.check_element(self, locator2)...
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!!