|
| 1 | +from selenium import webdriver |
| 2 | +from selenium.webdriver.support.ui import Select |
| 3 | +import unittest |
| 4 | +import re |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +class ProductTest(unittest.TestCase): |
| 9 | + def setUp(self): |
| 10 | + # Initialize the driver |
| 11 | + self.driver = webdriver.Chrome('chromedriver') |
| 12 | + # Allow a little time for the driver to initialize |
| 13 | + self.driver.implicitly_wait(30) |
| 14 | + # Set the base address of the dojo |
| 15 | + self.base_url = "http://localhost:8080/" |
| 16 | + self.verificationErrors = [] |
| 17 | + self.accept_next_alert = True |
| 18 | + |
| 19 | + def login_page(self): |
| 20 | + # Make a member reference to the driver |
| 21 | + driver = self.driver |
| 22 | + # Navigate to the login page |
| 23 | + driver.get(self.base_url + "login") |
| 24 | + # Good practice to clear the entry before typing |
| 25 | + driver.find_element_by_id("id_username").clear() |
| 26 | + # Set the user to an admin account |
| 27 | + driver.find_element_by_id("id_username").send_keys('admin') |
| 28 | + driver.find_element_by_id("id_password").clear() |
| 29 | + # Use the password unqiue to the container. Info on finding this below |
| 30 | + # https://github.com/DefectDojo/django-DefectDojo/blob/master/DOCKER.md |
| 31 | + driver.find_element_by_id("id_password").send_keys('eE1F6IKz3Uq4rLg6aYtI0k') |
| 32 | + # "Click" the but the login button |
| 33 | + driver.find_element_by_css_selector("button.btn.btn-success").click() |
| 34 | + return driver |
| 35 | + |
| 36 | + def test_create_product(self): |
| 37 | + # Login to the site. Password will have to be modified |
| 38 | + # to match an admin password in your own container |
| 39 | + driver = self.login_page() |
| 40 | + # Navigate to the product page |
| 41 | + driver.get(self.base_url + "product") |
| 42 | + # "Click" the dropdown button to see options |
| 43 | + driver.find_element_by_id("dropdownMenu1").click() |
| 44 | + # "Click" the add prodcut button |
| 45 | + driver.find_element_by_link_text("Add Product").click() |
| 46 | + # Fill in th product name |
| 47 | + driver.find_element_by_id("id_name").clear() |
| 48 | + driver.find_element_by_id("id_name").send_keys("QA Test") |
| 49 | + # Tab into the description area to fill some text |
| 50 | + # Couldnt find a way to get into the box with selenium |
| 51 | + driver.find_element_by_id("id_name").send_keys("\tThis is just a test. Be very afraid.") |
| 52 | + # Select an option in the poroduct type |
| 53 | + Select(driver.find_element_by_id("id_prod_type")).select_by_visible_text("Research and Development") |
| 54 | + # "Click" the submit button to complete the transaction |
| 55 | + driver.find_element_by_css_selector("input.btn.btn-primary").click() |
| 56 | + # Query the site to determine if the product has been added |
| 57 | + productTxt = driver.find_element_by_tag_name("BODY").text |
| 58 | + # Assert ot the query to dtermine status of failure |
| 59 | + self.assertTrue(re.search(r'Product added successfully', productTxt)) |
| 60 | + |
| 61 | + def test_edit_product_title(self): |
| 62 | + # Login to the site. Password will have to be modified |
| 63 | + # to match an admin password in your own container |
| 64 | + driver = self.login_page() |
| 65 | + # Navigate to the product page |
| 66 | + driver.get(self.base_url + "product") |
| 67 | + # "Click" the dropdown option |
| 68 | + driver.find_element_by_class_name("pull-left").click() |
| 69 | + # "Click" the edit option |
| 70 | + driver.find_element_by_link_text("Edit").click() |
| 71 | + # Clear the old product name |
| 72 | + driver.find_element_by_id("id_name").clear() |
| 73 | + # Fill in the product name |
| 74 | + driver.find_element_by_id("id_name").send_keys("EDITED QA Test") |
| 75 | + # "Click" the submit button to complete the transaction |
| 76 | + driver.find_element_by_css_selector("input.btn.btn-primary").click() |
| 77 | + # Query the site to determine if the product has been added |
| 78 | + productTxt = driver.find_element_by_tag_name("BODY").text |
| 79 | + # Assert ot the query to dtermine status of failure |
| 80 | + self.assertTrue(re.search(r'Product updated successfully', productTxt)) |
| 81 | + |
| 82 | + def test_delete_product(self): |
| 83 | + # Login to the site. Password will have to be modified |
| 84 | + # to match an admin password in your own container |
| 85 | + driver = self.login_page() |
| 86 | + # Navigate to the product page |
| 87 | + driver.get(self.base_url + "product") |
| 88 | + # "Click" the dropdown option |
| 89 | + driver.find_element_by_class_name("pull-left").click() |
| 90 | + # "Click" the edit option |
| 91 | + driver.find_element_by_link_text("Delete").click() |
| 92 | + # "Click" the delete button to complete the transaction |
| 93 | + driver.find_element_by_css_selector("button.btn.btn-danger").click() |
| 94 | + # Query the site to determine if the product has been added |
| 95 | + productTxt = driver.find_element_by_tag_name("BODY").text |
| 96 | + # Assert ot the query to dtermine status of failure |
| 97 | + self.assertTrue(re.search(r'Product and relationships removed.', productTxt)) |
| 98 | + |
| 99 | + def tearDown(self): |
| 100 | + self.driver.quit() |
| 101 | + self.assertEqual([], self.verificationErrors) |
| 102 | + |
| 103 | + |
| 104 | +def suite(): |
| 105 | + suite = unittest.TestSuite() |
| 106 | + # Add each test the the suite to be run |
| 107 | + # success and failure is output by the test |
| 108 | + suite.addTest(ProductTest('test_create_product')) |
| 109 | + suite.addTest(ProductTest('test_edit_product_title')) |
| 110 | + suite.addTest(ProductTest('test_delete_product')) |
| 111 | + return suite |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + runner = unittest.TextTestRunner(descriptions=True, failfast=True) |
| 116 | + ret = not runner.run(suite()).wasSuccessful() |
| 117 | + sys.exit(ret) |
0 commit comments