自动化测试

自动化测试_第1张图片

login_page.py

# -*- coding: utf-8 -*-
# @Author  : 思齐
# @Time    : 2023/6/11 22:16
# @Function:
from __future__ import annotations
from selenium import webdriver
from selenium.webdriver.common.by import By

from shixun2.tests.page.base_page import BasePage
from shixun2.tests.page.index_page import IndexPage


class LoginPage(BasePage):
    '''
    登录页面,集成父类BasePage用于复用driver
    '''


    def open_login_page(self) -> LoginPage:
        # 打开登录页面
        login_url = "https://litemall.hogwarts.ceshiren.com/#/login?redirect=%2Fdashboard"
        self.driver.get(login_url)
        return self
    #     登录
    def click_login_btn(self) -> IndexPage:
        # 点击登录按钮
        # self.driver.find_element(By.XPATH,'//*[@id="app"]/div/form/div[3]').click()
        login_buttton = '//span[text()="登录"]'
        self.driver.find_element(By.XPATH, login_buttton).click()
        # 点击登录之后,返回首页
        return IndexPage(self.driver)

 index_page.py

# -*- coding: utf-8 -*-
# @Author  : 思齐
# @Time    : 2023/6/11 22:16
# @Function:
from __future__ import annotations
from selenium.webdriver.common.by import By

from shixun2.tests.page.base_page import BasePage
from shixun2.tests.page.goods_list_page import GoodsListPage


class IndexPage(BasePage):
    '''
    首页,集成父类BasePage用于复用driver
    '''
    def click_goods_manager_button(self) -> IndexPage:
        goods_manager = '//span[text()="商品管理"]'
        self.driver.find_element(By.XPATH, goods_manager).click()
        return self

    def click_goods_list_button(self) -> GoodsListPage:
        #         点击商品列表
        goods_list_button = '//span[text()="商品列表"]'
        self.driver.find_element(By.XPATH, goods_list_button).click()
        # 跳转到商品列表页面
        return GoodsListPage(self.driver)
goods_page.py
# -*- coding: utf-8 -*-
# @Author  : 思齐
# @Time    : 2023/6/11 22:16
# @Function:
from selenium.webdriver.common.by import By

from shixun2.tests.page.base_page import BasePage
from shixun2.tests.page.goods_add_page import GoodsAddPage


class GoodsListPage(BasePage):
    '''
    商品列表页面,集成父类BasePage用于复用driver
    '''
    def click_goods_add_button(self):
        #         进入商品列表,点击添加按钮
        goods_add_button = '//*[text()="添加"]'
        self.driver.find_element(By.XPATH, goods_add_button).click()
        # 跳转至商品添加页面
        return GoodsAddPage(self.driver)
goods_list_page.py
# -*- coding: utf-8 -*-
# @Author  : 思齐
# @Time    : 2023/6/11 22:16
# @Function:
from __future__ import annotations
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from shixun2.tests.page.base_page import BasePage



class GoodsListPage(BasePage):
    '''
    商品列表页面,集成父类BasePage用于复用driver
    '''

    def __init__(self, driver=None):
        super().__init__(driver)

    # 点击删除按钮
    def click_goods_delete_button(self) -> GoodsListPage:
        goods_del_button = '//*[text()="删除"]'
        self.driver.find_element(By.XPATH, goods_del_button).click()
        return self
    # 输入搜索内容

    def send_keys_goods_name(self,goods_name) -> GoodsListPage:
        # 输入商品名称
        goods_name_search_input = '//*[@placeholder="请输入商品名称"]'
        self.driver.find_element(By.XPATH,goods_name_search_input).clear()
        self.driver.find_element(By.XPATH, goods_name_search_input).send_keys(goods_name)
        return self
    # 点击搜索按钮
    def click_goods_search_button(self):
        # 点击查询
        goods_search_button = '//*[text()="查找"]'
        self.driver.find_element(By.XPATH, goods_search_button).click()
        time.sleep(1)
    # 点击编辑按钮
    def click_goods_edit_button(self):
        #     进入商品列表,点击编辑按钮
        goods_edit_button = '//*[text()="编辑"]'
        self.driver.find_element(By.XPATH,goods_edit_button).click()
        # import语句,放入函数中,防止循环导入
        from shixun2.tests.page.goods_edit_page import GoodsEditPage
        return GoodsEditPage(self.driver)

    def click_goods_add_button(self):
        #         进入商品列表,点击添加按钮
        goods_add_button = '//*[text()="添加"]'
        self.driver.find_element(By.XPATH, goods_add_button).click()
        # import语句,放入函数中,防止循环导入
        # from tests.pages.goods_add_page import GoodsAddPage
        from shixun2.tests.page.goods_add_page import GoodsAddPage
        # 跳转至商品添加页面
        return GoodsAddPage(self.driver)

    def get_goods_name_text_list(self,expected_number=9):
        # 获取所有商品名称
        goods_name_col = '.is-center.el-table__cell'
        # 显式等待
        def wait(driver):
            # 编写显示等待的逻辑
            # 获取列表元素
            goods_name_ele_list = self.driver.find_elements(By.CSS_SELECTOR, goods_name_col)
            # 直到元素长度大于逾期值
            return len(goods_name_ele_list) > expected_number
            # 显式等待 使用自定义函数
        WebDriverWait(self.driver, 10).until(wait)
        goods_name_ele_list = self.driver.find_elements(By.CSS_SELECTOR, goods_name_col)
        goods_name_text_list = [ge.text for ge in goods_name_ele_list]
        return goods_name_text_list
goods_edit_page.py
# -*- coding: utf-8 -*-
# @Author  : 思齐
# @Time    : 2023/6/11 22:16
# @Function:
from __future__ import annotations
from selenium.webdriver.common.by import By

from shixun2.tests.page.base_page import BasePage
from shixun2.tests.page.goods_list_page import GoodsListPage

class GoodsEditPage(BasePage):
    '''
    商品编辑页面
    '''
    # 编辑商品名称
    def send_keys_goods_named(self,goods_name) -> GoodsEditPage:
        # 输入商品名称
        goods_name_input = '//*[text()="商品名称"]/../div/div/input'
        self.driver.find_element(By.XPATH, goods_name_input).clear()
        self.driver.find_element(By.XPATH, goods_name_input).send_keys(goods_name)
        return self
    def click_update_button(self) -> GoodsListPage:
        #         更新商品
        putaway_button = '//*[text()="更新商品"]'
        self.driver.find_element(By.XPATH, putaway_button).click()
        # 跳转到商品列表页面
        return GoodsListPage(self.driver)
goods_add_page.py
# -*- coding: utf-8 -*-
# @Author  : 思齐
# @Time    : 2023/6/11 22:16
# @Function:
from __future__ import annotations
from selenium.webdriver.common.by import By


from shixun2.tests.page.base_page import BasePage
from shixun2.tests.page.goods_list_page import GoodsListPage

class GoodsAddPage(BasePage):
    '''
    商品添加页面,集成父类BasePage用于复用driver
    '''
    def send_keys_goods_number(self,goods_number) -> GoodsAddPage:
        # 输入商品编号
        goods_number_input = '.el-input__inner'
        self.driver.find_element(By.CSS_SELECTOR, goods_number_input).send_keys(goods_number)
        return self

    def send_keys_goods_name(self,goods_name) -> GoodsAddPage:
        # 输入商品名称
        goods_name_input = '//*[text()="商品名称"]/../div/div/input'
        self.driver.find_element(By.XPATH, goods_name_input).send_keys(goods_name)
        return self

    def click_putaway_button(self):
        #上架
        putaway_button = '//*[text()="上架"]'
        self.driver.find_element(By.XPATH, putaway_button).click()
        # 跳转到商品列表页面
        return GoodsListPage(self.driver)
base_page.py
# -*- coding: utf-8 -*-
# @Author  : 思齐
# @Time    : 2023/6/11 22:16
# @Function:
from selenium import webdriver
class BasePage:
    '''
    公共基类,用于封装所有页面的公共方法
    '''

    def __init__(self, driver=None):
        # 只有driver是none才初始化
        if driver is None:
            # pytest初始化方法,每次运行前执行
            # 打开Edge浏览器
            self.driver = webdriver.Edge()
            # 最大化窗口
            self.driver.maximize_window()
            # 设置隐式等待,等待页面元素加载完成之后,(无法等待js渲染的元素)(显式等待解决这个问题)
            self.driver.implicitly_wait(10)
        else:
            # 复用driver
            self.driver = driver
pytest D:/PycharmProjects/shixun2/shixun2/tests
--alluredir=D:/PycharmProjects/shixun2/shixun2/docs
--clean-alluredir
allure serve D:/PycharmProjects/shixun2/shixun2/docs
test_litemall_po.py
# -*- coding: utf-8 -*-
# @Author  : 思齐
# @Time    : 2023/6/11 22:16
# @Function:
import time
import allure
from shixun2.tests.page.login_page import LoginPage
@allure.feature("商品管理功能")
class TestLitemallPo:
    '''
    litemall po 页面测试类
    '''

    def setup(self):
        # 执行每个函数之前更新时间戳,防止用例直接互相干扰
        self.nowtime = int(time.time())
    def setup_class(self):
        self.index_page = LoginPage().open_login_page().click_login_btn()
        #     定义一个唯一前缀,防止测试数据冲突
        self.PREFIX_NUM = 20202200
        self.nowtime = int(time.time())
        with allure.step("进入首页后,点击商品管理商品列表按钮"):
           self. goods_list_page = self.index_page.click_goods_manager_button().click_goods_list_button()

    # 完成编辑,删除,查询
    @allure.title("商品编辑测试用例")
    def test_goods_edit(self):
        '''
        商品编辑方法
        :return:
        '''
        with allure.step("点击添加按钮"):
            goods_add_page = self.goods_list_page.click_goods_add_button()
        time.sleep(2)
        with allure.step("进入商品添加页面,输入商品编号、商品名称、点击上架按钮"):
            goods_number = int(self.nowtime + self.PREFIX_NUM)
            goods_name = f"{self.PREFIX_NUM}_{str(self.nowtime)}_商品名称添加"
            good_list_page = goods_add_page.send_keys_goods_number(goods_number).send_keys_goods_name(goods_name).click_putaway_button()
        with allure.step("获取商品列表文本"):
            goods_name_text_list = good_list_page.get_goods_name_text_list()
        with allure.step("判断添加的名称是否存在"):
            assert goods_name in goods_name_text_list
        with allure.step("输入商品名称"):
            goods_name = f"{self.PREFIX_NUM}_{str(self.nowtime)}_商品名称添加"
            good_lists_page = self.goods_list_page.send_keys_goods_name(goods_name)
        with allure.step("点击查找按钮"):
            goods_search_button = good_lists_page.click_goods_search_button()
        with allure.step("获取商品列表文本"):
            goods_name_text_list = good_lists_page.get_goods_name_text_list()
        with allure.step("点击编辑按钮"):
            goods_edit_page = self.goods_list_page.click_goods_edit_button()
        time.sleep(2)
        with allure.step("进入商品编辑页面,更改商品名称"):
            goods_name2 = f"{self.PREFIX_NUM}_{str(self.nowtime)}_商品名称编辑"
            good_list_pages = goods_edit_page.send_keys_goods_named(goods_name2).click_update_button()
        with allure.step("输入搜索内容"):
            good_lists_page = self.goods_list_page.send_keys_goods_name(goods_name2)
        with allure.step("点击查找按钮"):
            goods_search_button = good_lists_page.click_goods_search_button()
        with allure.step("获取商品列表文本"):
            goods_name_text_list = good_lists_page.get_goods_name_text_list()
        with allure.step("断言:修改的是否在列表里"):
            assert goods_name2 in goods_name_text_list
    @allure.title("商品删除测试用例")
    def test_goods_del(self):
        # 删除不用新页面
        with allure.step("点击添加按钮"):
            goods_add_page = self.goods_list_page.click_goods_add_button()
        time.sleep(1)
        with allure.step("进入商品添加页面,输入商品编号、商品名称、点击上架按钮"):
            goods_number = int(self.nowtime + self.PREFIX_NUM)
            goods_name = f"{self.PREFIX_NUM}_{str(self.nowtime)}_商品名称添加"
            good_list_page = goods_add_page.send_keys_goods_number(goods_number).send_keys_goods_name(
                goods_name).click_putaway_button()
        with allure.step("获取商品列表文本"):
            goods_name_text_list = good_list_page.get_goods_name_text_list()
        with allure.step("判断添加的名称是否存在"):
            assert goods_name in goods_name_text_list
        with allure.step("输入商品名称"):
            goods_name = f"{self.PREFIX_NUM}_{str(self.nowtime)}_商品名称添加"
            good_lists_page = self.goods_list_page.send_keys_goods_name(goods_name)
        with allure.step("点击查找按钮"):
            goods_search_button = good_lists_page.click_goods_search_button()
        with allure.step("获取商品列表文本"):
            goods_name_text_list = good_lists_page.get_goods_name_text_list()
        with allure.step("点击删除按钮"):
            self.goods_list_page.click_goods_delete_button()
        time.sleep(2)
        with allure.step("输入搜索内容"):
            good_lists_page = self.goods_list_page.send_keys_goods_name(goods_name)
        with allure.step("点击查找按钮"):
            goods_search_button = good_lists_page.click_goods_search_button()
        with allure.step("获取商品列表文本"):
            goods_name_text_list = good_lists_page.get_goods_name_text_list()
        with allure.step("删除内容是否存在"):
            assert goods_name not in goods_name_text_list

    @allure.title("商品搜索测试用例")
    def test_goods_search(self):
        # 查找也不用新页面
        with allure.step("点击添加按钮"):
            goods_add_page = self.goods_list_page.click_goods_add_button()
        time.sleep(1)
        with allure.step("进入商品添加页面,输入商品编号、商品名称、点击上架按钮"):
            goods_number = int(self.nowtime + self.PREFIX_NUM)
            goods_name = f"{self.PREFIX_NUM}_{str(self.nowtime)}_商品名称添加"
            good_list_page = goods_add_page.send_keys_goods_number(goods_number).send_keys_goods_name(
                goods_name).click_putaway_button()
        with allure.step("获取商品列表文本"):
            goods_name_text_list = good_list_page.get_goods_name_text_list()
        with allure.step("判断添加的名称是否存在"):
            assert goods_name in goods_name_text_list
        with allure.step("输入商品名称"):
            goods_name = f"{self.PREFIX_NUM}_{str(self.nowtime)}_商品名称添加"
            good_lists_page = self.goods_list_page.send_keys_goods_name(goods_name)
        with allure.step("点击查找按钮"):
            goods_search_button = good_lists_page.click_goods_search_button()
        with allure.step("获取商品列表文本"):
            goods_name_text_list = good_lists_page.get_goods_name_text_list()
        with allure.step("在不在"):
            assert goods_name in goods_name_text_list
    @allure.title("商品添加测试用例")
    def test_goods_add(self):
        '''
        商品添加测试方法
        :return:
        '''
        with allure.step("点击添加按钮"):
            goods_add_page = self.goods_list_page.click_goods_add_button()
        time.sleep(1)
        with allure.step("进入商品添加页面,输入商品编号、商品名称、点击上架按钮"):
            goods_number = int(self.nowtime + self.PREFIX_NUM)
            goods_name = f"{self.PREFIX_NUM}_{str(self.nowtime)}_商品名称添加"
            good_list_page = goods_add_page.send_keys_goods_number(goods_number).send_keys_goods_name(goods_name).click_putaway_button()
        with allure.step("获取商品列表文本"):
            goods_name_text_list = good_list_page.get_goods_name_text_list()
        with allure.step("判断添加的名称是否存在"):
            assert goods_name in goods_name_text_list

你可能感兴趣的:(python,开发语言)