import os
import time
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import WebDriverException
from django.contrib.staticfiles.testing import StaticLiveServerTestCase


class HomeTests(StaticLiveServerTestCase):
    fixtures = ['accounts.json', 'products.json']

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.browser = WebDriver()

    @classmethod
    def tearDownClass(cls):
        cls.browser.quit()
        super().tearDownClass()

    def test_home_page_has_product_list(self):
        self.login()
        self.assertTrue(
            self.browser.find_element_by_css_selector('.product__list')
        )

    def login(self):
        self.browser.get('%s%s' % (self.live_server_url, '/accounts/login/'))
        username_input = self.browser.find_element_by_name("login")
        username_input.send_keys('john@example.com')
        password_input = self.browser.find_element_by_name("password")
        password_input.send_keys('Bf25XBdP4vdt2X9L')
        self.browser.find_element_by_xpath('//input[@value="Login"]').click()
