import pytest
import time
import sys

# sys.path.append('../..')
from engine import get_engine

project_name = "TestLogin"
# logger = Logger(project_name).get_logger()
'''
def setup_function():
    print('setup_function:每个用例开始前都会执行')

def teardown_function():
    print('teardown_function:每个用例结束后都会执行')
'''


@pytest.mark.login
@get_engine(__file__, 'LOGIN')
def test_login(engine):
    username = engine.get_userInfo('username')
    password = engine.get_userInfo('password')
    url = engine.get_testUrl('URL')
    print(username, password, url)
    engine.logger.info("username: %s" % username)
    engine.logger.info("password :%s" % password)
    engine.logger.info("Open url: %s" % url)

    tags = engine.driver.find_element_by_xpath('//*[@id="root"]/div/div/div[2]/form/div[1]/div/div/span/span/span')
    # print(tags.text)
    engine.logger.info("tags.text:%s" % tags.text)
    tags.click()
    time.sleep(1)

    engine.driver.find_element_by_xpath('//*[@id="rc-tree-select-list_1"]/ul/li/ul/li[3]/span[1]').click()

    tags2 = engine.driver.find_element_by_xpath('//*[@id="rc-tree-select-list_1"]/ul/li/ul/li[3]/ul/li[3]/span[2]/span')
    # print(tags2.text)
    engine.logger.info("tags2.text:%s" % tags2.text)
    tags2.click()
    time.sleep(1)

    engine.driver.find_element_by_xpath('//*[@id="username"]').send_keys(username)
    engine.driver.find_element_by_xpath('//*[@id="password"]').send_keys(password)
    engine.driver.find_element_by_xpath('//*[@id="root"]/div/div/div[2]/form/div[4]/div/div/span/button').click()

    time.sleep(2)
    # engine.driver.quit()


@pytest.mark.login
@get_engine(__file__, 'LOGIN')
def test_login_pwerr(engine):
    username = engine.get_userInfo('username')
    password = engine.get_userInfo('password')
    url = engine.get_testUrl('URL')
    # print(username, password, url)
    engine.logger.info("username: %s" % username)
    engine.logger.info("password :%s" % password)
    engine.logger.info("Open url: %s" % url)

    # driver = engine.driver

    tags = engine.driver.find_element_by_xpath('//*[@id="root"]/div/div/div[2]/form/div[1]/div/div/span/span/span')
    # print(tags.text)
    engine.logger.info("tags.text:%s" % tags.text)
    tags.click()
    time.sleep(1)

    engine.driver.find_element_by_xpath('//*[@id="rc-tree-select-list_1"]/ul/li/ul/li[3]/span[1]').click()

    tags2 = engine.driver.find_element_by_xpath('//*[@id="rc-tree-select-list_1"]/ul/li/ul/li[3]/ul/li[3]/span[2]/span')
    # print(tags2.text)
    engine.logger.info("tags2.text:%s" % tags2.text)
    tags2.click()
    time.sleep(1)
    # 不输入密码
    time1 = time.time()
    form_explain_list = engine.driver.xpath('//div[@class="ant-row ant-form-item ant-form-item-with-help"]//div[@class="ant-form-explain"]')
    alert_message_list = engine.driver.xpath('//div[@class="main___2iu6s"]//span[@class="ant-alert-message"]')
    assert form_explain_list ==[]
    assert alert_message_list ==[]
    engine.load_image()
    engine.driver.xpath('//*[@id="username"]')[0].send_keys(username)
    # engine.driver.find_element_by_xpath('//*[@id="password"]').send_keys('123')
    engine.driver.xpath('//*[@id="root"]/div/div/div[2]/form/div[4]/div/div/span/button')[0].click()
    form_explain = engine.driver.xpath('//div[@class="ant-form-explain"]')[0].text
    assert form_explain.strip() == '请输入密码！'
    engine.logger.info('test_login_password_err:' + '不输入密码，返回正确的错误信息,text:' + form_explain)

    # 输入错误的密码
    engine.driver.find_element_by_xpath('//*[@id="password"]').send_keys('test_gt_pw')
    engine.driver.find_element_by_xpath('//*[@id="root"]/div/div/div[2]/form/div[4]/div/div/span/button').click()
    alert_message = engine.driver.xpath('//div[@class="main___2iu6s"]//span[@class="ant-alert-message"]')[0].text
    assert alert_message.strip() == '账户或密码错误'
    engine.logger.info('test_login_password_err:' + '输入错误密码，返回正确的错误信息,text:' + alert_message)


@pytest.mark.login
@get_engine(__file__, 'LOGIN')
def test_login_usererr(engine):
    username = engine.get_userInfo('username')
    password = engine.get_userInfo('password')
    url = engine.get_testUrl('URL')
    # print(username, password, url)
    engine.logger.info("username: %s" % username)
    engine.logger.info("password :%s" % password)
    engine.logger.info("Open url: %s" % url)

    # driver = engine.driver

    tags = engine.driver.find_element_by_xpath('//*[@id="root"]/div/div/div[2]/form/div[1]/div/div/span/span/span')
    # print(tags.text)
    engine.logger.info("tags.text:%s" % tags.text)
    tags.click()
    time.sleep(1)

    engine.driver.find_element_by_xpath('//*[@id="rc-tree-select-list_1"]/ul/li/ul/li[3]/span[1]').click()

    tags2 = engine.driver.find_element_by_xpath('//*[@id="rc-tree-select-list_1"]/ul/li/ul/li[3]/ul/li[3]/span[2]/span')
    # print(tags2.text)
    engine.logger.info("tags2.text:%s" % tags2.text)
    tags2.click()
    time.sleep(1)
    # 不输入密码
    assert engine.driver.xpath('//div[@class="ant-form-explain"]') == []
    assert engine.driver.xpath('//div[@class="main___2iu6s"]//span[@class="ant-alert-message"]') == []
    # engine.driver.find_element_by_xpath('//*[@id="username"]').send_keys(username)
    engine.driver.find_element_by_xpath('//*[@id="password"]').send_keys(password)
    engine.driver.find_element_by_xpath('//*[@id="root"]/div/div/div[2]/form/div[4]/div/div/span/button').click()
    form_explain = engine.driver.xpath('//div[@class="ant-form-explain"]')[0].text
    assert form_explain.strip() == '请输入账户名！'
    engine.logger.info('test_login_password_err:' + '不输入账户，返回正确的错误信息,text:' + form_explain)

    # 输入错误的密码
    engine.driver.find_element_by_xpath('//*[@id="username"]').send_keys('test_gt_user')
    engine.driver.find_element_by_xpath('//*[@id="root"]/div/div/div[2]/form/div[4]/div/div/span/button').click()
    alert_message = engine.driver.xpath('//div[@class="main___2iu6s"]//span[@class="ant-alert-message"]')[0].text
    assert alert_message.strip() == '账户或密码错误'
    engine.logger.info('test_login_password_err:' + '输入错误账户，返回正确的错误信息,text:' + alert_message)


# if __name__ == '__main__':
#     pytest.main(["-m=login", "--html=report.html"])
