Best Python code snippet using assertpy_python
newsletter_subscription.py
Source:newsletter_subscription.py
...63 context.page.get_data('email').get_when_visible().send_keys(data)64@then("warning about wrong email is visible")65def step_impl(context):66 (assert_that(context.page.empty_fields.get_when_visible()[0].text)67 .described_as("Checking correctness of warning message")68 .contains("Write correct email address "))69@then("information that email is already in use is visible")70def step_impl(context):71 (assert_that(context.page.email_already_in_use_warning.get_when_visible().text)72 .described_as("Checking correctnes of warning information about email already in use ")73 .contains("This e-mail is already in use. Please use different email"))74@then("modal with confirmation that user has been added to the newsletter is visible")75def step_impl(context):76 (assert_that(context.page.confirmation_modal.get_when_visible().text)77 .described_as("Checking if confirmation modal is visible")78 .contains("Successfully added to newsletter"))79@then("information about missing all fields is displayed correctly")80def step_impl(context):81 dict = {0: 'The "E-mail" field is required!', 1: 'The "First name" field is required!',82 2: 'The "Surname" field is required!', 3: 'The "newsletter type" field is required!',83 4: 'The "Start date" field is required!', 5: 'Accepting terms and condition is required!'}84 for idx, val in enumerate(context.page.empty_fields.get_when_visible()):85 assert_that(val.text).described_as("Checking the correctness of the information for user").contains(dict[idx])86@then("agreement contains all valid data")87def step_impl(context):88 (assert_that(context.page.agreement_title.get_when_visible().text)89 .described_as("Checking agreement details")90 .contains("Agreement"))91@then("information that subscription must be longer day is visible")92def step_impl(context):93 # notification should be corrected when XXX(bug number) will be resolved94 (assert_that(context.page.error_notification.get_when_visible().text)95 .described_as("Checking error notification")...
tests.py
Source:tests.py
...26 wishlist.search_bar(game_title)27 time.sleep(0.5)28 found_game_title = wishlist.first_record_title()2930 assert_that(game_title).is_equal_to(found_game_title).described_as("Searched game title should match with search bar input.")313233 def test_02_filter_games(self):34 game_title = "Siege of Avalon: Anthology" 35 filters = Filters(self.driver)36 37 filters.get_wishlist_page()38 filters.select_genre("57")39 filters.select_system("windows")40 filters.select_language("pl")41 filters.select_features("1")42 filters.select_top_price("2")4344 time.sleep(0.5) #short sleep due to page going too fast but same selector is available for different games45 found_game_title = filters.first_record_title()4647 assert_that(game_title).is_equal_to(found_game_title).described_as("Searched game title should be adequate to applied filters.")48 49 def test_03_sort_games(self):50 game_title = "Cyberpunk 2077"51 sorter = Sorter(self.driver)5253 sorter.get_wishlist_page()54 sorter.choose_sorter("UserReviews")5556 time.sleep(0.5)57 found_game_title = sorter.first_record_title()5859 assert_that(game_title).is_equal_to(found_game_title).described_as("First displayed game title should be adequate to applied sort.")606162 def test_04_add_and_remove_game_from_cart(self):63 game_title = "Heroes of Might and Magic® 3: Complete"64 empty_cart_msg = "Odkryj niezwykÅe gry i oferty."65 cart = Cart(self.driver)6667 cart.add_game_to_cart_by_gogid("1207658787")68 self.driver.refresh() #refresh due to issue where cart content is empty after first addition69 cart.open_cart()70 added_game = cart.first_record_title()71 72 assert_that(game_title).is_equal_to(added_game).described_as("Game in cart should equal added game.")7374 cart.remvoe_first_record()75 time.sleep(0.5)76 empty_cart_displayed_msg = cart.empty_cart_msg()7778 assert_that(empty_cart_msg).is_equal_to(empty_cart_displayed_msg).described_as(f"Empty cart message should be 'Odkryj niezwykÅe gry i oferty.'")798081 def test_05_go_to_game_page(self):82 game_url = "https://www.gog.com/game/prince_of_persia_the_sands_of_time"83 wishlist = WishlistPage(self.driver)8485 wishlist.get_wishlist_page()86 wishlist.open_game_main_page("Prince of Persia")87 current_url = self.driver.current_url8889 assert_that(game_url).is_equal_to(current_url).described_as(f"URL of the game should lead to its page.")909192 @classmethod93 def tearDownClass(cls):94 cls.driver.close()95 cls.driver.quit()9697if __name__ == "__main__":
...
test_json_place_holder_service.py
Source:test_json_place_holder_service.py
...4post_keys = ['userId', 'id', 'title', 'body']5def test_get_posts(service_obj):6 res = service_obj.get_posts()7 with soft_assertions():8 assert_that(res.status_code).described_as('Response code').is_equal_to(200)9 assert_that(res.json()).described_as('Records in response').is_length(100)10 for post in res.json():11 assert_that(post).described_as('post').contains_only(*post_keys)12def test_get_a_single_post(service_obj):13 # requesting a random post no in each test run14 requested_post_id = random.randint(1, 100)15 res = service_obj.get_post_with_id(requested_post_id)16 with soft_assertions():17 assert_that(res.status_code).described_as('Response code').is_equal_to(200)18 assert_that(res.json()).described_as('Response').contains_only(*post_keys)19 assert_that(res.json()).described_as('Id in response').has_id(requested_post_id)20def test_delete_a_post(service_obj):21 # requesting a random post no in each test run22 requested_post_id = random.randint(1, 100)23 res = service_obj.delete_post_with_id(requested_post_id)24 with soft_assertions():25 assert_that(res.status_code).described_as('Response code').is_equal_to(200)26 assert_that(res.json()).described_as('Get Response').is_equal_to({})27def test_invalid_post(service_obj):28 res = service_obj.get_invalid_posts()29 assert_that(res.status_code).described_as('Response code').is_equal_to(404)30def test_post_a_new_post(service_obj):31 # requesting a random user id in each test run32 requested_user_id = random.randint(1, 100)33 body = {34 "title": "foo",35 "body": "bar",36 "userId": requested_user_id37 }38 res = service_obj.post_a_new_post(body)39 with soft_assertions():40 assert_that(res.status_code).described_as('Response code').is_equal_to(201)41 assert_that(res.json()).described_as('Response').contains_only(*post_keys)42 # getting the created request outside of soft assert43 # so as if creation fails, then get is not executed44 created_post_id = res.json().get('id')45 get_res = service_obj.get_post_with_id(created_post_id)46 body['id'] = created_post_id47 # ideally you should verify newly created body, but it doesnt works48 # newly created post is sent as a blank json from server49 # assert_that(get_res.json()).described_as('Get Response').is_equal_to(body)50 assert_that(get_res.json()).described_as('Get Response').is_equal_to({})51def test_put_a_post(service_obj):52 # requesting a random post id in each test run53 requested_user_id = 154 requested_post_id = random.randint(1, 100)55 body = {56 "id": requested_post_id,57 "title": "abc",58 "body": "xyz",59 "userId": requested_user_id60 }61 res = service_obj.put_a_post(body)62 with soft_assertions():63 assert_that(res.status_code).described_as('Response code').is_equal_to(200)64 assert_that(res.json()).described_as('Response').contains_only(*post_keys)65 get_res = service_obj.get_post_with_id(requested_post_id)66 # ideally we should be checking for the post with updated body67 # newly updated post is being sent as the older one itself68 # assert_that(get_res.json()).described_as('Get Response').is_equal_to(body)...
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!!