Best Python code snippet using SeleniumBase
test_new_blog.py
Source:test_new_blog.py
...19 assert new_blog_page.does_have_text_in_page('New Blog')20@marks.all_ssize21def test_new_blog_page_should_save_new_blog(responsive_target, login_for_new_blog):22 new_blog_page = NewBlogPage(responsive_target['driver'], independent=False)23 new_blog_page.scroll_to_top()24 new_blog_page.enter_text_in_element(os.getcwd()+"/tests/data/test_image.jpg", 'main_image_input')25 new_blog_page.enter_text_in_element('selenium title', 'blog_title_input', clear=True)26 new_blog_page.enter_text_in_element('selenium subtitle', 'blog_subtitle_input', clear=True)27 new_blog_page.enter_text_in_element_and_click('new-tag', 'blog_tag_input')28 new_blog_page.scroll_to_top()29 new_blog_page.wait_for_element_to_have_text('fetch_status_title', 'ok')30 # errors:31 # - sometimes, fetch result display 'timeout error'32 # - firefox does not allow to input text at div element33 # - steps such as wait for element issue34 # check page title does exist35 assert new_blog_page.does_element_exist('fetch_status_title')36 # clean up37 new_blog_page.click_element('image_delete_icon')38 new_blog_page.click_element('blog_tag_delete_icon')39@marks.all_ssize40@pytest.mark.ppp41def test_new_blog_page_should_publish_new_blog(responsive_target, login_for_new_blog):42 new_blog_page = NewBlogPage(responsive_target['driver'], independent=False)43 new_blog_page.scroll_to_top()44 new_blog_page.enter_text_in_element('selenium title', 'blog_title_input', clear=True)45 new_blog_page.wait_for_element_to_have_text('fetch_status_title', 'ok')46 new_blog_page.enter_text_in_element('selenium subtitle', 'blog_subtitle_input', clear=True)47 new_blog_page.wait_for_element_to_have_text('fetch_status_title', 'ok')48 new_blog_page.scroll_to_bottom()49 new_blog_page.wait_for_animation_finish()50 new_blog_page.click_element("publish_button")51 new_blog_page.scroll_to_top()52 new_blog_page.wait_for_element_to_have_text('fetch_status_title', 'ok')53 # errors:54 # - sometimes, fetch result display 'timeout error'55 # - firefox does not allow to input text at div element56 # - steps such as wait for element issue57 # should re-implement this58 # check page title does exist59 assert new_blog_page.does_element_exist('fetch_status_title')60@marks.all_ssize61@pytest.mark.ppp62def test_new_blog_page_should_not_display_non_published_blog_at_blog_list_page(responsive_target, login_for_new_blog):63 new_blog_page = NewBlogPage(responsive_target['driver'], independent=False)64 target_blog_title = fake.sentence()65 new_blog_page.scroll_to_top()66 new_blog_page.wait_for_element_to_be_clickable('blog_title_input')67 new_blog_page.enter_text_in_element(target_blog_title, 'blog_title_input', clear=True)68 new_blog_page.wait_for_element_to_have_text('fetch_status_title', 'ok')69 new_blog_page.enter_text_in_element('selenium subtitle', 'blog_subtitle_input', clear=True)70 new_blog_page.scroll_to_bottom()71 new_blog_page.wait_for_element_to_have_text('fetch_status_title', 'ok')72 new_blog_page.driver.get(cfg.blog_list_url)73 blog_list_page = BlogListPage(responsive_target['driver'], independent=False)74 if responsive_target['size_type'] not in ['desktop', 'laptop']:75 blog_list_page.wait_for_element_to_be_clickable('setting_icon')76 # sometimes, firefox cause not clickable error even if above line so wait animation for additional77 blog_list_page.wait_for_animation_finish()78 blog_list_page.click_element('setting_icon')79 blog_list_page.wait_for_animation_finish()80 blog_list_page.enter_text_in_element(target_blog_title, 'filter_keyword_input')81 if responsive_target['size_type'] not in ['desktop', 'laptop']:82 blog_list_page.click_element('filter_sort_close_icon')83 blog_list_page.wait_for_animation_finish()84 blog_list_page.wait_for_element('no_search_result_title')85 assert 186 new_blog_page.driver.get(cfg.new_blog_url)87@marks.all_ssize88def test_new_blog_page_should_display_published_blog_at_blog_list_page(responsive_target, login_for_new_blog):89 new_blog_page = NewBlogPage(responsive_target['driver'], independent=False)90 target_blog_title = fake.sentence()91 new_blog_page.scroll_to_top()92 new_blog_page.wait_for_element('blog_title_input')93 new_blog_page.enter_text_in_element(target_blog_title, 'blog_title_input', clear=True)94 new_blog_page.wait_for_element_to_have_text('fetch_status_title', 'ok')95 new_blog_page.enter_text_in_element('selenium subtitle', 'blog_subtitle_input', clear=True)96 new_blog_page.wait_for_element_to_have_text('fetch_status_title', 'ok')97 new_blog_page.scroll_to_bottom()98 new_blog_page.wait_for_animation_finish()99 new_blog_page.click_element("publish_button")100 new_blog_page.scroll_to_top()101 new_blog_page.wait_for_element_to_have_text('fetch_status_title', 'ok')102 new_blog_page.driver.get(cfg.blog_list_url)103 blog_list_page = BlogListPage(responsive_target['driver'], independent=False)104 if responsive_target['size_type'] not in ['desktop', 'laptop']:105 blog_list_page.wait_for_element_to_be_clickable('setting_icon')106 # sometimes, firefox cause not clickable error even if above line so wait animation for additional107 blog_list_page.wait_for_animation_finish()108 blog_list_page.click_element('setting_icon')109 blog_list_page.wait_for_animation_finish()110 blog_list_page.wait_for_element('filter_keyword_input')111 blog_list_page.enter_text_in_element(target_blog_title, 'filter_keyword_input')112 if responsive_target['size_type'] not in ['desktop', 'laptop']:113 blog_list_page.click_element('filter_sort_close_icon')114 blog_list_page.wait_for_animation_finish()...
mobile_screen.py
Source:mobile_screen.py
1from src.utils.print import PRINT2from src.global_defines import Direction, SwipeArea3from src.generic_building_blocks.generic_screen import GenericScreen, DERIVED_CLASS_MISSING_IMPLEMENTATION4class MobileScreen(GenericScreen):5 def __init__(self, test):6 GenericScreen.__init__(self, test)7 def get_screen_id(self):8 PRINT(DERIVED_CLASS_MISSING_IMPLEMENTATION % ('MobileScreen', 'get_screen_id()'), 'red')9 raise NotImplementedError10 def swipe(self, distance=None, direction=Direction.UP, area=SwipeArea.CENTER, rect=None):11 """12 :param distance: The range you want to perform the swipe, if value is None swipe will be 1/3 of the screen13 :param direction: Can be UP, DOWN, LEFT, RIGHT14 :param area: Screen is divided to 3 swipe sections, TOP is the first 1/3 section of the screens, CENTER is15 center point of the screen and BOTTOM is last 1/3 section of the screen16 :param rect: The rect where the swipe is being performed on17 """18 if rect is None:19 rect = self.test.driver.get_window_rect()20 VERTICAL_SWIPE = direction == Direction.UP or direction == Direction.DOWN21 HORIZONTAL_SWIPE = direction == Direction.LEFT or direction == Direction.RIGHT22 """23 Calculate the swipe start point24 """25 section_size = rect['height'] / 326 start_x = rect['x'] + (rect['width'] / 2)27 if area == SwipeArea.TOP:28 start_y = rect['y'] + (section_size / 2)29 elif area == SwipeArea.CENTER:30 start_y = rect['y'] + (rect['height'] / 2)31 elif area == SwipeArea.BOTTOM:32 start_y = rect['y'] + (section_size * 2.5)33 """34 Calculate the swipe end point35 """36 if distance is None and VERTICAL_SWIPE:37 distance = rect['height'] / 338 elif distance is None and HORIZONTAL_SWIPE:39 distance = rect['width'] / 340 if direction == Direction.UP or direction == Direction.LEFT:41 distance *= -142 end_y = start_y if HORIZONTAL_SWIPE else start_y + distance43 end_x = start_x if VERTICAL_SWIPE else start_x + distance44 self.test.driver.swipe_by_coordinates(start_x, start_y, end_x, end_y)45 def search_horizontally_for_item_by_text(self, text, distance, direction, area):46 """47 :param text: The text in element we are looking for48 :param distance: The range you want to perform the swipe, if value is None swipe will be 1/3 of the screen49 :param direction: Can be LEFT, RIGHT50 :param area: Screen is divided to 3 swipe sections, TOP is the first 1/3 section of the screens, CENTER is51 center point of the screen and BOTTOM is last 1/3 section of the screen52 """53 element = None54 counter = 055 while counter < 35:56 element = self.test.driver.find_element_by_text(text)57 if element is not None:58 break59 self.swipe(distance=distance, direction=direction, area=area)60 counter += 161 return element62 def search_for_item_by_text(self, text, scroll_to_top=False, retries=2):63 """64 The following function scroll and search for element on screens by text65 :param text: The text in element we are looking for66 :param scroll_to_top: define if you want to scroll to top of the screen before start the search67 :retries: the retries between swipes when searching for a text on the screen68 :return: the element if found None if not69 """70 return self.__search_by_type__('text', text, scroll_top=scroll_to_top, retries=retries)71 def search_for_item_by_id(self, accessibility_id, scroll_to_top=False, retries=2):72 """73 The following function scroll and search for element on screens by id74 :param accessibility_id: The element identifier we are looking for75 :param scroll_to_top: define if you want to scroll to top of the screen before start the search76 :retries: the retries between swipes when searching for an id on the screen77 :return: the element if found None if not78 """79 return self.__search_by_type__('id', accessibility_id, scroll_top=scroll_to_top, retries=retries)80 """81 Private Implementation 82 """83 def __search_by_type__(self, access_type, value, scroll_top=False, retries=2):84 if scroll_top is True:85 before = self.test.driver.get_screenshot_as_base64()86 while True:87 self.swipe(direction=Direction.DOWN)88 self.test.driver.wait(1)89 after = self.test.driver.get_screenshot_as_base64()90 if before == after:91 break92 before = after93 # scroll down and search94 element = None95 before = self.test.driver.get_screenshot_as_base64()96 while True:97 if access_type == 'text':98 element = self.test.driver.find_element_by_text(value, retries=retries)99 elif access_type == 'id':100 element = self.test.driver.find_element_by_accessibility_id(value, retries=retries)101 if element:102 break103 self.swipe()104 self.test.driver.wait(1)105 after = self.test.driver.get_screenshot_as_base64()106 if before == after:107 break108 before = after...
side_menu.py
Source:side_menu.py
1from src.generic_building_blocks.mobile.screens.mobile_screen import MobileScreen2class SideMenu(MobileScreen):3 """4 Public Implementation5 """6 def navigate_to_item(self, name, scroll_to_top=False):7 self.test.building_blocks.navigation_bar.press_menu_button()8 element = self.search_for_item_by_text(name, scroll_to_top=scroll_to_top)9 element.click()10 """11 Private Implementation 12 """13 def __init__(self, test):14 self.test = test15 self.__setup_side_menu__()16 def __setup_side_menu__(self):...
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!!