Zadanie 4 - testy
This commit is contained in:
14
tests/flask_test_server.py
Normal file
14
tests/flask_test_server.py
Normal file
@@ -0,0 +1,14 @@
|
||||
import threading
|
||||
import time
|
||||
from src.main import app
|
||||
|
||||
def run_app():
|
||||
app.run(host="127.0.0.1", port=5000, debug=False, use_reloader=False)
|
||||
|
||||
def start_flask_in_thread():
|
||||
thread = threading.Thread(target=run_app)
|
||||
thread.daemon = True
|
||||
thread.start()
|
||||
# Wait a bit for the server to start
|
||||
time.sleep(2)
|
||||
return thread
|
||||
27
tests/test_pages.py
Normal file
27
tests/test_pages.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import pytest
|
||||
from src.main import app
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
app.testing = True
|
||||
with app.test_client() as client:
|
||||
yield client
|
||||
|
||||
def test_about_page_status_and_content(client):
|
||||
response = client.get('/about')
|
||||
assert response.status_code == 200
|
||||
assert b'O projekcie' in response.data
|
||||
|
||||
def test_contact_page_get(client):
|
||||
response = client.get('/contact')
|
||||
assert response.status_code == 200
|
||||
assert b'<form' in response.data
|
||||
assert b'name="name"' in response.data
|
||||
|
||||
def test_contact_page_post(client):
|
||||
response = client.post('/contact', data={
|
||||
'name': 'Tester',
|
||||
'message': 'To jest test.'
|
||||
}, follow_redirects=True)
|
||||
assert response.status_code == 200
|
||||
assert b'Dzi' in response.data # fragment "Dziękujemy za kontakt!"
|
||||
43
tests/test_selenium_contact.py
Normal file
43
tests/test_selenium_contact.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.common.by import By
|
||||
from selenium.webdriver.common.keys import Keys
|
||||
from selenium.webdriver.support.ui import WebDriverWait
|
||||
from selenium.webdriver.support import expected_conditions as EC
|
||||
import time
|
||||
import unittest
|
||||
from flask_test_server import start_flask_in_thread
|
||||
|
||||
class ContactPageSeleniumTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
# Start Flask server in background
|
||||
cls.flask_thread = start_flask_in_thread()
|
||||
cls.base_url = "http://127.0.0.1:5000/contact"
|
||||
# Możesz zmienić na Firefox, jeśli nie masz ChromeDriver
|
||||
cls.driver = webdriver.Chrome()
|
||||
cls.driver.implicitly_wait(5)
|
||||
time.sleep(2) # Ensure server is up
|
||||
|
||||
def test_contact_form_submission(self):
|
||||
self.driver.get(self.base_url)
|
||||
name_input = self.driver.find_element(By.ID, "name")
|
||||
message_input = self.driver.find_element(By.ID, "message")
|
||||
name_input.send_keys("Selenium")
|
||||
message_input.send_keys("To jest test Selenium.")
|
||||
# Find all submit buttons and click the second one (contact form)
|
||||
submit_btns = self.driver.find_elements(By.XPATH, "//button[@type='submit']")
|
||||
submit_btns[1].click()
|
||||
# Wait for the success message to appear
|
||||
wait = WebDriverWait(self.driver, 5)
|
||||
success_msg = wait.until(
|
||||
EC.visibility_of_element_located((By.XPATH, "//*[contains(text(), 'Dziękujemy za kontakt!')]"))
|
||||
)
|
||||
self.assertTrue(success_msg.is_displayed())
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.driver.quit()
|
||||
# No direct way to stop Flask dev server, but thread will exit with process
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user