From 311c601724c4d26065ad1a1a0389a4861893ef4e Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 13 Jul 2023 21:49:27 +0200 Subject: [PATCH] fix e2e tests --- e2e/test_login.py | 7 +++++-- e2e/test_logout.py | 13 +++++++++---- e2e/test_registration.py | 8 ++++++-- e2e/test_workouts.py | 2 +- e2e/utils.py | 12 ++++++++---- 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/e2e/test_login.py b/e2e/test_login.py index de57f01c..79148fbb 100644 --- a/e2e/test_login.py +++ b/e2e/test_login.py @@ -22,7 +22,7 @@ class TestLogin: assert inputs[1].get_attribute('id') == 'password' assert inputs[1].get_attribute('type') == 'password' - button = selenium.find_element(By.TAG_NAME, 'button') + button = selenium.find_elements(By.TAG_NAME, 'button')[-1] assert button.get_attribute('type') == 'submit' assert 'Log in' in button.text @@ -47,4 +47,7 @@ class TestLogin: assert 'Statistics' in nav assert 'Add a workout' in nav assert user['username'] in nav - assert 'Logout' in nav + logout_button = selenium.find_elements(By.CLASS_NAME, 'logout-button')[ + 0 + ] + assert logout_button diff --git a/e2e/test_logout.py b/e2e/test_logout.py index ed34811d..3169d9ac 100644 --- a/e2e/test_logout.py +++ b/e2e/test_logout.py @@ -6,14 +6,19 @@ from .utils import register_valid_user class TestLogout: def test_user_can_log_out(self, selenium): user = register_valid_user(selenium) - user_menu = selenium.find_element(By.CLASS_NAME, 'nav-items-user-menu') - logout_link = user_menu.find_elements(By.CLASS_NAME, 'nav-item')[2] + logout_button = selenium.find_elements(By.CLASS_NAME, 'logout-button')[ + 0 + ] - logout_link.click() + logout_button.click() + modal = selenium.find_element(By.ID, 'modal') + confirm_button = modal.find_elements(By.CLASS_NAME, 'confirm')[0] + confirm_button.click() selenium.implicitly_wait(1) nav = selenium.find_element(By.ID, 'nav').text assert 'Register' in nav assert 'Login' in nav assert user['username'] not in nav - assert 'Logout' not in nav + buttons = selenium.find_elements(By.CLASS_NAME, 'logout-button') + assert buttons == [] diff --git a/e2e/test_registration.py b/e2e/test_registration.py index 24e8f3d1..188f25e2 100644 --- a/e2e/test_registration.py +++ b/e2e/test_registration.py @@ -35,7 +35,7 @@ class TestRegistration: assert form_infos[1].text == 'Enter a valid email address.' assert form_infos[2].text == 'At least 8 characters required.' - button = selenium.find_element(By.TAG_NAME, 'button') + button = selenium.find_elements(By.TAG_NAME, 'button')[-1] assert button.get_attribute('type') == 'submit' assert 'Register' in button.text @@ -84,7 +84,11 @@ class TestRegistration: register(selenium, user) assert selenium.current_url == URL - errors = selenium.find_element(By.CLASS_NAME, 'error-message').text + errors = ( + selenium.find_element(By.ID, 'user-form') + .find_element(By.CLASS_NAME, 'error-message') + .text + ) assert 'Sorry, that username is already taken.' in errors def test_user_does_not_return_error_if_email_is_already_taken( diff --git a/e2e/test_workouts.py b/e2e/test_workouts.py index d6b02973..521c3fb6 100644 --- a/e2e/test_workouts.py +++ b/e2e/test_workouts.py @@ -30,7 +30,7 @@ class TestWorkout: ) selenium.find_element(By.NAME, 'workout-distance').send_keys('10') - confirm_button = selenium.find_element(By.CLASS_NAME, 'confirm') + confirm_button = selenium.find_elements(By.CLASS_NAME, 'confirm')[-1] confirm_button.click() WebDriverWait(selenium, 10).until( diff --git a/e2e/utils.py b/e2e/utils.py index 795f865f..1cf426f1 100644 --- a/e2e/utils.py +++ b/e2e/utils.py @@ -34,7 +34,7 @@ def register(selenium, user): password.send_keys(user.get('password')) accepted_policy = selenium.find_element(By.ID, 'accepted_policy') accepted_policy.click() - submit_button = selenium.find_element(By.TAG_NAME, 'button') + submit_button = selenium.find_elements(By.TAG_NAME, 'button')[-1] submit_button.click() @@ -45,7 +45,7 @@ def login(selenium, user): email.send_keys(user.get('email')) password = selenium.find_element(By.ID, 'password') password.send_keys(user.get('password')) - submit_button = selenium.find_element(By.TAG_NAME, 'button') + submit_button = selenium.find_elements(By.TAG_NAME, 'button')[-1] submit_button.click() @@ -65,8 +65,12 @@ def register_valid_user(selenium): def register_valid_user_and_logout(selenium): user = register_valid_user(selenium) user_menu = selenium.find_element(By.CLASS_NAME, 'nav-items-user-menu') - logout_link = user_menu.find_elements(By.CLASS_NAME, 'nav-item')[2] - logout_link.click() + logout_button = user_menu.find_elements(By.CLASS_NAME, 'logout-button')[0] + logout_button.click() + modal = selenium.find_element(By.ID, 'modal') + confirm_button = modal.find_elements(By.CLASS_NAME, 'confirm')[0] + confirm_button.click() + selenium.implicitly_wait(1) return user