• 陈泽健's avatar
    1. 2024-10-21 · ec796de8
    陈泽健 提交于
       - 修改了自动化测试脚本架构,使用PO模式,将共有方法封装在base目录下,任何模块都可以进行调用。
       - 将测试数据csv为文件统一放在测试数据目录下,并标注所属模块功能。
       - 将驱动加载方式改为更加灵活的自动下载方式,避免其他人员使用时手动下载驱动。
       - 补充安卓信息模块的Mqtt主题上报以及接收脚本,但暂时还未与实际mqtt主题进行调试,需要先整理出所有的mqtt主题,再进行代码调试。
    ec796de8
Base.py 5.5 KB
import logging
import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common import StaleElementReferenceException

class Base:
    def __init__(self, driver):
        self.driver = driver
        self.logger = logging.getLogger(__name__)

    def find_element(self, by, value):
        """
        查找元素,并处理可能的异常
        """
        try:
            return WebDriverWait(self.driver, 30).until(
                EC.presence_of_element_located((by, value))
            )
        except StaleElementReferenceException:
            return WebDriverWait(self.driver, 30).until(
                EC.presence_of_element_located((by, value))
            )

    def perform_login(self, username, password, verification_code):
        """
        执行登录操作
        """
        self.driver.implicitly_wait(5)
        self.driver.refresh()

        try:
            # 清除并输入用户名
            username_input = self.find_element(By.XPATH, '//*[@id="app"]/div/div[3]/div[1]/div[2]/div/form/div[1]/div/input')
            username_input.clear()
            username_input.send_keys(username)

            # 清除并输入密码
            password_input = self.find_element(By.XPATH, '//*[@id="app"]/div/div[3]/div[1]/div[2]/div/form/div[2]/div/input')
            password_input.clear()
            password_input.send_keys(password)

            # 清除并输入验证码
            verification_input = self.find_element(By.XPATH, '//*[@id="app"]/div/div[3]/div[1]/div[2]/div/form/div[3]/div[1]/div/input')
            verification_input.clear()
            verification_input.send_keys(verification_code)

            # 点击登录按钮
            login_button = self.find_element(By.XPATH, '//*[@id="app"]/div/div[3]/div[1]/div[2]/div/form/div[4]/input')
            login_button.click()

            # 检查登录是否成功
            if self.check_login_null():
                self.logger.info("登录失败,必填项不能为空!断言成功!")
            elif self.check_login_success():
                self.logger.info("登录成功,断言成功!")
                self.return_to_login_page()
            elif self.check_login_failure():
                self.logger.info("登录失败, 必填项错误!断言成功!")
            else:
                self.logger.error("登录状态未知,断言失败!")
                pytest.fail("登录状态未知,断言失败!")
        except StaleElementReferenceException as e:
            self.logger.error(f"元素已失效,重新查找... {e}")
            pytest.fail(f"元素已失效,重新查找... {e}")
        except Exception as e:
            self.logger.error(f"登录操作失败: {e}")
            pytest.fail(f"登录操作失败: {e}")

    def return_to_login_page(self):
        """
        返回登录页面
        """
        try:
            # 定位到退出按钮并点击
            logout_button = self.find_element(By.XPATH, '//*[@id="app"]/div/div[1]/div/img[3]')
            logout_button.click()

            # 记录日志,表明已成功尝试返回登录页面
            self.logger.info("已成功返回登录页面")

            # 刷新页面,并等待一段时间以确保页面加载完成
            self.driver.refresh()
            self.driver.implicitly_wait(5)
        except Exception as e:
            # 如果发生异常,记录错误日志并使测试用例失败
            self.logger.error(f"返回登录页面失败: {e}")
            pytest.fail(f"返回登录页面失败: {e}")

    def check_login_null(self):
        """
        检查登录失败,必填项为空的情况
        """
        try:
            login_element_text = WebDriverWait(self.driver, 10).until(
                EC.visibility_of_element_located((By.XPATH, '/html/body/div[2]/p'))
            ).text

            # 断言登录失败后,提示信息中包含'请输入'关键词
            assert "请输入" in login_element_text, "登录失败后未找到'请输入'提示语"
            return True
        except Exception as e:
            self.logger.error(f"登录失败检查失败: {e}")
            return False

    def check_login_success(self):
        """
        检查登录成功的情况
        """
        try:
            message_element_text = WebDriverWait(self.driver, 10).until(
                EC.visibility_of_element_located((By.XPATH, "//*[contains(text(), '欢迎')]"))
            ).text

            # 断言欢迎信息是否符合预期
            assert "欢迎 预定标准版测试" == message_element_text, "登录成功后找到'欢迎 预定标准版测试'提示语"
            return True
        except Exception as e:
            self.logger.error(f"登录成功检查失败: {e}")
            return False

    def check_login_failure(self):
        """
        检查登录失败的情况
        """
        try:
            login_element_text = WebDriverWait(self.driver, 10).until(
                EC.visibility_of_element_located((By.XPATH, '//*[@id="app"]/div/div[3]/div[1]/div[1]/div[1]'))
            ).text

            # 断言登录元素的文本是否为"账号登录",如果符合,则说明登录失败检查成功
            assert "账号登录" == login_element_text, "登录失败后未找到'账号登录'提示语"
            return True
        except Exception as e:
            self.logger.error(f"登录失败检查失败: {e}")
            return False