import allure
import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from common.read_config import ReadConfig
from test_login.page_login import PageLogin
from common.verifycode import VerifyCode
from common import constant
from common.logger import log



@allure.feature('登录模块')
class TestLogin(object):   
    @pytest.fixture(scope='function', autouse=True)
    def housework(self, browser):
        # 驱动器从fixture传来
        self.driver = browser
        # 执行案例前统一获取页面元素操作方法
        self.page_login = PageLogin(self.driver)
        # 从配置文件中获取url
        self.url = ReadConfig().get('HOST', 'URL')
        self.page_login.open_page(self.url)

    """登录成功案例"""

    @allure.story('登录成功案例-点击登录')
    def test_login_success_click(self):
        # 获取账号输入元素
        account_element = self.page_login.get_account()
        # 密码
        password_element = self.page_login.get_password()
        # 验证码
        verify_input_element = self.page_login.get_verify_code()
        # 输入账号密码，账号密码从配置文件中来
        self.page_login.input_text(account_element, ReadConfig().get('ACCOUNT-PURCHASE', 'ACCOUNT'))
        self.page_login.input_text(password_element, ReadConfig().get('ACCOUNT-PURCHASE', 'PASSWORD'))
        # 获取验证码
        # 先获取验证码图片元素
        verify_code_element = self.page_login.get_verify_image()
        # 获取验证码中的文字信息
        verify_code_text = VerifyCode(verify_code_element, self.driver).indentify_verify_image_guke()
        # 填充验证码文字
        self.page_login.input_text(verify_input_element, verify_code_text)
        # 获取登录按钮
        login_button_element = self.page_login.get_login_button()
        # 点击登录
        self.page_login.single_click(login_button_element)
        # 判断登录是否成功，登录成功会切换到另外一个页面，通过页面title来判断是否切换成功
        assert self.page_login.get_window_title() == constant.LOGIN_SUCCESS

    '''验证码错误案例'''

    @allure.story('登录验证码错误案例')
    def test_login_verify_code_error(self):
        # 获取账号输入元素
        account_element = self.page_login.get_account()
        # 密码
        password_element = self.page_login.get_password()
        # 验证码
        verify_input_element = self.page_login.get_verify_code()
        # 输入账号密码，账号密码从配置文件中来
        self.page_login.input_text(account_element, ReadConfig().get('ACCOUNT-PURCHASE', 'ACCOUNT'))
        self.page_login.input_text(password_element, ReadConfig().get('ACCOUNT-PURCHASE', 'PASSWORD'))
        # 获取验证码
        # 输入错误的验证码
        error_code = '1234'
        self.page_login.input_text(verify_input_element, error_code)
        # 获取登录按钮
        login_button_element = self.page_login.get_login_button()
        # 点击登录
        self.page_login.single_click(login_button_element)
        # 获取登录结果
        login_result_element = self.page_login.get_login_result()
        # 断言案例是否执行成功
        assert self.page_login.get_text(login_result_element) == constant.VERIFY_CODE_ERROR

    '''验证码错误案例'''

    @allure.story('不输入用户名报错案例')
    def test_login_no_input_userid(self):
        # 密码
        password_element = self.page_login.get_password()
        # 验证码
        verify_input_element = self.page_login.get_verify_code()
        # 输入密码，密码从配置文件中来
        self.page_login.input_text(password_element, ReadConfig().get('ACCOUNT-PURCHASE', 'PASSWORD'))
        # 获取验证码
        # 先获取验证码图片元素
        verify_code_element = self.page_login.get_verify_image()
        # 获取验证码中的文字信息
        verify_code_text = VerifyCode(verify_code_element, self.driver).indentify_verify_image_guke()
        # 填充验证码文字
        self.page_login.input_text(verify_input_element, verify_code_text)
        # 获取登录按钮
        login_button_element = self.page_login.get_login_button()
        # 点击登录
        self.page_login.single_click(login_button_element)
        # 获取登录结果
        login_result_element = self.page_login.get_login_result()
        # 断言案例是否执行成功
        assert self.page_login.get_text(login_result_element) == constant.NO_USER_OR_PASSWORD

    @allure.story('不输入密码报错案例')
    def test_login_no_input_password(self):
        # 获取账号输入元素
        account_element = self.page_login.get_account()
        # 验证码
        verify_input_element = self.page_login.get_verify_code()
        # 输入账号密码，账号密码从配置文件中来
        self.page_login.input_text(account_element, ReadConfig().get('ACCOUNT-PURCHASE', 'ACCOUNT'))
        # 获取验证码
        # 先获取验证码图片元素
        verify_code_element = self.page_login.get_verify_image()
        # 获取验证码中的文字信息
        verify_code_text = VerifyCode(verify_code_element, self.driver).indentify_verify_image_guke()
        # 填充验证码文字
        self.page_login.input_text(verify_input_element, verify_code_text)
        # 获取登录按钮
        login_button_element = self.page_login.get_login_button()
        # 点击登录
        self.page_login.single_click(login_button_element)
        # 获取登录结果
        login_result_element = self.page_login.get_login_result()
        # 断言案例是否执行成功
        assert self.page_login.get_text(login_result_element) == constant.NO_USER_OR_PASSWORD

    @allure.story('不输入验证码案例')
    def test_login_no_input_verifycode(self):
        # 获取账号输入元素
        account_element = self.page_login.get_account()
        # 密码
        password_element = self.page_login.get_password()
        # 输入账号密码，账号密码从配置文件中来
        self.page_login.input_text(account_element, ReadConfig().get('ACCOUNT-PURCHASE', 'ACCOUNT'))
        self.page_login.input_text(password_element, ReadConfig().get('ACCOUNT-PURCHASE', 'PASSWORD'))
        # 获取登录按钮
        login_button_element = self.page_login.get_login_button()
        # 点击登录
        self.page_login.single_click(login_button_element)
        # 获取登录结果
        login_result_element = self.page_login.get_login_result()
        # 断言案例是否执行成功
        assert self.page_login.get_text(login_result_element) == constant.NO_VERIFY_CODE

    @allure.story('用户名错误案例')
    def test_login_userid_error(self):
        # 获取账号输入元素
        account_element = self.page_login.get_account()
        # 密码
        password_element = self.page_login.get_password()
        # 验证码
        verify_input_element = self.page_login.get_verify_code()
        # 输入账号密码，账号密码从配置文件中来
        self.page_login.input_text(account_element, ReadConfig().get('ACCOUNT-PURCHASE', 'ACCOUNT') + "ERRORTEST")
        self.page_login.input_text(password_element, ReadConfig().get('ACCOUNT-PURCHASE', 'PASSWORD'))
        # 获取验证码
        # 先获取验证码图片元素
        verify_code_element = self.page_login.get_verify_image()
        # 获取验证码中的文字信息
        verify_code_text = VerifyCode(verify_code_element, self.driver).indentify_verify_image_guke()
        # 填充验证码文字
        self.page_login.input_text(verify_input_element, verify_code_text)
        # 获取登录按钮
        login_button_element = self.page_login.get_login_button()
        # 点击登录
        self.page_login.single_click(login_button_element)
        # 获取登录结果
        login_result_element = self.page_login.get_login_result()
        # 断言案例是否执行成功
        assert self.page_login.get_text(login_result_element) == constant.USER_PASSWORD_ERROR

    '''密码错误案例'''
    @allure.story('密码错误案例')
    def test_login_password_error(self):
        # 获取账号输入元素
        account_element = self.page_login.get_account()
        # 密码
        password_element = self.page_login.get_password()
        # 验证码
        verify_input_element = self.page_login.get_verify_code()
        # 输入账号密码，账号密码从配置文件中来
        self.page_login.input_text(account_element, ReadConfig().get('ACCOUNT-PURCHASE', 'ACCOUNT'))
        self.page_login.input_text(password_element, ReadConfig().get('ACCOUNT-PURCHASE', 'PASSWORD') + "ERRORTEST")
        # 获取验证码
        # 先获取验证码图片元素
        verify_code_element = self.page_login.get_verify_image()
        # 获取验证码中的文字信息
        verify_code_text = VerifyCode(verify_code_element, self.driver).indentify_verify_image_guke()
        # 填充验证码文字
        self.page_login.input_text(verify_input_element, verify_code_text)
        # 获取登录按钮
        login_button_element = self.page_login.get_login_button()
        # 点击登录
        self.page_login.single_click(login_button_element)
        # 获取登录结果
        login_result_element = self.page_login.get_login_result()
        # 断言案例是否执行成功
        assert constant.USER_PASSWORD_ERROR in self.page_login.get_text(login_result_element)
