selenium+pytest:Web自动化测试用例及conftest文件(五)

文章目录

  • 前言
  • 一、测试页面
  • 二、conftest配置文件
  • 总结


前言


提示:以下是本篇文章正文内容,下面案例可供参考

一、测试页面

测试页面均继承OperationPageFunction类,在页面上的步骤抽象成方法从而实现自动化操作

import logging.config
from congfig.pub.FilePath import LOG_PATH_CON
from base.OperationPageElement import OperationPageFunction
logging.config.fileConfig(LOG_PATH_CON)
elPath = "Login.yaml"
# 登录页面
class LoginPage(OperationPageFunction):

    #登录方法,通过输入用户名及密码进行登录
    def loginInputUsername(self,*text):
        return self.getYamElData(elPath,"loginInputUsername",*text)

    def loginInputPassword(self,*text):
        return self.getYamElData(elPath,"loginInputPassword",*text)
    #点击登录按钮
    def clickLoginButton(self):
        current_title = self.getPageTitle
        self.getYamElData(elPath,"clickLoginButton")
        return current_title

    #获取路由title,判断是否登录成功,True成功,False失败
    def checkLoginStatus(self,current_title):
        if self.getPageTitle != current_title:
            return True
        else:
            return False


    #正向登录方法,用于其他页面使用
    def goToIndex(self):
        self.getYamElData(elPath,"loginInputUsername","123")
        self.getYamElData(elPath, "loginInputPassword", "456")
        return self.checkLoginStatus(self.clickLoginButton())

二、conftest配置文件

该文件用来统一管理测试过程中前置和后置操作,类文件名命名为conftest,pytest执行时会自动查找对应文件下的方法进行执行

mport logging.config
import sys
import pytest
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from congfig.pub.FilePath import LOG_PATH_CON,W_EXECUTEABLE_PATH,URL,M_EXECUTEABLE_PATH
from page.action.atpplatform.CasePage import CasePage
from page.action.atpplatform.InterfaceListPage import InterfaceListPage
from page.action.atpplatform.LoginPage import LoginPage
from page.action.atpplatform.ProManagerPage import ProManagerPage
from tools.YamlTools import OperationTestDataYaml
from congfig.pub.FrontColour import FrontColour as FC
import platform

logging.config.fileConfig(LOG_PATH_CON)
#判断系统是mac还是window,调用不同的驱动路径
def chooseSystem():
    sys_mess = platform.uname()
    if sys_mess[0] == 'Windows':
        return W_EXECUTEABLE_PATH
    elif sys_mess[0] == 'Darwin':
        return M_EXECUTEABLE_PATH
    else:
        return False
def gloDriver():
    logging.info("驱动浏览器")
    option = webdriver.ChromeOptions()
    # option.add_experimental_option("detach", True)
    #配置静默模式及静默模式下窗口尺寸配置,规避静默模式下元素无法定位
    # option.add_argument('headless')
    option.add_argument('window-size=1920x1080')
    option.add_argument('--start-maximized')
    global driver
    try:
        driver = webdriver.Chrome(executable_path=chooseSystem(),options=option)
    except WebDriverException as e:
        logging.error(FC.RED%"浏览器驱动错误,请检查,错误信息:{0}".format(e))
        sys.exit()
    driver.get(URL)
    driver.maximize_window()
    loginAction()


@pytest.fixture()
def getDriver():
     yield gloDriver()
    # time.sleep(5)
    # driver.quit()
#登陆操作
def loginAction():

    LP = LoginPage(driver)
    LP.goToIndex()

总结

以上selenium+pytest自动化框架已经完成一个大概了,代码还存在一些bug和不合理的地方后续会持续优化,且会加上allure测试报告使脚本更加符合实际使用

你可能感兴趣的:(Selenium,selenium,前端,自动化)