代码拉取完成,页面将自动刷新
import re
import allure
import pytest
from playwright.sync_api import Page, expect
"""
测试示例
"""
@allure.feature("xxl-job-admin任务调度中心测试")
@allure.story("用户管理子模块")
class TestUser:
# 每个用例执行前都会先执行的夹具
@pytest.fixture(autouse=True)
def start_for_each(self, page: Page):
print("-------用户管理模块测试用例前置执行逻辑----------")
# 打开主页
page.goto("/xxl-job-admin/")
# 点击主页“用户管理”菜单
page.get_by_role("link", name="用户管理").click()
# 断言是否成功导航到用户管理页面
expect(page).to_have_url(re.compile(".*/user"))
yield
print("-------用户管理模块测试用例后置执行逻辑----------")
# doSomething...
# 测试新增用户(!!!不要admin、admin123这种有前缀相同的账号,断言会有异常,一般可以使用uuid)
# !!!主要这个测试顺序并不会新增这两个用户后才会去执行面的用例,可能的执行顺序是先新增完zhangsan,接着删除lisi,然后再新增lisi
@allure.title("新增用户")
# @pytest.mark.parametrize("username,password",[("zhangsan","123456"),("lisi","123456")])
@pytest.mark.parametrize("username,password",[("wangwu","123456")])
# @pytest.mark.skip # 跳过测试
def test_create_user(self, username, password, page: Page):
# 点击”新增用户按钮“
with allure.step("点击新增用户按钮"):
page.get_by_role("button", name="新增用户").click()
# 输入账号
with allure.step(f"输入账号:{username}"):
page.locator("#addModal").get_by_placeholder("请输入账号").fill(username)
# 输入密码
with allure.step(f"输入密码:{password}"):
page.locator("#addModal").get_by_placeholder("请输入密码").fill(password)
# 选择管理员账号
with allure.step("选择管理员角色"):
page.locator("#addModal input[name=\"role\"]").nth(1).check()
# 点击保存按钮
with allure.step("点击保存按钮"):
page.locator("#addModal").get_by_text("保存").click()
# 断言”新增成功“是否出现
expect(page.get_by_text("新增成功")).to_be_visible()
# 测试查询用户
@allure.title("查询用户")
@pytest.mark.parametrize("username",["zhangsan"])
def test_search_user(self, username, page: Page):
# 点击”角色“下拉框,选择”管理员“
# <select class="form-control" id="role">
# <option value="-1">全部</option>
# <option value="1">管理员</option>
# <option value="0">普通用户</option>
# </select>
# 查询页-"角色"查询条件下拉框
with allure.step("点击角色下拉框选择管理员角色"):
page.locator("#role").select_option("1")
# 点击”每页xx条记录“选择每页查询100条
# <div class="dataTables_length" id="user_list_length">
# <label>每页
# <select name="user_list_length" aria-controls="user_list" class="form-control input-sm">
# <option value="10">10</option>
# <option value="25">25</option>
# <option value="50">50</option><
# option value="100">100</option><
# /select>
# 条记录<
# /label>
# </div>
with allure.step("点击分页大小下拉框选择每页100条记录"):
page.locator('xpath=//*[@id="user_list_length"]/label/select').select_option("100")
# 输入账号关键词
with allure.step(f"输入账号关键词:{username}"):
page.locator("#username").fill(username)
# 点击搜索按钮
with allure.step("点击搜索按钮"):
page.get_by_role("button", name="搜索").click()
# 断言查询结果是否有该账号
expect(page.locator('xpath=//*[@id="user_list"]/tbody')).to_contain_text(username)
# 测试删除用户
@allure.title("删除用户")
@pytest.mark.parametrize("username",["lisi"])
# @pytest.mark.skip
def test_delete_user(self, username, page: Page):
# 点击”角色“下拉框,选择”全部“
with allure.step("点击角色下拉框选择全部角色"):
page.locator("#role").select_option("-1")
# 点击”每页xx条记录“选择每页查询100条
with allure.step("点击分页大小下拉框选择每页100条记录"):
page.locator('xpath=//*[@id="user_list_length"]/label/select').select_option("100")
# 输入账号关键词
with allure.step(f"输入账号关键词:{username}"):
page.locator("#username").fill(username)
# 点击搜索按钮
with allure.step("点击搜索按钮"):
page.get_by_role("button", name="搜索").click()
# 点击该用户所在行上的“删除”按钮
with allure.step(f"点击用户:{username}行中的删除按钮"):
page.locator('xpath=//*[@id="user_list"]/tbody').get_by_role("row", name=re.compile(f"{username}.*")).get_by_role("button", name="删除").click()
# 点击确认删除按钮
with allure.step("点击确认删除按钮"):
page.get_by_text("确定", exact=True).click()
# 断言是否删除成功
expect(page.get_by_text("成功")).to_be_visible()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。