AI自动化测试速成(Pytest框架)

将上面的附件发送给AI并发送根据附件提示词完成代码编写 可得到以下代码**:**

复制代码
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By  # 导入 By 模块


@pytest.fixture(scope="function")
def browser():
    # 启动浏览器
    driver = webdriver.Chrome()
    yield driver
    # 关闭浏览器
    driver.quit()


import time


def test_login(browser):
    # 1. 打开登录页面
    browser.get("https://hmshop-test.itheima.net/Home/user/login.html")
    # 添加注释:访问登录页面

    # 2. 输入用户名 13600001111
    username_input = browser.find_element(By.CSS_SELECTOR, "#username")
    username_input.send_keys("13600001111")
    # 添加注释:定位用户名输入框并输入用户名

    # 3. 输入密码 123456
    password_input = browser.find_element(By.CSS_SELECTOR, "#password")
    password_input.send_keys("123456")
    # 添加注释:定位密码输入框并输入密码

    # 4. 输入图片验证码 8888
    captcha_input = browser.find_element(By.CSS_SELECTOR, "#verify_code")
    captcha_input.send_keys("8888")
    # 添加注释:定位验证码输入框并输入验证码

    # 5. 点击登录按钮
    login_button = browser.find_element(By.CSS_SELECTOR, "#loginform > div > div.login_bnt > a")
    login_button.click()
    # 添加注释:定位登录按钮并点击

    # 6. 等待一段时间,观察登录结果(可根据实际情况调整等待时间)
    time.sleep(5)
    # 添加注释:等待 5 秒,观察登录结果

    # 7. 验证登录是否成功(这里简单检查 URL 是否跳转)
    current_url = browser.current_url
    assert "index" in current_url, "登录失败"
    # 添加注释:验证登录是否成功,检查 URL 是否包含 "index"

运行结果如下图所示

相关推荐
2025年一定要上岸2 天前
【pytest高阶】源码的走读方法及插件hook
运维·前端·python·pytest
惜.己3 天前
pytest中使用ordering控制函数的执行顺序
开发语言·python·pytest
惜.己4 天前
pytest中使用skip跳过某个函数
开发语言·python·测试工具·pytest
程序员小远5 天前
Pytest+Selenium UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·ui·pytest
惜.己7 天前
pytest简单使用和生成测试报告
开发语言·python·pytest
better-tomorrow7 天前
pytest-log
pytest
别在内卷了10 天前
测试学习之——Pytest Day4
python·学习·pytest
别在内卷了11 天前
测试学习之——Pytest Day3
python·学习·pytest
土小帽软件测试11 天前
mac系统安装、启动Jenkins,创建pytest接口自动化任务
macos·jenkins·pytest
Chasing__Dreams12 天前
pytest--1--pytest-mock常用的方法
pytest