Created
March 21, 2020 07:45
-
-
Save mloza/e51f3c3d5d0f601475a5b1b7d849a921 to your computer and use it in GitHub Desktop.
Kod do wpisu Testy w Django z użyciem Selenium https://blog.mloza.pl/testy-w-django-z-uzyciem-selenium/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth.models import User | |
from django.contrib.staticfiles.testing import StaticLiveServerTestCase | |
from selenium.webdriver.firefox.webdriver import WebDriver | |
from selenium.webdriver.support.wait import WebDriverWait | |
class AuthenticationTests(StaticLiveServerTestCase): | |
def __init__(self, methodName='runTest'): | |
super().__init__(methodName='runTest') | |
#dane konta testowego | |
self.username = "test" | |
self.email = "test@test.com" | |
self.test_pass = "test_pass" | |
@classmethod | |
def setUpClass(cls): | |
#otwieramy przeglądarkę | |
super(AuthenticationTests, cls).setUpClass() | |
cls.selenium = WebDriver() | |
@classmethod | |
def tearDownClass(cls): | |
#zamykamy przeglądarkę | |
cls.selenium.quit() | |
super(AuthenticationTests, cls).tearDownClass() | |
def runTest(self): | |
#tworzenie użytkownika | |
user = User.objects.create_superuser(self.username, self.email, self.test_pass) | |
user.save() | |
#próba zalogowania | |
self.selenium.get("%s/admin" % self.live_server_url) | |
self.selenium.find_element_by_id("id_username").send_keys(self.username) | |
self.selenium.find_element_by_id("id_password").send_keys(self.test_pass) | |
self.selenium.find_element_by_css_selector("input[type='submit']").click() | |
#czekamy aż załaduje się strona panelu | |
WebDriverWait(self.selenium, 2).until(lambda driver: driver.find_element_by_css_selector("#content h1")) | |
#sprawdzamy czy jesteśmy na stronie panelu | |
self.assertEqual("Site administration", self.selenium.find_element_by_css_selector("#content h1").text) | |
#sprzątamy | |
user.delete() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment