Skip to content

Commit

Permalink
feat(conftest,login_page): 优化登录步骤,增加API的新增并且删除用例
Browse files Browse the repository at this point in the history
  • Loading branch information
lihuacai168 committed Jan 15, 2024
1 parent cc43f27 commit 94ac692
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 26 deletions.
60 changes: 49 additions & 11 deletions testcases/conftest.py → conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# !/usr/bin/python3
# -*- coding: utf-8 -*-import pytest
import os
from urllib.parse import urlparse

import pytest
from urllib.parse import urlparse
from playwright.sync_api import sync_playwright

from pages.login_page import LoginPage

from log import logger
from pages.login_page import LoginPage

# @Author: 花菜
# @File: conftest.py
Expand All @@ -19,6 +19,7 @@ def extract_domain(url_string):
parsed_url = urlparse(url_string)
return parsed_url.netloc


@pytest.fixture(scope="session")
def page(pytestconfig):
with sync_playwright() as p:
Expand All @@ -36,19 +37,56 @@ def page(pytestconfig):
browser.close()


# 创建一个 pytest fixture 实现登录操作,并设置为session级别,实现共享登录状态
@pytest.fixture(scope="function")
def login(page, pytestconfig):
# @pytest.fixture(scope="session")
def auth_page():
with sync_playwright() as p:
logger.info("page session fixture starting....")
browser = p.chromium.launch(headless=False, timeout=50_000)
logger.info("使用auth.json文件恢复登录状态")
base_path = os.path.dirname(os.path.realpath(__file__))
context = browser.new_context(
storage_state=os.path.join(base_path, "auth.json"),
viewport={"width": 1620, "height": 1080},
)
page = context.new_page()
context.tracing.start(screenshots=True, snapshots=True, sources=True)
yield page
logger.info("page session fixture closing.......")
context.tracing.stop(path="trace.zip")
browser.close()


def _login(page, pytestconfig, is_goto_project_detail=False):
default_url = "http://119.91.147.215"
base_url = ""
if not pytestconfig.getoption("base_url"):
base_url = default_url
logger.warning(f"没有传入base-url,会使用默认{base_url=},如果需要使用--base-url=xxx修改")

login_page = LoginPage(
page, pytestconfig.getoption("base_url") or "http://119.91.147.215"
page, base_url=base_url
)
if login_page.base_url == default_url:
logger.warning("使用默认登录地址,如果需要修改请使用命令行参数 --base-url")

login_page.login("test", "test2020")
yield login_page
# 可以在这里添加断言确认登录成功
if is_goto_project_detail:
logger.info("登录并进入项目详情")
login_page.switch2project_base()
login_page.enter_project_detail()
return login_page


# 创建一个 pytest fixture 实现登录操作,并设置为session级别,实现共享登录状态
@pytest.fixture(scope="function")
def login(page, pytestconfig):
yield _login(page, pytestconfig)


# 创建一个 pytest fixture 实现登录操作,并设置为session级别,实现共享登录状态
@pytest.fixture(scope="function")
def login_and_goto_project_detail(page, pytestconfig):
yield _login(page, pytestconfig, is_goto_project_detail=True)




def pytest_addoption(parser):
Expand Down
10 changes: 8 additions & 2 deletions pages/login_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,21 @@ def login(self, username: str, password: str):
self.page.wait_for_load_state("networkidle", timeout=3_000)
logger.info("登录成功")

# @allure.step("切换项目列表首页")
@allure.step("切换项目列表首页")
def switch2project_base(self, project_id=7):
logger.info("切换项目列表首页")
self.page.goto(
self.base_url + f"/fastrunner/api_record/{project_id}", timeout=3000
)
self.page.get_by_role("menuitem", name=" 首 页").click()

# @allure.step("从项目列表进入项目详情")
@allure.step("从项目列表进入项目详情")
def enter_project_detail(self, project_name="示例项目"):
logger.info(f"从项目列表进入项目详情:{project_name=}")
self.page.get_by_text(project_name, exact=True).click()

@allure.step("登录并进入项目详情")
def login_and_to_project_detail(self, username, password, project_name):
self.login(username, password)
self.switch2project_base()
self.enter_project_detail(project_name)
45 changes: 38 additions & 7 deletions testcases/test_api_page.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# !/usr/bin/python3
# -*- coding: utf-8 -*-
import json
import uuid
from urllib.parse import quote

import allure
from playwright.sync_api import expect
from playwright.sync_api import Page, expect

from log import logger

Expand All @@ -15,12 +16,8 @@
# @Email: lihuacai168@gmail.com
@allure.feature("API列表")
@allure.step("检查搜索API列表")
def test_api_list(login, page) -> None:
def test_api_list(login_and_goto_project_detail, page) -> None:
logger.info("检查搜索API列表...")
# page = login.page

login.switch2project_base()
login.enter_project_detail()
page.get_by_role("menuitem", name=" API 模板").click()
expect(page.get_by_role("button", name="批量运行")).to_be_visible()
expect(page.get_by_role("button", name="重置")).to_be_visible()
Expand All @@ -30,7 +27,7 @@ def test_api_list(login, page) -> None:
page.get_by_role("textbox", name="请输入接口名称").fill(search_api_name)
# 定义你希望等待的接口地址
get_api_path = (
login.base_url
login_and_goto_project_detail.base_url
+ f"/api/fastrunner/api/?page=1&node=&project=7&search={quote(search_api_name)}&tag=&rigEnv=&onlyMe=true&showYAPI=true"
)
logger.info(f"等待接口地址:{get_api_path}")
Expand All @@ -39,3 +36,37 @@ def test_api_list(login, page) -> None:
logger.info(f"接口返回数据:{data}")
expect(page.get_by_text(f"共 {data['count']} 条")).to_be_visible()
logger.info("检查搜索API列表成功")


@allure.feature("API列表")
@allure.step("增加然后删除API")
def test_api_add_del(login_and_goto_project_detail: Page, page: Page) -> None:
page.get_by_role("menuitem", name=" API 模板").click()
logger.info("点击测试分组")
page.get_by_text(" 测试分组").click()
page.get_by_role("button", name=" 添加接口").click()
page.get_by_role("textbox", name="请输入接口名称").click()
page.get_by_role("textbox", name="请输入接口名称").fill("ui")
page.get_by_role("textbox", name="请输入接口名称").press("Enter")
api_name = "ui自动化添加" + uuid.uuid4().hex
page.get_by_role("textbox", name="请输入接口名称").fill(api_name)
page.get_by_placeholder("请输入接口请求地址").click()
page.get_by_placeholder("请输入接口请求地址").fill("/hello")
page.get_by_role("button", name="Save", exact=True).click()
logger.info("点击保存按钮")
expect(page.get_by_text(f"POST /hello {api_name}").first).to_be_visible(
timeout=1_000
)
logger.info("断言添加接口成功")

# 删除接口
logger.info("删除接口")
page.get_by_text(f"POST /hello {api_name}").first.click()
page.get_by_role("cell", name="   ").get_by_role("button").nth(3).click()
page.get_by_role("button", name="").click()
page.get_by_role("button", name="确定").click()
logger.info("点击确定按钮")
expect(page.get_by_text(f"POST /hello {api_name}").first).not_to_be_visible(
timeout=1_000
)
logger.info("断言删除接口成功")
8 changes: 2 additions & 6 deletions testcases/test_testcase_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@
@allure.testcase("testcases/test_login.py")
@allure.feature("case列表")
@allure.step("case搜索")
def test_case_list(login, page) -> None:
# page = login.page
def test_case_list(login_and_goto_project_detail, page) -> None:
logger.info("检查搜索case列表...")
login.switch2project_base()
login.enter_project_detail()

page.get_by_role("menuitem", name=" 测试用例").click()
search_testcase_name = "登录"
page.get_by_placeholder("请输入用例名称").fill(search_testcase_name)
get_test_case_path = f"/api/fastrunner/test/?project=7&node=&search={quote(search_testcase_name)}&searchType=1&caseType=&onlyMe=true&page=1"
logger.info(f"等待接口地址:{get_test_case_path}")
with page.expect_response(
login.base_url + get_test_case_path, timeout=3000
login_and_goto_project_detail.base_url + get_test_case_path, timeout=3000
) as case_response_info:
data = json.loads(case_response_info.value.request.response().body().decode())
logger.info(f"接口返回数据:{data}")
Expand Down

0 comments on commit 94ac692

Please sign in to comment.