Commit 6fb4aae9 authored by Johannes Tomren Røsvik's avatar Johannes Tomren Røsvik
Browse files

Merge branch '4-write-test-scripts-to-practice-black-box-test-methods' into 'master'

Resolve "Write test scripts to practice black-box test methods"

Closes #4

See merge request !9
parents 3c7418a8 41ea691a
Pipeline #77090 failed with stages
in 1 minute and 47 seconds
......@@ -23,7 +23,8 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '$n%^#g%qx#82w6t^dvjqwv)q*1cy+fwh1ohku7-rbjqcei2^jr'
RECAPTCHA_KEY = '6Ld1AN8UAAAAAGENA-uPsuCVBPydoONxPjGip2fO'
RECAPTCHA_SECRET_KEY = '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe'
RECAPTCHA_SITE_KEY = '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI'
ADMINS = [('APU', 'tdt4242apu@gmail.com')]
......
......@@ -10,7 +10,7 @@
{% csrf_token %}
{% bootstrap_form form %}
<div class="form-group">
<div class="g-recaptcha" data-sitekey="6Ld1AN8UAAAAALCLQJZdnam9MmRaB7Smulf0E5F1"></div>
<div class="g-recaptcha" data-sitekey="{{ captcha_sitekey }}"></div>
</div>
<button type="submit">Sign up</button>
</form>
......
import unittest
import os
from selenium import webdriver
url = "http://tdt4237.herokuapp.com/nonexistentpage/"
class AgreelanceSignUp(unittest.TestCase):
def setUp(self):
if os.name == "nt":
self.driver = webdriver.Chrome("./bin/chromedriver.exe")
else:
self.driver = webdriver.Chrome("./bin/chromedriver")
def test_404_image(self):
driver = self.driver
driver.get(url)
image_element = driver.find_element_by_xpath("/html/body/div/img")
image_source = image_element.get_attribute("src")
self.assertTrue(image_source == "https://248.no/img/cat.gif")
def test_404_text(self):
driver = self.driver
driver.get(url)
header_element = driver.find_element_by_xpath("/html/body/div/h2")
header_text = header_element.text
self.assertTrue(header_text == "Let’s agree to disagree, this page does not exist.")
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import os
import random
import string
from time import sleep
url = "http://127.0.0.1:8000/"
url_login = url + "user/login/"
url_logout = url + "user/logout/"
url_new_project = url + "projects/new/"
url_projects = url + "projects/"
class AgreelanceSignUp(unittest.TestCase):
def setUp(self):
if os.name == "nt":
self.driver = webdriver.Chrome("./bin/chromedriver.exe")
else:
self.driver = webdriver.Chrome("./bin/chromedriver")
def test_offers(self):
self.login("admin", "qwerty123")
project_name_1 = self.rString(20)
project_name_2 = self.rString(20)
project_name_3 = self.rString(20)
self.new_project(project_name_1)
self.new_project(project_name_2)
self.new_project(project_name_3)
self.logout()
self.login("joe", "qwerty123")
self.apply_project(project_name_1, self.rDigits(10))
self.apply_project(project_name_2, self.rDigits(10))
self.apply_project(project_name_3, self.rDigits(10))
self.logout()
self.login("admin", "qwerty123")
self.send_response(project_name_1, 0)
self.send_response(project_name_2, 1)
self.send_response(project_name_3, 2)
print("Accepted " + project_name_1)
print("Pending " + project_name_2)
print("Declined " + project_name_3)
sleep(1)
def login(self, username, password):
driver = self.driver
driver.get(url_login)
driver.find_element_by_id("id_username").send_keys(username)
driver.find_element_by_id("id_password").send_keys(password)
driver.find_element_by_xpath("/html/body/div/form/button").click()
sleep(1)
def send_response(self, project_title, status_index):
driver = self.driver
driver.get(url)
project = driver.find_element_by_xpath("//*[contains(text(), \"" + project_title + "\")]")
project.click()
sleep(1)
driver.find_element_by_xpath("/html/body/div/li/div/button").click()
sleep(1)
Select(driver.find_element_by_id("id_status")).select_by_index(status_index)
driver.find_element_by_id("id_feedback").send_keys("FEEDBACK_TEXT")
project = driver.find_element_by_xpath("//*[contains(text(), \"Send Response\")]")
sleep(1)
def apply_project(self, project_title, price):
driver = self.driver
driver.get(url_projects)
sleep(1)
project = driver.find_element_by_xpath("//*[contains(text(), \"" + project_title + "\")]")
project.click()
sleep(1)
driver.find_element_by_xpath("/html/body/div/ul/li/button").click()
sleep(1)
driver.find_element_by_xpath("//*[@id=\"id_title\"]").send_keys("TASK_TITLE")
driver.find_element_by_xpath("//*[@id=\"id_description\"]").send_keys("TASK_DESCRIPTION")
driver.find_element_by_xpath("//*[@id=\"id_price\"]").send_keys(price)
driver.find_element_by_name("offer_submit").click()
sleep(1)
try:
return "Pending" in driver.find_element_by_xpath("/html/body/div/ul/li/div/button").text
except Exception:
return False
def logout(self):
driver = self.driver
driver.get(url_logout)
sleep(1)
def new_project(self, title):
driver = self.driver
driver.get(url_new_project)
sleep(1)
driver.find_element_by_id("id_title").send_keys(title)
driver.find_element_by_id("id_description").send_keys(title)
Select(driver.find_element_by_id("id_category_id")).select_by_index(1)
driver.find_element_by_xpath("/html/body/div/form/div[4]/div/p[1]/label/input").send_keys("TASK_TITLE")
driver.find_element_by_id("budget_input").send_keys(1000)
driver.find_element_by_xpath("/html/body/div/form/div[4]/div/p[3]/label/textarea").send_keys("TASK_DESCRIPTION")
driver.find_element_by_xpath("/html/body/div/form/div[5]/div[2]/button").click()
sleep(1)
def tearDown(self):
self.driver.close()
@staticmethod
def rString(length):
return ''.join(random.choices(string.ascii_lowercase, k=length))
@staticmethod
def rDigits(length):
return ''.join(random.choices(string.digits, k=length))
if __name__ == "__main__":
unittest.main()
......@@ -35,15 +35,18 @@ class AgreelanceDarkMode(unittest.TestCase):
def test_dark_mode_clicked(self):
driver = self.driver
driver.get(url)
# Try to open navbar in case the screen is small
try:
toggler = driver.find_element_by_class_name("navbar-toggler")
toggler.click()
time.sleep(1)
finally:
darkmode_click = driver.find_element_by_class_name("custom-control-label")
darkmode_click.click()
time.sleep(1)
except Exception:
pass
darkmode_click = driver.find_element_by_class_name("custom-control-label")
darkmode_click.click()
time.sleep(1)
self.assertEqual(driver.find_element_by_tag_name("body").value_of_css_property("background-color"), "rgba(0, 0, 0, 1)")
......@@ -52,4 +55,4 @@ class AgreelanceDarkMode(unittest.TestCase):
self.driver.close()
if __name__ == "__main__":
unittest.main()
\ No newline at end of file
unittest.main()
import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import os
import random
import string
from time import sleep
url = "http://127.0.0.1:8000/"
url_login = url + "user/login/"
url_logout = url + "user/logout/"
url_new_project = url + "projects/new/"
url_projects = url + "projects/"
class AgreelanceSignUp(unittest.TestCase):
def setUp(self):
if os.name == "nt":
self.driver = webdriver.Chrome("./bin/chromedriver.exe")
else:
self.driver = webdriver.Chrome("./bin/chromedriver")
def test_offers(self):
self.login("admin", "qwerty123")
project_name = self.rString(20)
self.new_project(project_name)
self.logout()
self.login("joe", "qwerty123")
self.assertTrue(self.apply_project(project_name, self.rDigits(199))) # max (minus 1)
self.logout()
self.login("harrypotter", "qwerty123")
self.assertTrue(self.apply_project(project_name, self.rDigits(200))) # max+ (minus 1)
def login(self, username, password):
driver = self.driver
driver.get(url_login)
driver.find_element_by_id("id_username").send_keys(username)
driver.find_element_by_id("id_password").send_keys(password)
driver.find_element_by_xpath("/html/body/div/form/button").click()
sleep(1)
def apply_project(self, project_title, price):
driver = self.driver
driver.get(url_projects)
project = driver.find_element_by_xpath("//*[contains(text(), \"" + project_title + "\")]")
project.click()
sleep(1)
driver.find_element_by_xpath("/html/body/div/ul/li/button").click()
sleep(1)
driver.find_element_by_xpath("//*[@id=\"id_title\"]").send_keys("TASK_TITLE")
driver.find_element_by_xpath("//*[@id=\"id_description\"]").send_keys("TASK_DESCRIPTION")
driver.find_element_by_xpath("//*[@id=\"id_price\"]").send_keys(price)
driver.find_element_by_name("offer_submit").click()
sleep(1)
try:
print(driver.find_element_by_xpath("/html/body/div/ul/li/div/button").text)
return "Pending" in driver.find_element_by_xpath("/html/body/div/ul/li/div/button").text
except Exception:
return False
def logout(self):
driver = self.driver
driver.get(url_logout)
sleep(1)
def new_project(self, title):
driver = self.driver
driver.get(url_new_project)
driver.find_element_by_id("id_title").send_keys(title)
driver.find_element_by_id("id_description").send_keys(title)
Select(driver.find_element_by_id("id_category_id")).select_by_index(1)
driver.find_element_by_xpath("/html/body/div/form/div[4]/div/p[1]/label/input").send_keys("TASK_TITLE")
driver.find_element_by_id("budget_input").send_keys(1000)
driver.find_element_by_xpath("/html/body/div/form/div[4]/div/p[3]/label/textarea").send_keys("TASK_DESCRIPTION")
driver.find_element_by_xpath("/html/body/div/form/div[5]/div[2]/button").click()
sleep(1)
def tearDown(self):
self.driver.close()
@staticmethod
def rString(length):
return ''.join(random.choices(string.ascii_lowercase, k=length))
@staticmethod
def rDigits(length):
return ''.join(random.choices(string.digits, k=length))
if __name__ == "__main__":
unittest.main()
# This test will only pass if the site and secret keys are set to
# RECAPTCHA_SITE_KEY = 6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI
# RECAPTCHA_SECRET_KEY = 6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe
# in agreelance/settings.py. See recaptcha docs for more information:
# https://developers.google.com/recaptcha/docs/faq
import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import os
import random
import string
from time import sleep
url = "http://127.0.0.1:8000/user/signup"
class AgreelanceSignUp(unittest.TestCase):
def setUp(self):
if os.name == "nt":
self.driver = webdriver.Chrome("./bin/chromedriver.exe")
else:
self.driver = webdriver.Chrome("./bin/chromedriver")
def test_two_way_postal_code_city(self):
driver = self.driver
values = self.getNominalValues()
combinations = [
["Oslo", "0378", True],
["Oslo", "7018", False],
["Trondheim", "0378", False],
["Trondheim", "7018", True],
]
for combination in combinations:
values["city"] = combination[0]
values["postal_code"] = combination[1]
self.assertTrue(self.case_tester(combination) == combination[2])
def test_two_way_city_country(self):
driver = self.driver
values = self.getNominalValues()
combinations = [
["Oslo", "Norway", True],
["Oslo", "Sweden", False],
["Stockholm", "Norway", False],
["Stockholm", "Sweden", True],
]
for combination in combinations:
values["city"] = combination[0]
values["country"] = combination[1]
self.assertTrue(self.case_tester(combination) == combination[2])
def test_two_way_password_verify(self):
driver = self.driver
values = self.getNominalValues()
combinations = [
["qwerty123", "Qwerty123", False],
["qwerty123", "qwerty123", True],
]
for combination in combinations:
values["password"] = combination[0]
values["password_confirmation"] = combination[1]
self.assertTrue(self.case_tester(combination) == combination[2])
def test_username(self):
testcases = [
["username", "", False], # min-
["username", self.rString(1), True], # min
["username", self.rString(150), True], # max
["username", self.rString(151), False], # max+
]
self.assertTrue(self.case_tester(testcases))
def test_firstname(self):
testcases = [
["firstname", "", False], # min-
["firstname", self.rString(1), True], # min
["firstname", self.rString(30), True], # max
["firstname", self.rString(31), False], # max+
]
self.assertTrue(self.case_tester(testcases))
def test_lastname(self):
testcases = [
["lastname", "", False], # min-
["lastname", self.rString(1), True], # min
["lastname", self.rString(30), True], # max
["lastname", self.rString(31), False], # max+
]
self.assertTrue(self.case_tester(testcases))
def test_categories(self):
testcases = [
["categories", None, False], # min-
]
self.assertTrue(self.case_tester(testcases))
def test_company(self):
testcases = [
["company", "", True], # min
["company", "30", True], # max
["company", "30", False], # max+
]
self.assertTrue(self.case_tester(testcases))
def test_email(self):
email_6 = self.rString(1) + "@" + self.rString(1) + ".co"
email_254 = self.rString(248) + "@" + self.rString(1) + ".com"
email_255 = self.rString(248) + "@" + self.rString(2) + ".com"
testcases = [
["email", "", False], # min-
["email", email_6, True], # min
["email", email_254, True], # max
["email", email_255, False], # max+
]
self.assertTrue(self.case_tester(testcases))
def test_phone_number(self):
testcases = [
["phone_number", "", False], # min-
["phone_number", self.rDigits(1), True], # min
["phone_number", self.rDigits(50), True], # max
["phone_number", self.rDigits(51), False], # max+
]
self.assertTrue(self.case_tester(testcases))
def test_country(self):
testcases = [
["country", "", False], # min-
["country", self.rString(1), True], # min
["country", self.rString(50), True], # max
["country", self.rString(51), False], # max+
]
self.assertTrue(self.case_tester(testcases))
def test_state(self):
testcases = [
["state", "", False], # min-
["state", self.rString(1), True], # min
["state", self.rString(50), True], # max
["state", self.rString(51), False], # max+
]
self.assertTrue(self.case_tester(testcases))
def test_city(self):
testcases = [
["city", "", False], # min-
["city", self.rString(1), True], # min
["city", self.rString(50), True], # max
["city", self.rString(51), False], # max+
]
self.assertTrue(self.case_tester(testcases))
def test_postal_code(self):
testcases = [
["postal_code", "", False], # min-
["postal_code", self.rDigits(1), True], # min
["postal_code", self.rDigits(50), True], # max
["postal_code", self.rDigits(51), False], # max+
]
self.assertTrue(self.case_tester(testcases))
def test_street_address(self):
testcases = [
["street_address", "", False], # min-
["street_address", self.rString(1), True], # min
["street_address", self.rString(50), True], # max
["street_address", self.rString(51), False], # max+
]
self.assertTrue(self.case_tester(testcases))
def case_tester(self, testcases):
driver = self.driver
for case in testcases:
values = self.getNominalValues()
values[case[0]] = case[1]
formstate = self.fill_form(values)
if formstate != case[2]:
print(values)
return False
return True
def fill_form(self, values):
driver = self.driver
driver.get(url)
username = driver.find_element_by_id("id_username")
firstname = driver.find_element_by_id("id_first_name")
lastname = driver.find_element_by_id("id_last_name")
categories = Select(driver.find_element_by_id("id_categories"))
company = driver.find_element_by_id("id_company")
email = driver.find_element_by_id("id_email")
email_confirmation = driver.find_element_by_id("id_email_confirmation")
password = driver.find_element_by_id("id_password1")
password_confirmation = driver.find_element_by_id("id_password2")
phone_number = driver.find_element_by_id("id_phone_number")
country = driver.find_element_by_id("id_country")
state = driver.find_element_by_id("id_state")
city = driver.find_element_by_id("id_city")
postal_code = driver.find_element_by_id("id_postal_code")
street_address = driver.find_element_by_id("id_street_address")
recaptcha = driver.find_element_by_class_name("g-recaptcha")
submit = driver.find_element_by_xpath("/html/body/div[1]/form/button")
username.send_keys(values["username"])
firstname.send_keys(values["firstname"])
lastname.send_keys(values["lastname"])
if values["categories"] != None:
categories.select_by_index(values["categories"])
company.send_keys(values["company"])
email.send_keys(values["email"])
email_confirmation.send_keys(values["email"])
password.send_keys(values["password"])
password_confirmation.send_keys(values["password"])
phone_number.send_keys(values["phone_number"])
country.send_keys(values["country"])
state.send_keys(values["state"])
city.send_keys(values["city"])
postal_code.send_keys(values["postal_code"])
street_address.send_keys(values["street_address"])
# recaptcha.click()
submit.click()
timeout = 1
try:
element_present = EC.presence_of_element_located((By.CLASS_NAME, 'alert'))
WebDriverWait(driver, timeout).until(element_present)
alert = driver.find_element_by_class_name("alert")
return "Your account has been created" in alert.text