ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

pom模式+ddt思想+logger+allure 重构jpress

2020-11-26 15:00:44  阅读:233  来源: 互联网

标签:loc jpress self ddt allure debug import logger def


一、新建页面project

1、BasePage

import logging
import logging.handlers
import datetime
from Lib.ShowapiRequest import ShowapiRequest
import time
import os
from PIL import Image


class BasePage(object):

    def __init__(self, driver):
        '''
        初始化driver
        :param driver:
        '''
        self.driver = driver

    def find_element(self, *loc):
        '''
        根据属性查找
        :param loc:
        :return:
        '''
        return self.driver.find_element(*loc)

    def type_text(self, text, *loc):
        '''
        输入文本
        :param text:
        :param loc: (多个参数需放至参数尾)
        :return:
        '''
        self.find_element(*loc).send_keys(text)

    def click(self, *loc):
        '''
        点击事件
        :param loc:
        :return:
        '''
        self.find_element(*loc).click()

    def clear(self, *loc):
        '''
        清空文本
        :param loc:
        :return:
        '''
        self.find_element(*loc).clear()

    def get_title(self):
        '''
        获取title
        :return:
        '''
        return self.driver.title

    def get_logger(self):
        '''
        记录日志
        :return:
        '''
        logger = logging.getLogger('mylogger')
        logger.setLevel(logging.DEBUG)
        rf_handler = logging.handlers.TimedRotatingFileHandler(
            'D:\TestSoftware\PyCharm\PyCharmProject\Jpress\Logs\\all.log', when='midnight', interval=1, backupCount=7,
            atTime=datetime.time(0, 0, 0, 0))
        rf_handler.setFormatter(logging.Formatter('%(%(asctime)s-%(levelname)s-%(message)s'))

        f_handler = logging.FileHandler('D:\TestSoftware\PyCharm\PyCharmProject\Jpress\Logs\error.log')
        f_handler.setLevel(logging.ERROR)
        rf_handler.setFormatter(logging.Formatter('%(asctime)s-%(levelname)s-%(filename)s[:%(lineno)d - %(message)s]'))

        logger.addHandler(rf_handler)
        logger.addHandler(f_handler)
        return logger


    def get_code(self, id):
        '''
        获取验证码
        :param id:
        :return:
        '''
        #   获取验证码图片
        t = time.time()
        #   保存到当前项目下Screen下
        path = os.path.dirname(os.path.dirname(__file__)) + '\\Screen'
        picture_name1 = path + '\\' + str(t) + '.png'
        self.driver.save_screenshot(picture_name1)
        code = self.driver.find_element_by_id(id)
        #   print(code.location)
        left = code.location['x']
        top = code.location['y']
        right = code.size['width'] + left
        height = code.size['height'] + top
        dpr = self.driver.execute_script('return window.devicePixelRatio')
        #   print(dpr)
        im = Image.open(picture_name1)
        #   抠验证码区域图
        code_img = im.crop((left * dpr, top * dpr, right * dpr, height * dpr))
        picture_name2 = path + '\\' + str(t) + '.png'
        code_img.save(picture_name2)
        #   调用验证码识别api xxx xxx
        #   272526                 a924d4e982ae404b8a068b4d1c7784f2
        r = ShowapiRequest("http://route.showapi.com/184-4", "xxx", "xxx")
        r.addFilePara("image", picture_name2)
        r.addBodyPara("typeId", "34")
        r.addBodyPara("convert_to_jpg", "0")
        r.addBodyPara("needMorePrecise", "0")
        res = r.post()
        text = res.json()['showapi_res_body']
        code_text = text['Result']
        return code_text

2、管理员登录page

from selenium.webdriver.common.by import By
from Cases.pom.page.BasePage_Utils import BasePage
from time import sleep

class AdLogin_Page(BasePage):

    adName =(By.NAME, 'user')
    adPwd = (By.NAME, 'pwd')
    adCaptcha = (By.NAME, 'captcha')
    adRegister_btn =(By.CLASS_NAME, 'btn')

    def __init__(self,driver):
        BasePage.__init__(self, driver)

    def admin_Login_Page(self):
        '''
            管理员登录
            :param
        '''
        self.driver.get('http://192.168.242.101:8080/jpress/user/login')
        self.driver.maximize_window()
        self.logger = BasePage.get_logger()
        self.logger.info('测试管理员用户登录')


    def admin_username_input(self,username):
        '''
        输入管理员名称
        :param username:
        :return:
        '''
        self.clear(*self.adName)
        self.type_text(username, *self.adName)
        self.logger.debug('输入管理员用户名称: %s', username)

    def admin_pwd_input(self,pwd):
        '''
        输入管理员密码
        :param pwd:
        :return:
        '''
        self.clear(*self.adPwd)
        self.type_text(pwd, *self.adPwd)
        self.logger.debug('输入管理员用户密码: %s', pwd)


    def admin_captcha_input(self,captcha):
        '''

        :param captcha:
        :return:
        '''
        self.clear(*self.adCaptcha)
        self.type_text(captcha, *self.adCaptcha)
        self.logger.debug('输入管理员用户验证码: %s', captcha)

    '''
        pm: admin_login
        param:  
    '''
    def admin_login(self):
        '''
        点击登录
        :return:
        '''
        self.click(*self.adRegister_btn)
        self.logger.debug('点击,管理员用户登录!!!!!')
        sleep(2)

3、普通用户登录page

from selenium.webdriver.common.by import By
from Cases.pom.page.BasePage_Utils import BasePage
from time import sleep

class UserLoginPage(BasePage):

    userName =(By.NAME, 'user')
    userPwd = (By.NAME, 'pwd')
    userCaptcha = (By.NAME,'captcha')
    userLogin_btn =(By.CLASS_NAME,'btn')


    def __init__(self,driver):
        BasePage.__init__(self, driver)


    def user_Login_Page(self):
        '''
        进入登录页
        :return:
        '''
        self.driver.get('http://192.168.242.101:8080/jpress/admin/login')
        self.driver.maximize_window()
        self.logger = BasePage.get_logger()
        self.logger.info('测试用户登录')

    def user_username(self,username):
        '''
        输入管理员名称
        :param username:
        :return:
        '''
        self.clear(*self.userName)
        self.type_text(username, *self.userName)
        self.logger.debug('输入普通用户名称: %s', username)


    def user_pwd(self,pwd):

        '''
        输入管理员密码
        :param pwd:
        :return:
        '''
        self.clear(*self.userPwd)
        self.type_text(pwd, *self.userPwd)
        self.logger.debug('输入普通用户密码: %s',pwd)

    def user_captcha(self,captcha):
        '''
        输入管理员验证码
        :param captcha:
        :return:
        '''
        self.clear(*self.userCaptcha)
        self.type_text(captcha, *self.userCaptcha)
        self.logger.debug('输入普通用户验证码: %s',captcha)

    def user_login(self):
        '''
        点击管理员登录
        :return:
        '''
        self.click(*self.userLogin_btn)
        self.logger.debug('点击,普通用户登录!!!!!')
        sleep(2)

4、普通用户注册page

from time import sleep

from selenium.webdriver.common.by import By

from Cases.pom.page.BasePage_Utils import BasePage


class UserRegisterPage(BasePage):

    userName = (By.NAME, 'username')
    userEmail = (By.NAME, 'email')
    userPwd = (By.NAME, 'pwd')
    userConfirmPwd = (By.NAME, 'confirmPwd')
    userCaptcha = (By.NAME, 'captcha')
    user_register_btn = (By.CLASS_NAME, 'btn')


    def __init__(self, driver):
        '''
            :param 初始化driver
            :return
        '''
        BasePage.__init__(self, driver)


    def register_page(self):
        '''
            用户注册页
        '''
        self.driver.get('http://192.168.242.101:8080/jpress/user/register')
        self.driver.maximize_window()
        self.logger = BasePage.get_logger()
        self.logger.info('测试普通用户注册!!!')


    def input_username(self, username):
        '''
            :param username
            :return
        '''
        self.clear(*self.userName)
        self.type_text(username, *self.userName)

    def input_email(self, email):
        '''
        :param email:
        :return:
        '''
        self.clear(*self.userEmail)
        self.type_text(email, *self.userEmail)

    def input_pwd(self, pwd):
        '''
        :param pwd:
        :return:
        '''
        self.clear(*self.userPwd)
        self.type_text(pwd, *self.userPwd)

    def input_confirmPwd(self, confirmPwd):
        '''
        输入确认密码
        :param confirmPwd:
        :return:
        '''
        self.clear(*self.userConfirmPwd)
        self.type_text(confirmPwd, *self.userConfirmPwd)

    def input_captcha(self, captcha):
        '''
        输入验证码
        :param captcha:
        :return:
        '''
        self.clear(*self.userCaptcha)
        self.type_text(captcha, *self.userCaptcha)

    def click_register_btn(self):
        '''
        点击注册
        :return:
        '''
        self.click(*self.user_register_btn)
        sleep(2)

5、文章管理page

from time import sleep

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from Cases.pom.page.BasePage_Utils import BasePage


class ArticlePage(BasePage):
    '''
        文章管理page
    '''

    def __init__(self, login):
        BasePage.__init__(self, login.driver)
        self.login = login
        self.logger = BasePage.get_logger()
        self.logger.info('测试文章管理')

    # 文章loc
    click_article_loc = (By.XPATH, '//*[@id="sidebar-menu"]/li[4]/a/span[1]')
    # 文章管理loc
    click_article_manage_loc = (By.XPATH, '//*[@id="sidebar-menu"]/li[4]/ul/li[1]/a')
    # 添加文章按钮loc
    click_add_article_btn_loc = (By.XPATH, '/html/body/div/div/section[3]/div/div/div/div[1]/div/div/a')

    # 文章标题
    article_title_loc = (By.ID, 'article-title')

    # iframe loc
    iframe_loc = (By.XPATH, '//*[@id="cke_1_contents"]/iframe')

    # body loc
    body_loc = (By.XPATH, '/html/body')

    # 添加按钮
    add_btn_loc = (By.XPATH, '//*[@id="form"]/div/div[2]/div[1]/div/button[1]')

    # 文章链接
    article_link_loc = (By.XPATH, '/html/body/div/div/section[3]/div/div/div/div[2]/table/tbody/tr[2]/td[2]/strong/a')

    # 删除文章链接
    del_article_link_loc = (By.XPATH, '/html/body/div/div/section[3]/div/div/div/div[2]/table/tbody/tr[2]/td[2]/div/div/a[3]')

    # 文章loc
    select_all_checkbox_loc = (By.XPATH, '/html/body/div/div/section[3]/div/div/div/div[2]/table/tbody/tr[1]/th[1]/input')

    # 删除所有按钮
    del_all_btn_loc = (By.ID, 'batchDel')


    def click_article(self):
        '''
        点击文章菜单
        :return:
        '''
        self.click(*self.click_article_loc)
        self.logger.debug('点击文章菜单')
        sleep(1)

    def click_article_manage(self):
        '''
        点击文章管理
        :return:
        '''
        self.click(*self.click_article_manage_loc)
        self.logger.debug('点击文章管理菜单')
        sleep(1)


    def click_add_article(self):
        '''
        点击添加文章
        :return:
        '''
        self.click(*self.click_add_article_btn_loc)
        self.logger.debug('点击添加文章')
        sleep(1)

    def input_article_title(self, title):
        '''
        输入文章标题
        :param title:
        :return:
        '''
        self.type_text(title, *self.article_title_loc)
        self.logger.debug('输入文章标题: %s:', title)
        sleep(1)

    def input_body(self, body):
        '''
        输入文章body
        :param body:
        :return:
        '''
        frame1 = self.find_element(*self.iframe_loc)
        self.login.driver.switch_to.frame(frame1)
        self.type_text(body, *self.body_loc)
        self.login.driver.switch_to.default_content()
        self.logger.debug('输入文章内容: %s:', body)

    def click_add_btn(self):
        '''
        点击添加按钮
        :return:
        '''
        self.click(*self.add_btn_loc)
        self.logger.debug('点击添加文章按钮')

    def del_single_article(self):
        '''
        删除单个文章
        :return:
        '''
        # 点击删除文章
        link = self.find_element(*self.article_link_loc)
        ActionChains(self.login.driver).move_to_element(link).perform()
        sleep(1)
        del_elem = self.find_element(*self.del_article_link_loc)
        self.logger.debug('点击文章下的子菜单垃圾箱')
        del_elem.click()

    def del_all_article(self):
        '''
        删除所有文章
        :return:
        '''
        sleep(1)

        self.find_element(*self.click_article_manage_loc).click()
        sleep(1)
        # 选中全选
        link = self.find_element(*self.select_all_checkbox_loc)
        link.click()

        self.find_element(*self.del_all_btn_loc).click()
        self.logger.debug('删除全部文章')

        WebDriverWait(self.login.driver, 5).until(EC.alert_is_present())
        alert = self.login.driver.switch_to.alert
        alert.accept()

6、分类管理page

from time import sleep

from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from Cases.pom.page.BasePage_Utils import BasePage


class CategoryPage(BasePage):
    '''
     分类管理页
    '''

    def __init__(self, login):
        '''
        获取login
        :param login:
        '''
        BasePage.__init__(self, login.driver)
        self.logger = BasePage.get_logger()
        self.logger.info('测试分类管理页')

    click_article_loc = (By.XPATH, '//*[@id="sidebar-menu"]/li[4]/a/span[1]')
    '''
    文章loc
    '''
    click_category_loc = (By.XPATH, '//*[@id="sidebar-menu"]/li[4]/ul/li[3]/a')
    '''
    分类loc
    '''
    category_name_loc = (By.NAME, 'category.title')
    '''
    分类名称loc
    '''
    parent_category_loc = (By.NAME, 'category.pid')
    '''
    父分类loc
    '''
    slug_loc = (By.NAME, 'category.slug')
    '''
    slug loc
    '''
    add_btn_loc = (By.XPATH, '/html/body/div/div/section[2]/div/div[1]/div/form/div[2]/div/div/button')
    '''
    添加按钮
    '''
    def click_article(self):
        '''
        点击文章
        :return:
        '''
        self.click(*self.click_article_loc)
        self.logger.debug('点击文章')
        sleep(1)

    def click_category(self):
        '''
        点击分类
        :return:
        '''
        self.click(*self.click_category_loc)
        self.logger.debug('点击分类')
        sleep(1)


    def input_category_name(self, name):
        '''
        输入分类名称
        :param name
        :return:
        '''
        self.type_text(name, *self.category_name_loc)
        self.logger.debug('输入分类名称')
        sleep(1)

    def select_parent_category(self, parent_name):
        '''
        选择父分类
        :param parent_name:
        :return:
        '''
        parent_category_elem = self.find_element(*self.parent_category_loc)
        Select(parent_category_elem).select_by_visible_text(parent_name)
        self.logger.debug('选择父分类')

    def input_slug(self, slug):
        '''
        输入slug
        :param slug:
        :return:
        '''
        try:
            self.type_text(slug, *self.slug_loc)
            self.logger.debug('输入slug')
        except:
            self.logger.warning('未找到元素: %s',slug)


    def click_add_btn(self):
        '''
        点击分类保存按钮
        :return:
        '''
        self.click(*self.add_btn_loc)
        self.logger.debug('点击保存')

二、测试

1、测试管理员登录




import pytest
from selenium.webdriver.support.wait import WebDriverWait

from Cases.pom.page.test_AdLogin_Page import AdLogin_Page
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from time import sleep
from Cases.pom.page.BasePage_Utils import BasePage
import allure


@allure.feature('测试管理员登录')
class TestAdminLogin(object):

    admin_login_data=[
        # ('admin', '123456', '666', '验证码不正确,请重新输入'),
        ('admin', '123456', '111', 'JPress后台'),
    ]

    def setup_class(self) -> None:
        '''
        管理员登录初始化
        :return:
        '''
        self.driver = webdriver.Chrome()
        self.adminLoginPage = AdLogin_Page(self.driver)
        self.adminLoginPage.admin_login()
        self.logger = BasePage.get_logger()

    # @pytest.mark.skip()
    @allure.title('管理员登录')
    @allure.step('输入账号/密码/登录/验证码,点击登录')
    @pytest.mark.dependency(name='admin_login')
    @pytest.mark.parametrize('username,pwd,captcha,expected', admin_login_data)
    def test_admin_login(self, username, pwd, captcha, expected):
        '''
        测试管理员登录
        :param username:
        :param pwd:
        :param captcha:
        :param expected:
        :return:
        '''

        self.adminLoginPage.admin_username_input(username)
        self.logger.debug('输入管理员名称: %s',username)
        self.adminLoginPage.admin_pwd_input(pwd)
        self.logger.debug('输入管理员密码: %s', pwd)
        if captcha != '666':
            captcha = BasePage.get_code('captchaImg')
        self.adminLoginPage.admin_captcha_input(captcha)
        self.logger.debug('输入管理员验证码: %s', captcha)
        self.adminLoginPage.admin_login()
        self.logger.debug('点击登录按钮!!!!!!')

        if captcha != '666':
            WebDriverWait(self.driver, 5).until(EC.title_is(expected))
            assert expected == self.driver.title
        else:
            WebDriverWait(self.driver, 5).until(EC.alert_is_present())
            alert = self.driver.switch_to.alert

            assert alert.text == expected
            self.logger.warning('校验alert.text == expected ')
            alert.accept()

            sleep(5)


if __name__ == '__main__':
    pytest.main(['testAdminLogin.py'])

2、测试普通用户登录


import pytest
from selenium.webdriver.support.wait import WebDriverWait
from Cases.pom.page.BasePage_Utils import BasePage
from Cases.pom.page.test_UseLogin_Page import UserLoginPage
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from time import sleep
import allure


@allure.feature('测试普通用户登录')
class TestUserLogin(object):
    '''
    测试普通用户登录
    '''

    login_data=[
        ('','123456','账号不能为空'),
        ('admin', '123456', '用户中心-2'),
    ]

    def setup_class(self) -> None:
        self.driver = webdriver.Chrome()
        self.loginPage = UserLoginPage(self.driver)
        self.loginPage.user_Login_Page()
        self.logger = BasePage.get_logger()
        self.logger.info('开始测试-普通用户登录!')

    @allure.title('测试用户登录')
    @allure.step('输入用户名称/密码,点击登录')
    @pytest.mark.parametrize('username, pwd, expected', login_data)
    def test_user_login(self, username, pwd, expected):
        '''
        测试用户登录
        :param username:
        :param pwd:
        :param expected:
        :return:
        '''
        self.loginPage.user_username(username)
        self.logger.debug('输入普通用户名:%s',username)
        self.loginPage.user_pwd(pwd)
        self.logger.debug('输入普通用户名:%s', pwd)
        self.loginPage.user_login()
        self.logger.debug('点击登录')
        sleep(3)
        # 验证
        if username != '':
            # 等待提示框
            WebDriverWait(self.driver, 5).until(EC.title_is(expected))
            sleep(3)
            # 验证
            assert self.driver.title == expected
            self.logger.debug('当用户名不为空时,校验driver.title == expected')
        else:
            # 等待提示框
            WebDriverWait(self.driver, 5).until(EC.alert_is_present())
            alert = self.driver.switch_to.alert
            assert alert.text == expected
            alert.accept()
            self.logger.debug('当用户名为空时,校验driver.title == expected')


if __name__ == '__main__':
    pytest.main(['-sv', 'testUserLogin.py'])


3、测试普通用户注册

import pytest
from selenium.webdriver.support.wait import WebDriverWait

from Cases.pom.page.test_UseRegist_Page import UserRegisterPage
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from time import sleep
from Cases.pom.page.BasePage_Utils import BasePage
import allure


@allure.feature('测试用户注册')
class TestUserRegister(object):
    '''
    测试普通用户注册
    '''

    login_data = [
        ('test001', 'test001@qq.com', '123456', '123456', '666', '验证码不正确'),
        ('demo007', 'demo006@qq.com', '123456', '123456', '111', '注册成功,点击确定进行登录。'),
    ]

    def setup_class(self) -> None:
        self.driver = webdriver.Chrome()
        self.registerPage = UserRegisterPage(self.driver)
        self.registerPage.register_page()
        self.logger = BasePage.get_logger()
        self.logger.info('开始测试-普通用户注册!!!')

    @allure.title('测试用户注册')
    @allure.step('输入用户名称/邮箱/密码/确认密码/验证码,点击登录')
    @pytest.mark.parametrize('username,email,pwd,confirmPwd,captcha,expected', login_data)
    def test1_register(self, username, email, pwd, confirmPwd, captcha, expected):
        '''
        测试普通用户注册
        :param username:
        :param email:
        :param pwd:
        :param confirmPwd:
        :param captcha:
        :param expected:
        :return:
        '''

        self.registerPage.input_username(username)
        self.logger.debug('输入普通用户名称:%s',username)
        self.registerPage.input_email(email)
        self.logger.debug('输入普通用户邮箱:%s', email)
        self.registerPage.input_pwd(pwd)
        self.logger.debug('输入普通用户密码:%s', pwd)
        self.registerPage.input_confirmPwd(confirmPwd)
        self.logger.debug('输入确认密码:%s', confirmPwd)

        if captcha != '666':
            captcha = BasePage.get_code(self.driver, 'captchaimg')
        self.registerPage.input_captcha(captcha)
        self.logger.debug('输入验证码:%s', captcha)
        self.registerPage.click_register_btn()
        self.logger.debug('点击注册按钮')

        WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        alert = self.driver.switch_to.alert
        assert alert.text == expected
        self.logger.debug('校验alert.text == expected')
        alert.accept()
        sleep(5)


if __name__ == '__main__':
    pytest.main(['-sv', 'testUserRegister.py'])

4、测试文章管理

import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from Cases.pom.page.test_Article_Page import ArticlePage
from selenium.webdriver.support import expected_conditions as EC
from Cases.pom.pageTest.testAdLogin import TestAdminLogin
from Cases.pom.page.BasePage_Utils import BasePage
from time import sleep
import allure


@allure.feature('测试文章管理')
class TestArticle(object):
    '''
    测试文章管理
    '''
    article_data = [
        ('我的文章', '我的文章内容', '文章保存成功。')
    ]

    def setup_class(self) -> None:
        self.login = TestAdminLogin()
        self.articlePage = ArticlePage(self.login)
        self.logger = BasePage.get_logger()

    @allure.title('测试添加文章')
    @allure.step('添加文章标题、内容,点击发布')
    @pytest.mark.dependency(depends=["admin_login"], scope="module")
    @pytest.mark.parametrize('title, content, expected', article_data)
    def test_add_ok(self, title, content, expected):
        '''
        测试添加文章
        :param title:
        :param content:
        :param expected:
        :return:
        '''
        self.articlePage.click_article()
        self.logger.debug('点击文章')
        self.articlePage.click_article_manage()
        self.logger.debug('点击文章管理')
        self.articlePage.click_add_article()
        self.logger.debug('点击添加文章')
        sleep(1)

        self.articlePage.input_article_title(title)
        self.logger.debug('输入文章标题:%s',title)
        self.articlePage.input_body(content)
        self.logger.debug('输入文章内容:%s', content)
        self.articlePage.click_add_btn()
        self.logger.debug('点击发布按钮')

        loc = (By.CLASS_NAME, 'toast-message')

        WebDriverWait(self.login.driver, 5).until(EC.visibility_of_element_located(loc))
        msg = self.login.driver.find_element(*loc).text
        assert msg == expected
        sleep(2)

    @allure.title('测试删除单个文章')
    @allure.step('选择文章,点击垃圾箱删除')
    @pytest.mark.dependency(depends=["admin_login"], scope="module")
    def test_delete_one_article_ok(self):
        '''
        测试删除单个文章
        :return:
        '''
        # 接上一个添加文章测试,不用再点击文章链接,直接点击文章管理
        self.articlePage.click_article_manage()
        self.logger.debug('点击文章管理')
        self.articlePage.del_single_article()
        self.logger.debug('删除文章')
        sleep(3)

    @allure.title('测试删除所有文章')
    @allure.step('全选文章,点击删除')
    @pytest.mark.dependency(depends=["admin_login"], scope="module")
    def test_delete_all_article_ok(self):
        '''
        测试删除所有文章
        :return:
        '''
        self.articlePage.del_all_article()
        self.logger.debug('删除所有文章')


if __name__ == '__main__':
    pytest.main(['testArticle.py'])

5、测试分类管理

import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from Cases.pom.page.BasePage_Utils import BasePage
from Cases.pom.page.test_Catefory_Page import CategoryPage
from selenium.webdriver.support import expected_conditions as EC
from Cases.pom.pageTest.testAdLogin import TestAdminLogin
from time import sleep
import allure


@allure.feature('测试分类管理')
class TestCategory(object):
    '''
    测试分类管理
    '''

    category_data = [
        ('','python', 'test', '分类名称不能为空'),
        ('test','python', 'test', ''),
    ]

    def setup_class(self) -> None:
        self.login = TestAdminLogin()
        self.categoryPage = CategoryPage(self.login)
        self.logger = BasePage.get_logger()
        self.logger.info('测试分类管理!!!!')

    @allure.title('添加分类')
    @allure.step('输入分类标题、内容、slug点击发布')
    @pytest.mark.dependency(depends=["admin_login"], scope="module")
    @pytest.mark.parametrize('name,parent,slug,expected', category_data)
    def test_add_category(self, name, parent, slug, expected):
        '''
        测试分类
        :param name:
        :param parent:
        :param slug:
        :param expected:
        :return:
        '''

        if name == '':
            self.categoryPage.click_article()
            self.logger.debug('点击文章')
            self.categoryPage.click_category()
            self.logger.debug('点击分类')

        self.categoryPage.input_category_name(name)
        self.logger.debug('输入分类名称: %s',name)

        self.categoryPage.select_parent_category(parent)
        self.logger.debug('选择父分类: %s', parent)
        self.categoryPage.input_slug(slug)
        self.logger.debug('输入slug: %s', slug)
        self.categoryPage.click_add_btn()
        self.logger.debug('点击添加')

        if name == '':
            loc = (By.CLASS_NAME, 'toast-message')
            WebDriverWait(self.login.driver, 5).until(EC.visibility_of_element_located(loc))
            msg = self.login.driver.find_element(*loc).text
            assert msg == expected
            self.logger.debug('校验:assert msg == expected')
            sleep(5)
        else:
            assert 1 == 1

if __name__ == '__main__':
    pytest.main(['testCategory.py'])

标签:loc,jpress,self,ddt,allure,debug,import,logger,def
来源: https://www.cnblogs.com/c-jw/p/14042299.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有