第三届火焰杯软件测试初赛web自动化代码

题目

base代码块,负责基本方法的重写

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

class BasePage:

    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)
        self.window_handles = None

    # 访问URL
    def get(self, url):
        self.driver.get(url)

    # 跳转新窗口
    def switch_to(self):
        windows = self.driver.window_handles
        self.driver.switch_to.window(windows[-1])

    # 元素定位(单个)
    def find_element(self, loc):
        return self.driver.find_element(*loc)

    # 元素定位(多个)
    def find_elements(self, loc):
        return self.driver.find_elements(*loc)

    # 输入
    def input(self, loc, txt):
        self.find_element(loc).send_keys(txt)

    # 清空
    def clear(self,loc):
        self.find_element(loc).clear()

    # 点击
    def click(self, loc):
        self.find_element(loc).click()

    # 等待
    def sleep(self, time):
        sleep(time)

    # 关闭
    def close(self):
        self.driver.quit()

    # 鼠标移动指定位置
    def move_to(self,loc):
        ac = ActionChains(self.driver)

        # 鼠标移动到 元素上
        ac.move_to_element(
            self.driver.find_element(*loc)
        ).perform()

 page页面,负责网页元素定位已经页面操作跳转,有用到显示等待自定义方法的重写

from pages.base_page import BasePage
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
class login(BasePage):

    # 登录按钮
    login_username = (By.XPATH, "//*[@name='username']")
    login_password = (By.XPATH, "//*[@name='password']")
    submit_btn = (By.CSS_SELECTOR, ".el-button--primary")

    # 商品列表
    goods_manage = (By.XPATH, "//*[text()='商品管理']")
    goods_up = (By.XPATH, "//*[text()='商品上架']")

    # 商品上架
    goods_num = (By.XPATH, "//*[text()='商品编号']/..//input")
    goods_name = (By.XPATH, "//*[text()='商品名称']/..//input")
    goods_money = (By.XPATH, "//*[text()='市场售价']/..//input")
    up_btn = (By.XPATH, "//*[text()='上架']")

    # 商品搜索
    input_goodsName = (By.XPATH, "//*[@placeholder='请输入商品名称']")
    input_goodsNum = (By.XPATH, "//*[@placeholder='请输入商品编号']")
    search_goods = (By.CSS_SELECTOR, ".el-icon-search")
    lists_element = (By.XPATH, "//tbody/tr/td[3]/div")
    lists_element_edit = (By.XPATH,"//*[text()='编辑'][1]")

    def __init__(self):
        super().__init__()
    # 登录界面
    def do_login(self,username,password):
        self.clear(self.login_username)
        self.input(self.login_username,username)

        self.clear(self.login_password)
        self.input(self.login_password,password)

        self.click(self.submit_btn)

    # 商品列表页面
    def go_goodsListPage(self):
        self.click(self.goods_manage)
        self.click(self.goods_up)

    # 添加商品
    def add_goods(self,goodNum,goodName,goodMoney):
        #
        self.click(self.goods_manage)
        self.click(self.goods_up)

        self.input(self.goods_num,goodNum)
        self.input(self.goods_name,goodName)
        self.input(self.goods_money,goodMoney)
        self.click(self.up_btn)


    # 搜索商品并返回搜索列表
    def search_good(self,goodsName=None,goodsNum=None):

        if goodsName != None:
            self.input(self.input_goodsName,goodsName)
        if goodsNum != None:
            self.input(self.input_goodsNum,goodsNum)
        self.click(self.search_goods)
        self.sleep(1)
        lists = self.find_elements(self.lists_element)
        return lists

    # 在返回的列表中第一行点击商品详情并返回商品名称编号
    def rt_goods_num_and_name(self,goodsName=None,goodsNum=None):
        lists = self.search_good(goodsName=goodsName,goodsNum=goodsNum)
        if lists == []:
            return None
        self.click(self.lists_element_edit)

        # 加入显示等待,优化时间
        try:
            WebDriverWait(self.driver,10).until(

                lambda driver: self.find_element(self.goods_name).get_attribute('value') != ''
            )
            text1 = self.find_element(self.goods_name).get_attribute('value')
            text2 = self.find_element(self.goods_num).get_attribute('value')
            return (text1, text2)
        except TimeoutException:
            print("输入框文字未显示")
            return None





test代码块,负责测试用例的运行,与网页操作分离,体现pom模式的分装,使用pytest的参数化,使代码有复用性

from pages.login_page import login
import time
from log.logger import logger
import pytest

class TestPage:
    timeStamp = time.time()
    def setup_class(self):
        self.driver = login()
        self.driver.get("https://litemall.hogwarts.ceshiren.com/")
        self.driver.do_login('hogwarts','test12345')
    def teardown_class(self):
        self.driver.close()

    @pytest.mark.parametrize("name",[
        'l',
        'lss',
        'l'*100

    ])
    def test_add(self,name):

        self.driver.add_goods(f"{self.timeStamp}+{name}+num",f"{self.timeStamp}+{name}+name",200)
        lists = self.driver.search_good(goodsNum=f"{self.timeStamp}+{name}+num",goodsName=f"{self.timeStamp}+{name}+name")

        logger.info(f"预期为{self.timeStamp}+{name}+name,实际为{lists[0].text}")
        assert lists[0].text == f"{self.timeStamp}+{name}+name"

    @pytest.mark.parametrize("name", [
        'l',
        'lss',
        'l' * 100

    ])
    def test_search(self,name):
        timeStamp2 = time.time()
        self.driver.add_goods(f"{timeStamp2}+{name}+num",f"{timeStamp2}+{name}+name",200)
        lists = self.driver.rt_goods_num_and_name(goodsNum=f"{timeStamp2}+{name}+num",goodsName=f"{timeStamp2}+{name}+name")

        logger.info(f"预期为{timeStamp2}+{name}+name,{timeStamp2}+{name}+num,实际为{lists}")
        assert lists == (f"{timeStamp2}+{name}+name",f"{timeStamp2}+{name}+num")

log文件

import logging

# 创建logger实例
logger = logging.getLogger('simple_example')

# 设置日志级别
logger.setLevel(logging.DEBUG)

# 创建流处理器
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# 日志打印格式
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# 添加格式配置
ch.setFormatter(formatter)

# 添加日志配置
logger.addHandler(ch)

项目目录

请在评论区分享你的建议和疑问哦,多多评论分享!!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值