Skip to content

Commit

Permalink
Tests: fix auth tests that use selenium
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Nov 28, 2023
1 parent 0a16e28 commit bc05121
Showing 1 changed file with 40 additions and 29 deletions.
69 changes: 40 additions & 29 deletions lemarche/www/auth/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from django.urls import reverse
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.support.select import Select

from lemarche.users.factories import DEFAULT_PASSWORD, UserFactory
Expand Down Expand Up @@ -69,6 +70,18 @@
}


def setup_selenium_browser():
options = FirefoxOptions()
options.add_argument("-headless")
try:
driver = webdriver.Firefox(options=options)
except: # selenium.common.exceptions.InvalidArgumentException: Message: binary is not a Firefox executable # noqa
service = FirefoxService(executable_path="/snap/bin/geckodriver")
driver = webdriver.Firefox(options=options, service=service)
# driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver')
return driver


def scroll_to_and_click_element(driver, element, click=True, sleep_time=1):
"""
Helper to avoid some errors with selenium
Expand All @@ -91,10 +104,8 @@ class SignupFormTest(StaticLiveServerTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
# selenium browser # TODO: make it test-wide
options = Options()
options.add_argument("-headless")
cls.driver = webdriver.Firefox(options=options)
# selenium browser
cls.driver = setup_selenium_browser()
cls.driver.implicitly_wait(1)
# other init
cls.user_count = User.objects.count()
Expand Down Expand Up @@ -127,7 +138,7 @@ def _complete_form(self, user_profile: dict, signup_url=reverse("auth:signup"),
submit_element = self.driver.find_element(By.CSS_SELECTOR, "form button[type='submit']")
scroll_to_and_click_element(self.driver, submit_element)

def _assert_signup_success(self, redirect_url: str) -> list:
def _assert_signup_success(self, redirect_url: str, with_content=True) -> list:
"""Assert the success signup and returns the sucess messages
Args:
Expand All @@ -138,16 +149,17 @@ def _assert_signup_success(self, redirect_url: str) -> list:
"""
# should create User
self.assertEqual(User.objects.count(), self.user_count + 1)
# user should be automatically logged in
header = self.driver.find_element(By.CSS_SELECTOR, "header#header")
self.assertTrue("Mon espace" in header.text)
self.assertTrue("Connexion" not in header.text)
# should redirect to redirect_url
self.assertEqual(self.driver.current_url, f"{self.live_server_url}{redirect_url}")
# message should be displayed
messages = self.driver.find_element(By.CSS_SELECTOR, "div.messages")
self.assertTrue("Inscription validée" in messages.text)
return messages
if with_content:
# user should be automatically logged in
header = self.driver.find_element(By.CSS_SELECTOR, "header#header")
self.assertTrue("Mon espace" in header.text)
self.assertTrue("Connexion" not in header.text)
# should redirect to redirect_url
self.assertEqual(self.driver.current_url, f"{self.live_server_url}{redirect_url}")
# message should be displayed
messages = self.driver.find_element(By.CSS_SELECTOR, "div.messages")
self.assertTrue("Inscription validée" in messages.text)
return messages

def test_siae_submits_signup_form_success(self):
self._complete_form(user_profile=SIAE.copy(), with_submit=True)
Expand Down Expand Up @@ -210,17 +222,17 @@ def test_buyer_submits_signup_form_error(self):
# should not submit form (position field is required)
self.assertEqual(self.driver.current_url, f"{self.live_server_url}{reverse('auth:signup')}")

# TODO: problem with this test
# def test_partner_submits_signup_form_success(self):
# self._complete_form(user_profile=PARTNER, with_submit=False)
# partner_kind_option_element = self.driver.find_element(
# By.XPATH, "//select[@id='id_partner_kind']/option[text()='Réseaux IAE']"
# )
# scroll_to_and_click_element(self.driver, partner_kind_option_element, sleep_time=10)
# submit_element = self.driver.find_element(By.CSS_SELECTOR, "form button[type='submit']")
# scroll_to_and_click_element(self.driver, submit_element)
def test_partner_submits_signup_form_success(self):
self._complete_form(user_profile=PARTNER, with_submit=False)

partner_kind_select_element = self.driver.find_element(By.CSS_SELECTOR, "select#id_partner_kind")
partner_kind_select = Select(partner_kind_select_element)
scroll_to_and_click_element(self.driver, partner_kind_select_element, click=False)
partner_kind_select.select_by_visible_text("Réseaux IAE")
submit_element = self.driver.find_element(By.CSS_SELECTOR, "form button[type='submit']")
scroll_to_and_click_element(self.driver, submit_element)

# self._assert_signup_success(redirect_url=reverse("wagtail_serve", args=("",)))
self._assert_signup_success(redirect_url=reverse("wagtail_serve", args=("",)), with_content=False)

def test_partner_submits_signup_form_error(self):
user_profile = PARTNER.copy()
Expand Down Expand Up @@ -253,9 +265,8 @@ class LoginFormTest(StaticLiveServerTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
options = Options()
options.add_argument("-headless")
cls.driver = webdriver.Firefox(options=options)
# selenium browser
cls.driver = setup_selenium_browser()
cls.driver.implicitly_wait(1)

def test_siae_user_can_sign_in_and_is_redirected_to_dashboard(self):
Expand Down

0 comments on commit bc05121

Please sign in to comment.