from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from .server_tools import reset_database
import sys




class FunctionalTest(StaticLiveServerTestCase):

    @classmethod
    def setUpClass(cls):
        for arg in sys.argv:
            if 'liveserver' in arg:
                cls.server_host=arg.split('=')[1]
                cls.server_url='http://'+cls.server_host
                cls.against_staging=True
                return
        super().setUpClass()
        cls.against_staging=False
        cls.server_url=cls.live_server_url


    @classmethod
    def tearDownClass(cls):
        if not cls.against_staging:
            super().tearDownClass()

    def setUp(self):
        if self.against_staging:
            reset_database(self.server_host)
        self.browser = webdriver.Chrome()
        self.browser.implicitly_wait(3)

    def tearDown(self):
        self.browser.quit()

    def check_for_row_in_list_table(self,row_text):
        table=self.browser.find_element_by_id('id_list_table')
        rows=table.find_elements_by_tag_name('tr')
        self.assertIn(row_text,[row.text for row in rows])


    def get_item_input_box(self):
        return self.browser.find_element_by_id('id_text')


    def assert_logged_in(self,email):
        self.browser.find_element_by_link_text('Log out')
        navbar=self.browser.find_element_by_css_selector('.navbar')
        self.assertIn(email,navbar.text)

    def assert_logged_out(self,email):
        self.browser.find_element_by_name('email')
        navbar=self.browser.find_element_by_css_selector('.navbar')
        self.assertNotIn(email,navbar.text)

