def test_srd_sd_weakness_extendedsearchtab_tc(self):
        driver = self.driver

        driver.get(self.base_url + "/search.php?extended&tree")
        el = driver.find_element_by_xpath("//select[@id='flaw_sel']")
        common_sd_methods.select_option_dropdow(el, "--+ CWE-843: Access of Resource Using Incompatible Type (Type Confusion)")
        numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
        self.verify_weakness(driver, numOfTestCases, "CWE-843: Access of Resource Using Incompatible Type (Type Confusion)")
 
        driver.get(self.base_url + "/search.php?extended&tree")
        el = driver.find_element_by_xpath("//select[@id='flaw_sel']")
        common_sd_methods.select_option_dropdow(el, "--+ CWE-835: Loop with Unreachable Exit Condition (Infinite Loop)")
        numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
        self.verify_weakness(driver, numOfTestCases, "CWE-835: Loop with Unreachable Exit Condition (Infinite Loop)")

        driver.get(self.base_url + "/search.php?extended&tree")
        el = driver.find_element_by_xpath("//select[@id='flaw_sel']")
        common_sd_methods.select_option_dropdow(el, "--+ CWE-506: Embedded Malicious Code")
        numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
        self.verify_weakness(driver, numOfTestCases, "CWE-506: Embedded Malicious Code")

        driver.get(self.base_url + "/search.php?extended&tree")
        el = driver.find_element_by_xpath("//select[@id='flaw_sel']")
        common_sd_methods.select_option_dropdow(el, "--+ CWE-170: Improper Null Termination")
        numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
        self.verify_weakness(driver, numOfTestCases, "CWE-170: Improper Null Termination")

        driver.get(self.base_url + "/search.php?extended&tree")
        el = driver.find_element_by_xpath("//select[@id='flaw_sel']")
        common_sd_methods.select_option_dropdow(el, "--+ CWE-336: Same Seed in PRNG")
        numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
        self.verify_weakness(driver, numOfTestCases, "CWE-336: Same Seed in PRNG")
    def test_srd_sd_code_complexity_extendedsearchtab_tc(self):
        ccplx = [
            "address alias level",
            "array address complexity",
            "array address complexity",
            "array length/limit complexity",
            "asynchronous",
            "bound violation",
            "buffer address type",
            "container",
            "data type",
            "index alias level",
            "local control flow",
            "loop complexity",
            "loop structure",
            "memory access",
            "memory location",
            "None/Other",
            "overflow magnitude",
            "overflow type",
            "pointer",
        ]

        for i in range(len(ccplx)):
            self.driver.get(self.base_url + "/search.php?extended&tree")
            el = self.driver.find_element_by_xpath("//select[@id='complex_sel']")
            common_sd_methods.select_option_dropdow(el, ccplx[i])
            self.driver.find_element_by_xpath("//input[@name='Submit']").click()
            common_sd_methods.assert_title(self, self.driver, self.verificationErrors)
def insert_test_cases(driver, base_url):
    insertedTestCasesID = []

    i = 0
    #3 is the size of the dropdown_options lists
    while i < 3:
        #inserting test cases
        driver.get(base_url + "/submit.php")
        el = driver.find_element_by_xpath("//select[@name='flawed[]']")
        common_sd_methods.select_option_dropdow(el, dropdown_options.get('flawed')[i])

        el = driver.find_element_by_xpath("//select[@name='languages[]']")
        common_sd_methods.select_option_dropdow(el, dropdown_options.get('languages')[i])

        el = driver.find_element_by_xpath("//select[@name='typesofartefacts[]']")
        common_sd_methods.select_option_dropdow(el, dropdown_options.get('artifact')[i])

        driver.find_element_by_xpath("//input[@name='input']").clear()
        driver.find_element_by_xpath("//input[@name='input']").send_keys("Selenium automated tests")
        driver.find_element_by_xpath("//input[@name='output']").clear()
        driver.find_element_by_xpath("//input[@name='output']").send_keys("Selenium automated tests")
        driver.find_element_by_xpath("//textarea[@name='description']").clear()
        driver.find_element_by_xpath("//textarea[@name='description']").send_keys("Selenium automated tests")

        #if true, include zip files using the 'Directory' tab
        #if i == 2:
        #    driver.find_element_by_xpath("//a[contains(text(),'Directory')]").click()
        #    driver.find_element_by_xpath("//input[@name='archive']").send_keys(os.getcwd() + "/srd_db_tcs/example_files/example_files.zip")
        #    driver.find_element_by_xpath("//a[contains(text(),'Files')]").click()
        #else:
        driver.find_element_by_xpath("//input[@name='file_1']").send_keys(os.getcwd() + "/srd_db_tcs/example_files/example_file_selenium.py")    


        driver.find_element_by_xpath("//input[@name='lines_1']").clear()
        driver.find_element_by_xpath("//input[@name='lines_1']").send_keys(str(random.randint(10, 80)))

        el = driver.find_element_by_xpath("//select[@name='typesofflaws_1[]']")
        common_sd_methods.select_option_dropdow(el, dropdown_options.get('type_flaw')[i])

        el = driver.find_element_by_xpath("//select[@name='ccplx_1[]']")
        common_sd_methods.select_option_dropdow(el, dropdown_options.get('ccplx')[i])

        driver.find_element_by_xpath("//input[@name='Submit']").click()

        time.sleep(3)

        try: insertedTestCasesID.append(re.search('\d+', driver.find_element_by_xpath("//div[@id='okay']").text).group(0))
        except AssertionError as e: verificationErrors.append("Could not find the test case ID!")

        i += 1

    return insertedTestCasesID
    def search_for_tc(self):
        i = 0
        #3 is the size of the dropdown_options lists
        while i < 3:
            self.driver.get(self.base_url + "/search.php?extended&tree")

            #take the test case ID 
            self.driver.find_element_by_xpath("//input[@name='reference']").send_keys("%s" %self.insertedTestCasesID[i])
            
            #insert keywords in the description field
            self.driver.find_element_by_xpath("//input[@name='description']").send_keys("selenium")

            #fill the dropdown menus
            el = self.driver.find_element_by_xpath("//select[@name='flawed[]']")
            common_sd_methods.select_option_dropdow(el, common_db_test_methods.dropdown_options.get('flawed')[i])

            el = self.driver.find_element_by_xpath("//select[@name='languages[]']")
            common_sd_methods.select_option_dropdow(el, common_db_test_methods.dropdown_options.get('languages')[i])

            el = self.driver.find_element_by_xpath("//select[@name='typesofartifacts[]']")
            common_sd_methods.select_option_dropdow(el, common_db_test_methods.dropdown_options.get('artifact')[i])

            el = self.driver.find_element_by_xpath("//select[@id='flaw_sel']")
            common_sd_methods.select_option_dropdow(el, common_db_test_methods.dropdown_options.get('type_flaw')[i])

            el = self.driver.find_element_by_xpath("//select[@id='complex_sel']")
            common_sd_methods.select_option_dropdow(el, common_db_test_methods.dropdown_options.get('ccplx')[i])
            
            self.driver.find_element_by_xpath("//input[@name='Submit']").click()
            time.sleep(3)   

            numberOfTestCases = common_sd_methods.count_test_cases_in_page(self.driver)

            if numberOfTestCases > 0:     
                #verify the search results
                self.driver.find_element_by_xpath("//div[@id='content']/form/table/tbody/tr[2]/td[2]/a").click()
                try: self.assertEqual(self.user, self.driver.find_element_by_xpath("//div[@id='content']/table/tbody/tr[3]/td[2]/span").text)
                except AssertionError as e: self.verificationErrors.append("Test case not found or author is not the same when searching for author '%s'" %(self.author))

                try: self.assertEqual(common_db_test_methods.dropdown_options.get('languages')[i], self.driver.find_element_by_xpath("//div[@id='content']/table/tbody/tr[6]/td[2]/span").text)
                except AssertionError as e: self.verificationErrors.append("Test case not found or language is not the same when searching for language '%s'" %(common_db_test_methods.dropdown_options.get('languages')[i]))
                try: self.assertEqual(common_db_test_methods.dropdown_options.get('artifact')[i], self.driver.find_element_by_xpath("//div[@id='content']/table/tbody/tr[7]/td[2]/span").text)
                except AssertionError as e: self.verificationErrors.append("Test case not found or the artifact is not the same when searching for artifact '%s'" %(common_db_test_methods.dropdown_options.get('artifact')[i]))
                try: self.assertEqual(re.search('\d+', common_db_test_methods.dropdown_options.get('type_flaw')[i]).group(0), re.search('\d+', self.driver.find_element_by_xpath("//div[@id='content']/table/tbody/tr[15]/td[2]/ul/li/span").text).group(0))
                except AssertionError as e: self.verificationErrors.append("Test case not found or the type flaw is not the same when searching for type flaw '%s'" %(common_db_test_methods.dropdown_options.get('type_flaw')[i]))
            else:
                self.verificationErrors.append("Test case not found when searching for the test cases previously inserted!")

                
            i += 1
    def test_srd_sd_dropdown_tc(self):
        driver = self.driver
        
        driver.get(self.base_url + "/search.php?simple")
        el = driver.find_element_by_xpath("//select[@name='flawed[]']")
        common_sd_methods.select_option_dropdow(el, "Bad")

        numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
        self.verify_flawed_element(driver, numOfTestCases, "Bad test case")
        
        if common_sd_methods.go_last_page(driver):
            numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
            self.verify_flawed_element(driver, numOfTestCases, "Bad test case")

        driver.get(self.base_url + "/search.php?simple")
        el = driver.find_element_by_xpath("//select[@name='flawed[]']")
        common_sd_methods.select_option_dropdow(el, "Good")

        numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
        self.verify_flawed_element(driver, numOfTestCases, "Good test case")
        
        if common_sd_methods.go_last_page(driver):
            numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
            self.verify_flawed_element(driver, numOfTestCases, "Good test case")

        driver.get(self.base_url + "/search.php?simple")            
        el = driver.find_element_by_xpath("//select[@name='flawed[]']")
        common_sd_methods.select_option_dropdow(el, "Mixed")

        numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
        self.verify_flawed_element(driver, numOfTestCases, "Mixed test case")
        
        if common_sd_methods.go_last_page(driver):
            numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
            self.verify_flawed_element(driver, numOfTestCases, "Mixed test case")

        driver.get(self.base_url + "/search.php?simple")            
        el = driver.find_element_by_xpath("//select[@name='languages[]']")
        common_sd_methods.select_option_dropdow(el, "C")

        numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
        self.verify_language_element(driver, numOfTestCases, "C")
        
        if common_sd_methods.go_last_page(driver):
            numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
            self.verify_language_element(driver, numOfTestCases, "C")

        driver.get(self.base_url + "/search.php?simple")            
        el = driver.find_element_by_xpath("//select[@name='languages[]']")
        common_sd_methods.select_option_dropdow(el, "Java")

        numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
        self.verify_language_element(driver, numOfTestCases, "Java")
        if common_sd_methods.go_last_page(driver):
            numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
            self.verify_language_element(driver, numOfTestCases, "Java")

        driver.get(self.base_url + "/search.php?simple")            
        el = driver.find_element_by_xpath("//select[@name='languages[]']")
        common_sd_methods.select_option_dropdow(el, "C++")

        numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
        self.verify_language_element(driver, numOfTestCases, "C++")
        
        if common_sd_methods.go_last_page(driver):
            numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
            self.verify_language_element(driver, numOfTestCases, "C++")

        driver.get(self.base_url + "/search.php?simple")            
        el = driver.find_element_by_xpath("//select[@name='languages[]']")
        common_sd_methods.select_option_dropdow(el, "PHP")
        
        numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
        self.verify_language_element(driver, numOfTestCases, "PHP")
        
        if common_sd_methods.go_last_page(driver):
            numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
            self.verify_language_element(driver, numOfTestCases, "PHP")

        driver.get(self.base_url + "/search.php?simple")      
        el = driver.find_element_by_xpath("//select[@name='typesofartifacts[]']")
        common_sd_methods.select_option_dropdow(el, "Source Code")      
        
        numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
        self.verify_artifact_element(driver, numOfTestCases, "Source Code")
        
        if common_sd_methods.go_last_page(driver):
            numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
            self.verify_artifact_element(driver, numOfTestCases, "Source Code")

        driver.get(self.base_url + "/search.php?simple")      
        el = driver.find_element_by_xpath("//select[@name='typesofartifacts[]']")
        common_sd_methods.select_option_dropdow(el, "Mix Of Artifact")   
        
        numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
        self.verify_artifact_element(driver, numOfTestCases, "Mix Of Artifact")
        
        if common_sd_methods.go_last_page(driver):
            numOfTestCases = common_sd_methods.count_test_cases_in_page(driver)
            self.verify_artifact_element(driver, numOfTestCases, "Mix Of Artifact")
    def update_test_cases(self):
        i = 2
        for id in self.insertedTestCasesID:
            self.driver.get(self.base_url + "/view_testcase.php?tID=%s" %id)

            self.driver.find_element_by_id("Flawed").click()
            self.driver.find_element_by_css_selector("input[type='text']").clear()
            self.driver.find_element_by_css_selector("input[type='text']").send_keys(common_db_test_methods.dropdown_options['flawed'][i])
            self.driver.find_element_by_css_selector("input[type='submit']").click()

            self.driver.find_element_by_id("Author").click()
            self.driver.find_element_by_xpath("(//input[@type='text'])[2]").clear()
            self.driver.find_element_by_xpath("(//input[@type='text'])[2]").send_keys("Paul")
            self.driver.find_element_by_xpath("(//input[@value='Save'])[2]").click()

            #if the associated test cases is used, uncomend this code
            #self.driver.find_element_by_id("Associatedtestcase").click()
            #self.driver.find_element_by_xpath("(//input[@type='text'])[3]").clear()
            #self.driver.find_element_by_xpath("(//input[@type='text'])[3]").send_keys("168304")
            #self.driver.find_element_by_xpath("(//input[@value='Save'])[3]").click()

            self.driver.find_element_by_id("Language").click()
            self.driver.find_element_by_xpath("(//input[@type='text'])[4]").clear()
            self.driver.find_element_by_xpath("(//input[@type='text'])[4]").send_keys(common_db_test_methods.dropdown_options['languages'][i])
            self.driver.find_element_by_xpath("(//input[@value='Save'])[4]").click()

            self.driver.find_element_by_id("Inputstring").click()
            self.driver.find_element_by_xpath("(//input[@type='text'])[5]").clear()
            self.driver.find_element_by_xpath("(//input[@type='text'])[5]").send_keys("Updating Selenium automated tests")
            self.driver.find_element_by_xpath("(//input[@value='Save'])[5]").click()
            
            self.driver.find_element_by_id("Expectedoutputstring").click()
            self.driver.find_element_by_xpath("(//input[@type='text'])[6]").clear()
            self.driver.find_element_by_xpath("(//input[@type='text'])[6]").send_keys("Updating Selenium automated tests")
            self.driver.find_element_by_xpath("(//input[@value='Save'])[6]").click()
            
            self.driver.find_element_by_id("Instruction").click()
            self.driver.find_element_by_xpath("(//input[@type='text'])[7]").clear()
            self.driver.find_element_by_xpath("(//input[@type='text'])[7]").send_keys("Updating Selenium automated tests")
            self.driver.find_element_by_xpath("(//input[@value='Save'])[7]").click()
            
            self.driver.find_element_by_id("Description").click()
            self.driver.find_element_by_xpath("//div[@id='content']/table/tbody/tr[13]/td[2]/form/textarea").clear()
            self.driver.find_element_by_xpath("//div[@id='content']/table/tbody/tr[13]/td[2]/form/textarea").send_keys("Updating Selenium automated tests")
            self.driver.find_element_by_xpath("(//input[@value='Save'])[8]").click()
            
            self.driver.find_element_by_xpath("//div[@id='content']/table/tbody/tr[15]/td[2]/ul/li/span").click()
            el = self.driver.find_element_by_xpath("//div[@id='uniqFlawName']/span/form/select")
            common_sd_methods.select_option_dropdow(el, common_db_test_methods.dropdown_options.get('type_flaw')[i])
            self.driver.find_element_by_xpath("//img[@alt='Send']").click()

            self.driver.find_element_by_xpath("//div[@id='content']/table/tbody/tr[15]/td[2]/ul/li/span[2]").click()
            self.driver.find_element_by_xpath("//div[@id='content']/table/tbody/tr[15]/td[2]/ul/li/form/input").clear()
            self.driver.find_element_by_xpath("//div[@id='content']/table/tbody/tr[15]/td[2]/ul/li/form/input").send_keys("45")
            self.driver.find_element_by_xpath("(//input[@value='Save'])[9]").click()

            self.driver.find_element_by_xpath("//div[@id='content']/table/tbody/tr[15]/td[2]/ul/li/span[3]").click()
            el = self.driver.find_element_by_xpath("//div[@id='uniqCCplxName']/span/form/select")
            #is necessary to insert a space at the beginning of the string for this dropdown menu
            common_sd_methods.select_option_dropdow(el, (common_db_test_methods.dropdown_options.get('ccplx')[i]))
            self.driver.find_element_by_xpath("//div[@id='uniqCCplxName']/span/form/a[2]/img").click()

            i = i - 1