<commit_before>from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

import unittest
import os
import re
import time

dd_driver = None
dd_driver_options = None


class BaseTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        global dd_driver
        if not dd_driver:
            # setupModule and tearDownModule are not working in our scenario, so for now we use setupClass and a global variable
            # global variables are dirty, but in unit tests scenario's like these they are acceptable
            print('launching browser for: ', cls.__name__)
            global dd_driver_options
            dd_driver_options = Options()

            # headless means no UI, if you want to see what is happening remove headless. Adding detach will leave the window open after the test
            dd_driver_options.add_argument("--headless")
            # dd_driver_options.add_experimental_option("detach", True)

            # the next 2 maybe needed in some scenario's for example on WSL or other headless situations
            dd_driver_options.add_argument("--no-sandbox")
            # dd_driver_options.add_argument("--disable-dev-shm-usage")

            # start maximized or at least with sufficient with because datatables will hide certain controls when the screen is too narrow
            dd_driver_options.add_argument("--window-size=1280,1024")
            # dd_driver_options.add_argument("--start-maximized")

            dd_driver_options.set_capability("acceptInsecureCerts", True)

            # some extra logging can be turned on if you want to query the browser javascripe console in your tests
            desired = webdriver.DesiredCapabilities.CHROME
            desired['goog:loggingPrefs'] = {'browser': 'ALL'}

            # change path of chromedriver according to which directory you have chromedriver.
            print('starting chromedriver with options: ', vars(dd_driver_options), desired)
            dd_driver = webdriver.Chrome('chromedriver', chrome_options=dd_driver_options, desired_capabilities=desired)
            dd_driver.implicitly_wait(30)

        cls.driver = dd_driver
        cls.base_url = os.environ['DD_BASE_URL']

    def setUp(self):
        self.verificationErrors = []
        self.accept_next_alert = True
        self.accept_javascript_errors = False
        self.driver.execute_script("console.clear()")
        # clear browser console logs?

    def login_page(self):
        driver = self.driver
        driver.get(self.base_url + "login")
        driver.find_element_by_id("id_username").clear()
        driver.find_element_by_id("id_username").send_keys(os.environ['DD_ADMIN_USER'])
        driver.find_element_by_id("id_password").clear()
        driver.find_element_by_id("id_password").send_keys(os.environ['DD_ADMIN_PASSWORD'])
        driver.find_element_by_css_selector("button.btn.btn-success").click()
        text = driver.find_element_by_tag_name("BODY").text
        self.assertFalse(re.search(r'Please enter a correct username and password', text))
        return driver

    def goto_product_overview(self, driver):
        driver.get(self.base_url + "product")
        self.wait_for_datatable_if_content("no_products", "products_wrapper")

    def goto_active_engagements_overview(self, driver):
        # return self.goto_engagements_internal(driver, 'engagement')
        # engagement overview doesn't seem to have the datatables yet modifying the DOM
        # https://github.com/DefectDojo/django-DefectDojo/issues/2173
        driver.get(self.base_url + 'engagement')
        return driver

    def goto_all_engagements_overview(self, driver):
        return self.goto_engagements_internal(driver, 'engagements_all')

    def goto_engagements_internal(self, driver, rel_url):
        driver.get(self.base_url + rel_url)
        self.wait_for_datatable_if_content("no_engagements", "engagements_wrapper")
        return driver

    def goto_all_findings_list(self, driver):
        driver.get(self.base_url + "finding")
        self.wait_for_datatable_if_content("no_findings", "open_findings_wrapper")

    def wait_for_datatable_if_content(self, no_content_id, wrapper_id):
        no_content = None
        try:
            no_content = self.driver.find_element_by_id(no_content_id)
        except:
            pass

        if no_content is None:
            # wait for product_wrapper div as datatables javascript modifies the DOM on page load.
            WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.ID, wrapper_id)))

    def change_system_setting(self, id, enable=True):
        # we set the admin user (ourselves) to have block_execution checked
        # this will force dedupe to happen synchronously as the celeryworker is not reliable in travis
        print("changing system setting " + id + " enable: " + str(enable))
        driver = self.login_page()
        driver.get(self.base_url + 'system_settings')

        is_enabled = driver.find_element_by_id(id).is_selected()
        if (enable and not is_enabled) or (not enable and is_enabled):
            # driver.find_element_by_xpath('//*[@id=' + id + ']').click()
            driver.find_element_by_id(id).click()
            # save settings
            driver.find_element_by_css_selector("input.btn.btn-primary").click()
            # check if it's enabled after reload

        is_enabled = driver.find_element_by_id(id).is_selected()

        if enable:
            self.assertTrue(is_enabled)

        if not enable:
            self.assertFalse(is_enabled)

        return is_enabled

    def enable_system_setting(self, id):<commit_msg>refactor datatables wait logic<commit_after>from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

import unittest
import os
import re
import time

dd_driver = None
dd_driver_options = None


class BaseTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        global dd_driver
        if not dd_driver:
            # setupModule and tearDownModule are not working in our scenario, so for now we use setupClass and a global variable
            # global variables are dirty, but in unit tests scenario's like these they are acceptable
            print('launching browser for: ', cls.__name__)
            global dd_driver_options
            dd_driver_options = Options()

            # headless means no UI, if you want to see what is happening remove headless. Adding detach will leave the window open after the test
            dd_driver_options.add_argument("--headless")
            # dd_driver_options.add_experimental_option("detach", True)

            # the next 2 maybe needed in some scenario's for example on WSL or other headless situations
            dd_driver_options.add_argument("--no-sandbox")
            # dd_driver_options.add_argument("--disable-dev-shm-usage")

            # start maximized or at least with sufficient with because datatables will hide certain controls when the screen is too narrow
            dd_driver_options.add_argument("--window-size=1280,1024")
            # dd_driver_options.add_argument("--start-maximized")

            dd_driver_options.set_capability("acceptInsecureCerts", True)

            # some extra logging can be turned on if you want to query the browser javascripe console in your tests
            desired = webdriver.DesiredCapabilities.CHROME
            desired['goog:loggingPrefs'] = {'browser': 'ALL'}

            # change path of chromedriver according to which directory you have chromedriver.
            print('starting chromedriver with options: ', vars(dd_driver_options), desired)
            dd_driver = webdriver.Chrome('chromedriver', chrome_options=dd_driver_options, desired_capabilities=desired)
            dd_driver.implicitly_wait(30)

        cls.driver = dd_driver
        cls.base_url = os.environ['DD_BASE_URL']

    def setUp(self):
        self.verificationErrors = []
        self.accept_next_alert = True
        self.accept_javascript_errors = False
        self.driver.execute_script("console.clear()")
        # clear browser console logs?

    def login_page(self):
        driver = self.driver
        driver.get(self.base_url + "login")
        driver.find_element_by_id("id_username").clear()
        driver.find_element_by_id("id_username").send_keys(os.environ['DD_ADMIN_USER'])
        driver.find_element_by_id("id_password").clear()
        driver.find_element_by_id("id_password").send_keys(os.environ['DD_ADMIN_PASSWORD'])
        driver.find_element_by_css_selector("button.btn.btn-success").click()
        text = driver.find_element_by_tag_name("BODY").text
        self.assertFalse(re.search(r'Please enter a correct username and password', text))
        return driver

    def goto_product_overview(self, driver):
        driver.get(self.base_url + "product")
        self.wait_for_datatable_if_content("no_products", "products_wrapper")

    def goto_active_engagements_overview(self, driver):
        # return self.goto_engagements_internal(driver, 'engagement')
        # engagement overview doesn't seem to have the datatables yet modifying the DOM
        # https://github.com/DefectDojo/django-DefectDojo/issues/2173
        driver.get(self.base_url + 'engagement')
        return driver

    def goto_all_engagements_overview(self, driver):
        return self.goto_engagements_internal(driver, 'engagements_all')

    def goto_engagements_internal(self, driver, rel_url):
        driver.get(self.base_url + rel_url)
        self.wait_for_datatable_if_content("no_engagements", "engagements_wrapper")
        return driver

    def goto_all_findings_list(self, driver):
        driver.get(self.base_url + "finding")
        self.wait_for_datatable_if_content("no_findings", "open_findings_wrapper")

    def wait_for_datatable_if_content(self, no_content_id, wrapper_id):
        no_content = None
        try:
            no_content = self.driver.find_element_by_id(no_content_id)
        except:
            pass

        if no_content is None:
            # wait for product_wrapper div as datatables javascript modifies the DOM on page load.
            WebDriverWait(self.driver, 30).until(EC.presence_of_element_located((By.ID, wrapper_id)))

    def change_system_setting(self, id, enable=True):
        # we set the admin user (ourselves) to have block_execution checked
        # this will force dedupe to happen synchronously as the celeryworker is not reliable in travis
        print("changing system setting " + id + " enable: " + str(enable))
        driver = self.login_page()
        driver.get(self.base_url + 'system_settings')

        is_enabled = driver.find_element_by_id(id).is_selected()
        if (enable and not is_enabled) or (not enable and is_enabled):
            # driver.find_element_by_xpath('//*[@id=' + id + ']').click()
            driver.find_element_by_id(id).click()
            # save settings
            driver.find_element_by_css_selector("input.btn.btn-primary").click()
            # check if it's enabled after reload

        is_enabled = driver.find_element_by_id(id).is_selected()

        if enable:
            self.assertTrue(is_enabled)

        if not enable:
            self.assertFalse(is_enabled)

        return is_enabled

    def enable_system_setting(self, id):
{'fraction_non_alphanumeric': 0.08310556884181756, 'fraction_numerical': 0.0029039972668261018, 'mean_word_length': 3.10986310986311, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7215504', 'n_tokens_mistral': 3411, 'n_tokens_neox': 3250, 'n_words': 804}