From d4bf0ed1a2724a762ceaee39159c660e93805319 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 19 Nov 2022 09:45:30 +0100 Subject: [PATCH] Refactor selenium login --- src/character/tests/test_interactions.py | 14 +++++++------- src/conftest.py | 5 +++++ src/party/tests/test_interactions.py | 9 +++------ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/character/tests/test_interactions.py b/src/character/tests/test_interactions.py index bb95cc7..d5798cb 100644 --- a/src/character/tests/test_interactions.py +++ b/src/character/tests/test_interactions.py @@ -20,8 +20,7 @@ def test_create_character(selenium: WebDriver, live_server: LiveServer): player = User.objects.create_user(username, password=password) # Go to home page - selenium.get(live_server.url) - login(selenium, username, password) + login(selenium, live_server, username, password) # Click on new character selenium.find_element(By.ID, "new-character").click() @@ -110,8 +109,7 @@ def test_list_characters(selenium: WebDriver, live_server: LiveServer): baker.make(Character, player=other) # Go to home page - selenium.get(live_server.url) - login(selenium, username, password) + login(selenium, live_server, username, password) # Assert only characters 1 and 2 are shown although there are 3 characters in DB assert Character.objects.count() == 3 @@ -133,8 +131,7 @@ def test_delete_character(selenium: WebDriver, live_server: LiveServer): player = User.objects.create_user(username, password=password) characters = baker.make(Character, _quantity=2, player=player) - selenium.get(live_server.url) - login(selenium, username, password) + login(selenium, live_server, username, password) assert Character.objects.count() == 2 selenium.find_element( @@ -147,7 +144,10 @@ def test_delete_character(selenium: WebDriver, live_server: LiveServer): assert Character.objects.filter(pk=characters[0].pk).first() is None -def login(selenium, username, password): +def login( + selenium: WebDriver, live_server: LiveServer, username: str, password: str +) -> None: + selenium.get(live_server.url) selenium.find_element(By.ID, "login").click() selenium.find_element(By.ID, "id_username").send_keys(username) selenium.find_element(By.ID, "id_password").send_keys(password) diff --git a/src/conftest.py b/src/conftest.py index 7755c9c..9c84d8f 100644 --- a/src/conftest.py +++ b/src/conftest.py @@ -17,3 +17,8 @@ def firefox_options(firefox_options): def settings(settings): settings.DEBUG_TOOLBAR = False return settings + + +@pytest.fixture +def initial_data(db: None) -> None: + call_command("loaddata", "initial_data") diff --git a/src/party/tests/test_interactions.py b/src/party/tests/test_interactions.py index ef2f824..e441c45 100644 --- a/src/party/tests/test_interactions.py +++ b/src/party/tests/test_interactions.py @@ -23,8 +23,7 @@ def test_add_character_to_existing_group(selenium: WebDriver, live_server: LiveS character = baker.make(Character, player=player) party = baker.make(Party, game_master=gm) - selenium.get(live_server.url) - login(selenium, username, password) + login(selenium, live_server, username, password) selenium.get(live_server.url + reverse("party:list")) selenium.find_element( @@ -52,8 +51,7 @@ def test_gm_observe_invited_character_in_group( character = baker.make(Character, player=player) party.invited_characters.add(character) - selenium.get(live_server.url) - login(selenium, username, password) + login(selenium, live_server, username, password) selenium.get(live_server.url + reverse("party:list")) selenium.find_element( @@ -81,8 +79,7 @@ def test_gm_observe_invited_character_in_two_groups( party.invited_characters.add(character) other_party.invited_characters.add(character) - selenium.get(live_server.url) - login(selenium, username, password) + login(selenium, live_server, username, password) selenium.get(live_server.url + reverse("party:list")) selenium.find_element(