Skip to content

Instantly share code, notes, and snippets.

@agusrichard
Created May 16, 2022 22:24
Show Gist options
  • Save agusrichard/cec3874130a7256ced99a199bc7736ef to your computer and use it in GitHub Desktop.
Save agusrichard/cec3874130a7256ced99a199bc7736ef to your computer and use it in GitHub Desktop.
from selenium import webdriver
from django.conf import settings
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
class TestFunctional(StaticLiveServerTestCase):
def setUp(self) -> None:
self.num_todos = 10
self.xpath_username = "//label[@for='username']"
self.xpath_password = "//label[@for='password']"
# Using assertIn because when running tests in headless mode, the message is "Please fill out the field"
# when running in normal mode, the message is "Please fill in the field"
# So, we choose to use assertIn in this case
self.required_field_message = "Please fill"
self.assertTrue(hasattr(settings, "WEB_DRIVER_PATH"))
options = webdriver.ChromeOptions()
if settings.WEB_DRIVER_HEADLESS:
options.add_argument("--headless")
self.driver = webdriver.Chrome(
executable_path=settings.WEB_DRIVER_PATH, chrome_options=options
)
self.driver.maximize_window()
def tearDown(self) -> None:
self.driver.quit()
def test_positive_register(self):
self.driver.get(self.live_server_url)
self.driver.find_element_by_link_text("Register").click()
username_label = self.driver.find_element_by_xpath(self.xpath_username)
self.assertEqual(username_label.text, "Username")
username_input = self.driver.find_element_by_id("id_username")
username_input.send_keys("test")
self.assertEqual(username_input.get_attribute("value"), "test")
password_label = self.driver.find_element_by_xpath(self.xpath_password)
self.assertEqual(password_label.text, "Password")
password_input = self.driver.find_element_by_id("id_password")
password_input.send_keys("test")
self.assertEqual(password_input.get_attribute("value"), "test")
self.driver.find_element_by_id("id-submit").click()
def test_positive_login(self):
self.test_positive_register()
title = self.driver.find_elements_by_xpath("//*[contains(text(), 'Register')]")
self.assertEqual(len(title), 1)
username_input = self.driver.find_element_by_id("id_username")
username_input.send_keys("test")
self.assertEqual(username_input.get_attribute("value"), "test")
password_label = self.driver.find_element_by_xpath(self.xpath_password)
self.assertEqual(password_label.text, "Password")
password_input = self.driver.find_element_by_id("id_password")
password_input.send_keys("test")
self.assertEqual(password_input.get_attribute("value"), "test")
self.driver.find_element_by_id("id-submit").click()
def test_positive_home(self):
self.test_positive_login()
self.driver.find_elements_by_xpath("//*[contains(text(), 'Nothing todo...')]")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment