web自动化(3)——项目实战之流程用例编写

发布时间:2023年12月26日

我的示例项目是一个管理后台项目,各位也可以使用自己的项目进行用例流程编写

业务流程:登录后点击贷款管理——新增贷款——填写表单后提交新增。

我所使用的这个流程中包含了框架切换、输入框、点击、下拉选择框、文件上传、时间控件等。

然后这里还将之前验证码识别和cookies的保存加载等方法进行封装,新建funcs.py,将方法移入该文件中,然后使用时进行导入即可

公共方法funcs.py

import json

import requests

def img1code(path):
    resp = requests.post(
        "https://upload.chaojiying.net/Upload/Processing.php",
        data={
            "user": "***",  # 需要自己注册输入自己的用户名密码
            "pass": "***",
            "softid": "96002",
            "codetype": "1902",  # 在官网根据自己的验证码选择验证码类型
        },
        files={"userfile": open(path, "rb")},  # 验证码截图
    )
    if resp.json()["err_no"] == 0:
        print("识别成功")
        return resp.json()["pic_str"]

    else:
        print("识别失败", resp.json()["err_str"])
        return False


def save_cookies(driver):
    """获取cookies将其存入本地文件"""
    cookies = driver.get_cookies()  # 获取cookies
    with open("cookies.json", "w") as f:
        f.write(json.dumps(cookies))


def load_cookies(driver):
    """从本地文件加载cookies"""
    driver.get('http://47.107.116.139/fangwei/m.php?m=Public&a=login&')
    with open("cookies.json") as f:
        _data = f.read()
        if _data:
            cookies = json.loads(_data)
        else:
            cookies = []
    for cookie in cookies:
        driver.add_cookie(cookie)  # 使用cookies
    driver.refresh()  # 刷新页面,向服务器发送cookies


def is_login(driver):
    """判断是否登录"""
    if '管理员登录' in driver.title:
        return False
    else:
        print('已经登录')
        return True

管理后台admin.py

from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from funcs import load_cookies, is_login, img1code, save_cookies

driver = Chrome()
driver.implicitly_wait(10)  # 隐式等待
load_cookies(driver)  # 浏览器启动后,加载cookies
driver.refresh()  # 刷新页面,向服务器发送cookies
# 判断:只有未登录,才执行登陆流程
if is_login(driver) is False:
    driver.get('http://47.107.116.139/fangwei/m.php?m=Public&a=login&')
    driver.find_element(By.XPATH, '//*[@id="verify"]').screenshot("code.png")  # 保存验证码图片
    code = img1code("code.png")  # 识别验证码
    if code:
        driver.find_element(By.XPATH, '/html/body/form/table/tbody/tr/td[3]/table/tbody/tr[2]/td[2]/input').send_keys(
            'admin')  # 输入用户名
        driver.find_element(By.XPATH, '/html/body/form/table/tbody/tr/td[3]/table/tbody/tr[3]/td[2]/input').send_keys(
            'msjy123')  # 输入密码
        driver.find_element(By.XPATH, '/html/body/form/table/tbody/tr/td[3]/table/tbody/tr[5]/td[2]/input').send_keys(
            code)  # 输入验证码
        driver.find_element(By.XPATH, '//*[@id="login_btn"]').click()  # 点击登录
    else:
        print("验证码识别失败,无法登录")

# 点击贷款管理
frame = driver.find_element(By.XPATH, '/html/frameset/frame[1]')  # 进入框架
driver.switch_to.frame(frame)
driver.find_element(By.XPATH, '//*[@id="navs"]/ul/li[2]/a').click()
driver.switch_to.default_content()  # 退出框架

# 点击全部贷款
frame = driver.find_element(By.XPATH, '//*[@id="menu-frame"]')  # 进入框架
driver.switch_to.frame(frame)
driver.find_element(By.XPATH, '/html/body/dl[1]/dd[1]/a').click()
driver.switch_to.default_content()  # 退出框架

# 定位到框架后再点击新增贷款
frame = driver.find_element(By.XPATH, '//*[@id="main-frame"]')  # 进入框架
driver.switch_to.frame(frame)
driver.find_element(By.XPATH, '/html/body/div[2]/div[3]/input[1]').click()
driver.find_element(By.XPATH, '//*[@id="colorpickerField"]').send_keys('#f00')  # 颜色
driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[4]/td[2]/input').send_keys(
    'wxm需要借款1个亿')  # 贷款名称
driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[5]/td[2]/input').send_keys('wxm借1个亿')  # 简短名称
driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[6]/td[2]/input[1]').send_keys('beifan')  # 会员名称
driver.find_element(By.XPATH, '//strong[text()="beifan"]').click()  # 下拉选择框选择会员
driver.find_element(By.XPATH, '//*[@id="citys_box"]/div[1]/div[2]/input[1]').click()  # 所在城市
# 分类-房产抵押
el = driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[8]/td[2]/select')
Select(el).select_by_visible_text('|--房产抵押标')
# 担保机构
el = driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[9]/td[2]/select')
Select(el).select_by_visible_text('担保机构01')
# # 担保范围
# driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[10]/td[2]/select')
# Select(el).select_by_visible_text('本金')
# 贷款缩略图
driver.find_element(By.XPATH,
                    '/html/body/div[2]/form/table[1]/tbody/tr[14]/td[2]/span/div[1]/div/div/button').click()  # 图片上传
driver.find_element(By.XPATH, '/html/body/div[6]/div[1]/div[2]/div/div[1]/ul/li[2]').click()  # 本地上传
driver.find_element(By.XPATH, '//input[@type="file"]').send_keys(r'D:\pythonProject2\code.png')  # 发送文件
driver.find_element(By.XPATH, '/html/body/div[6]/div[1]/div[3]/span[1]/input').click()  # 确定

el = driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[15]/td[2]/select')  # 借款用途
Select(el).select_by_visible_text('个人消费')
el = driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[16]/td[2]/select')  # 还款方式
Select(el).select_by_visible_text('到期还本息')
el = driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[17]/td[2]/select')  # 借款合同范本
Select(el).select_by_visible_text('等额本息合同范本【担保】')
el = driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[18]/td[2]/select')  # 转让合同范本
Select(el).select_by_visible_text('付息还本合同范本【普通】')

# 借款金额
driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[19]/td[2]/input').clear()
driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[19]/td[2]/input').send_keys('500000')
# 投标类型
el = driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[21]/td[2]/select')  # 转让合同范本
Select(el).select_by_visible_text('按金额')
# 最低投标金额
driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[22]/td[2]/input').clear()
driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[22]/td[2]/input').send_keys('100')
# 年利率
driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[27]/td[2]/input').send_keys('5')
# 筹标期限
driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[28]/td[2]/input').send_keys('30')
# 贷款描述
frame = driver.find_element(By.XPATH,
                            '/html/body/div[2]/form/table[1]/tbody/tr[30]/td[2]/div/div/div[2]/iframe')  # 进入框架
driver.switch_to.frame(frame)
driver.find_element(By.XPATH, '/html/body').send_keys('abcd')
driver.switch_to.default_content()  # 退出框架
# 借款状态
frame = driver.find_element(By.XPATH, '//*[@id="main-frame"]')
driver.switch_to.frame(frame)
driver.find_element(By.XPATH, '/html/body/div[2]/form/table[1]/tbody/tr[33]/td[2]/label[1]/input').click()
# 开始时间
el = driver.find_element(By.XPATH, '//*[@id="start_time"]')
driver.execute_script("arguments[0].scrollIntoView()", el)
driver.execute_script("arguments[0].value='2023-11-20 18:02:02'", el)  # 通过js强制输入
# 新增
driver.find_element(By.XPATH, '/html/body/div[2]/form/table[6]/tbody/tr[2]/td[2]/input[4]').click()
# 断言添加成功
el = driver.find_element(By.XPATH, '/html/body/div/table/tbody/tr[3]/td')
msg = el.text  # 元素中的文本
if msg == '添加成功':
    print('ok')
else:
    print(f"fail", msg)
save_cookies(driver)  # 浏览器关闭前,保存cookies
driver.quit()

前台user.py

import time

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

driver = Chrome()
driver.implicitly_wait(10)  # 隐式等待
# 访问前台
driver.get('http://47.107.116.139/fangwei/index.php')
driver.find_element(By.XPATH, '//*[@id="user_head_tip"]/a[1]').click()  # 点击立即登录按钮
driver.find_element(By.XPATH, '//*[@id="login-email-address').send_keys('admin')  # 输入用户名
driver.find_element(By.XPATH, '//*[@id="login-password"]').send_keys('msjy123')  # 输入密码
driver.find_element(By.XPATH, '//*[@id="ajax-login-submit"]').click()  # 点击登录按钮
time.sleep(1)
driver.get('http://47.107.116.139/fangwei/index.php?ctl=deal&id=25370&preview=1')
driver.find_element(By.XPATH, '//*[@id="J_BIDMONEY"]').send_keys('100')  # 输入投资金额
driver.find_element(By.XPATH, '//*[@id="tz_link"]').click()  # 点击立即投资
driver.find_element(By.XPATH, '//*[@id="J_bid_password"]').send_keys('msjy123')  # 输入支付密码
driver.find_element(By.XPATH, '//*[@id="J_bindpassword_btn"]').click()  # 点击确定按钮
# 提示信息
msg = WebDriverWait(driver, 10).until(
    lambda x: driver.find_element(
        By.XPATH, '//*[@id="fanwe_success_box"]/table/tbody/tr/td[2]/div[2]'
    ).text
)
print(msg)

以上代码缺点:

  1. 没有层次,不好维护
  2. 无法复用,代码工作大
  3. 无法计入pytest,参数/失败重试、自动截图、测试报告

因此我们必须要对代码进行封装,下章我们将对代码使用POM设计进行重构

文章来源:https://blog.csdn.net/weixin_42833736/article/details/135134783
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。