# Path: testapp/app/app/tests/utils.py
# class element_has_css_class(object):
#     """An expectation for checking that an element has a particular css class.
#     locator - used to find the element
#     returns the WebElement once it has the particular css class
#     """
# 
#     def __init__(self, locator, css_class):
#         self.locator = locator
#         self.css_class = css_class
# 
#     def __call__(self, driver):
#         element = driver.find_element(*self.locator)   # Finding the referenced element
#         if self.css_class in element.get_attribute("class"):
#             return element
#         else:
#             return False


import time
import os
from django.test import TestCase
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from .utils import element_has_css_class

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

    def login(self):
        self.driver.get('http://localhost:8000/admin')
        username_field = self.driver.find_element_by_id("id_username")
        password_field = self.driver.find_element_by_id("id_password")
        button = self.driver.find_element_by_css_selector('input[type=submit]')

        username_field.send_keys('admin')
        time.sleep(1)
        password_field.send_keys('admin')
        time.sleep(1)
        button.click()

    # selenium sees the navbar as visible because it's just moved to the left,
    # we cannot use is_displayed method
    def navbar_is_invisible(self, navbar):
        left = int(navbar.value_of_css_property('left').replace('px', ''))
        width = int(navbar.value_of_css_property('width').replace('px', ''))
        return left + width <= 0

    def navbar_is_visible(self, navbar):
        left = int(navbar.value_of_css_property('left').replace('px', ''))
        return left == 0

    def test_menu(self):
        # Wait until baton is ready
        wait = WebDriverWait(self.driver, 10)

# Path: testapp/app/app/tests/utils.py
# class element_has_css_class(object):
#     """An expectation for checking that an element has a particular css class.
#     locator - used to find the element
#     returns the WebElement once it has the particular css class
#     """
# 
#     def __init__(self, locator, css_class):
#         self.locator = locator
#         self.css_class = css_class
# 
#     def __call__(self, driver):
#         element = driver.find_element(*self.locator)   # Finding the referenced element
#         if self.css_class in element.get_attribute("class"):
#             return element
#         else:
#             return False


# Path: testapp/app/app/tests/test_e2e_menu_mobile.py
import time
import os
from django.test import TestCase
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from .utils import element_has_css_class


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

    def login(self):
        self.driver.get('http://localhost:8000/admin')
        username_field = self.driver.find_element_by_id("id_username")
        password_field = self.driver.find_element_by_id("id_password")
        button = self.driver.find_element_by_css_selector('input[type=submit]')

        username_field.send_keys('admin')
        time.sleep(1)
        password_field.send_keys('admin')
        time.sleep(1)
        button.click()

    # selenium sees the navbar as visible because it's just moved to the left,
    # we cannot use is_displayed method
    def navbar_is_invisible(self, navbar):
        left = int(navbar.value_of_css_property('left').replace('px', ''))
        width = int(navbar.value_of_css_property('width').replace('px', ''))
        return left + width <= 0

    def navbar_is_visible(self, navbar):
        left = int(navbar.value_of_css_property('left').replace('px', ''))
        return left == 0

    def test_menu(self):
        # Wait until baton is ready
        wait = WebDriverWait(self.driver, 10)

        wait.until(element_has_css_class((By.TAG_NAME, 'body'), "baton-ready"))