Refactor selenium login

This commit is contained in:
Gabriel Augendre 2022-11-19 09:45:30 +01:00
parent 312abbbcdd
commit d4bf0ed1a2
3 changed files with 15 additions and 13 deletions

View file

@ -20,8 +20,7 @@ def test_create_character(selenium: WebDriver, live_server: LiveServer):
player = User.objects.create_user(username, password=password) player = User.objects.create_user(username, password=password)
# Go to home page # Go to home page
selenium.get(live_server.url) login(selenium, live_server, username, password)
login(selenium, username, password)
# Click on new character # Click on new character
selenium.find_element(By.ID, "new-character").click() 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) baker.make(Character, player=other)
# Go to home page # Go to home page
selenium.get(live_server.url) login(selenium, live_server, username, password)
login(selenium, username, password)
# Assert only characters 1 and 2 are shown although there are 3 characters in DB # Assert only characters 1 and 2 are shown although there are 3 characters in DB
assert Character.objects.count() == 3 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) player = User.objects.create_user(username, password=password)
characters = baker.make(Character, _quantity=2, player=player) characters = baker.make(Character, _quantity=2, player=player)
selenium.get(live_server.url) login(selenium, live_server, username, password)
login(selenium, username, password)
assert Character.objects.count() == 2 assert Character.objects.count() == 2
selenium.find_element( 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 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, "login").click()
selenium.find_element(By.ID, "id_username").send_keys(username) selenium.find_element(By.ID, "id_username").send_keys(username)
selenium.find_element(By.ID, "id_password").send_keys(password) selenium.find_element(By.ID, "id_password").send_keys(password)

View file

@ -17,3 +17,8 @@ def firefox_options(firefox_options):
def settings(settings): def settings(settings):
settings.DEBUG_TOOLBAR = False settings.DEBUG_TOOLBAR = False
return settings return settings
@pytest.fixture
def initial_data(db: None) -> None:
call_command("loaddata", "initial_data")

View file

@ -23,8 +23,7 @@ def test_add_character_to_existing_group(selenium: WebDriver, live_server: LiveS
character = baker.make(Character, player=player) character = baker.make(Character, player=player)
party = baker.make(Party, game_master=gm) party = baker.make(Party, game_master=gm)
selenium.get(live_server.url) login(selenium, live_server, username, password)
login(selenium, username, password)
selenium.get(live_server.url + reverse("party:list")) selenium.get(live_server.url + reverse("party:list"))
selenium.find_element( selenium.find_element(
@ -52,8 +51,7 @@ def test_gm_observe_invited_character_in_group(
character = baker.make(Character, player=player) character = baker.make(Character, player=player)
party.invited_characters.add(character) party.invited_characters.add(character)
selenium.get(live_server.url) login(selenium, live_server, username, password)
login(selenium, username, password)
selenium.get(live_server.url + reverse("party:list")) selenium.get(live_server.url + reverse("party:list"))
selenium.find_element( selenium.find_element(
@ -81,8 +79,7 @@ def test_gm_observe_invited_character_in_two_groups(
party.invited_characters.add(character) party.invited_characters.add(character)
other_party.invited_characters.add(character) other_party.invited_characters.add(character)
selenium.get(live_server.url) login(selenium, live_server, username, password)
login(selenium, username, password)
selenium.get(live_server.url + reverse("party:list")) selenium.get(live_server.url + reverse("party:list"))
selenium.find_element( selenium.find_element(