Initial commit

This commit is contained in:
tomeros
2021-03-24 15:11:34 +02:00
commit 7d68b93238
60 changed files with 1847 additions and 0 deletions

View File

View File

@@ -0,0 +1,40 @@
from app.tests.utilities import selenium_utility
class Index(selenium_utility.SeleniumUtility):
_heading_locator = '//p[@id="heading-p"]'
_url_input_locator = '//input[@id="url-input"]'
_shorten_button_locator = '//button[@type="submit"]'
_enter_url_warning = '//div[@class="alert-box alert-warning"]'
_try_again_button = '//button[@id="try-again-btn"]'
def __init__(self, driver):
self.driver = driver
super().__init__(driver)
self.url_input = self.get_element(self._url_input_locator)
self.shorten_button = self.get_element(self._shorten_button_locator)
def get_heading_text(self):
return self.get_element(self._heading_locator).text
def enter_valid_url(self):
self.url_input = self.get_element(self._url_input_locator)
self.url_input.click()
self.url_input.send_keys('youtube.com')
def enter_invalid_url(self):
self.url_input.click()
self.url_input.send_keys('https://www.youtube.com/what?a=b&c=d')
def click_shorten_button(self):
self.shorten_button = self.get_element(self._shorten_button_locator)
self.shorten_button.click()
def check_warning_present(self):
return self.get_element(self._enter_url_warning).is_displayed()
def get_current_url(self):
return self.driver.current_url.split('/')[-1]
def click_try_again(self):
self.wait_for_element(self._try_again_button).click()

View File

@@ -0,0 +1,29 @@
from app.tests.utilities import selenium_utility
import pyperclip as pc
class Result(selenium_utility.SeleniumUtility):
_url_input_locator = '//input[@id="copy-able"]'
_copy_button_locator = '//button[@id="copy-btn"]'
_total_clicks_url = '//a[@id="total-clicks-link"]'
def __init__(self, driver):
self.driver = driver
super().__init__(driver)
self.url_input = self.get_element(self._url_input_locator)
self.copy_button = self.get_element(self._copy_button_locator)
def get_input_text(self):
return self.get_element(self._url_input_locator).get_attribute('value')
def click_copy_button(self):
self.copy_button.click()
def go_to_total_clicks(self):
self.get_element(self._total_clicks_url).click()
@staticmethod
def get_clipboard_content():
text = pc.paste()
return text

View File

@@ -0,0 +1,78 @@
# ------- standard library imports -------
import unittest
import multiprocessing
# ------- 3rd party imports -------
from selenium import webdriver
from flask_testing import LiveServerTestCase
# ------- local imports -------
from app.app import create_app
from app.tests.front_end_testing.index.index import Index
from app.tests.front_end_testing.result.result import Result
from app.tests.front_end_testing.total_clicks.total_clicks import TotalClicks
class TestAppSuccess(LiveServerTestCase, unittest.TestCase):
multiprocessing.set_start_method("fork")
def create_app(self):
app = create_app(config_file='tests/front_end_testing/settings.py')
app.testing = True
app.config.update(LIVESERVER_PORT=5002)
return app
@classmethod
def setUpClass(cls):
cls.chrome_browser = webdriver.Chrome()
def test(self):
try:
"""Test the index page."""
self.chrome_browser.get(self.get_server_url())
index = Index(self.chrome_browser)
# test that empty input shows error
index.click_shorten_button()
self.assertEqual(index.check_warning_present(), True)
# test that an invalid url redirecrt to error page
index.enter_invalid_url()
index.click_shorten_button()
self.assertEqual(index.get_current_url(), 'error')
# Go back to home page
index.click_try_again()
# check if heading is present
heading = index.get_heading_text()
self.assertEqual(heading,
'ShortMe is a free tool to shorten URLs. Create a short & memorable URL in seconds.')
# test that a valid URL is working
index.enter_valid_url()
index.click_shorten_button()
"""Test the result page once the long URL has been shortened"""
result = Result(self.chrome_browser)
# test that the short URL exist inside the input element
short_url = result.get_input_text()
self.assertIsNotNone(short_url)
# test that the copy button works
result.click_copy_button()
clipboard_content = result.get_clipboard_content()
self.assertEqual(clipboard_content, short_url)
# go to the total_clicks page
result.go_to_total_clicks()
total_clicks = TotalClicks(self.chrome_browser)
# test that the text matches the expected
p_text = total_clicks.get_total_paragraph_text()
self.assertEqual(p_text, 'Your URL has been clicked 0 times so far.')
finally:
self.chrome_browser.quit()
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,12 @@
from app.tests.utilities import selenium_utility
class TotalClicks(selenium_utility.SeleniumUtility):
_url_input_locator = '//p[@id="total-p"]'
def __init__(self, driver):
self.driver = driver
super().__init__(driver)
def get_total_paragraph_text(self):
return self.get_element(self._url_input_locator).text