自动化测试框架的示例

 下面是一套简单的自动化测试框架的示例,使用Python和Selenium WebDriver来实现:

1,环境配置

首先,您需要安装Python和Selenium WebDriver,并设置相关环境变量。

2,目录结构

在项目根目录下,创建以下目录结构:

- tests/
  - __init__.py
  - test_login.py
- pages/
  - __init__.py
  - login_page.py
- utils/
  - __init__.py
  - config.py
  - driver.py

3,配置文件

config.py 文件中,您可以定义一些全局配置项,如浏览器类型、URL等:

class Config:
    # 浏览器类型
    BROWSER = "Chrome"

    # 网站URL
    URL = "https://example.com"

    # 等待时间
    WAIT_TIME = 10

4,WebDriver 封装

driver.py 文件中,您可以封装一个 WebDriver 类,用于管理浏览器的生命周期,如启动浏览器、关闭浏览器等:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options

from utils.config import Config


class WebDriver:
    def __init__(self):
        if Config.BROWSER == "Chrome":
            options = Options()
            options.add_argument("--disable-extensions")
            options.add_argument("--headless")
            self.driver = webdriver.Chrome(options=options)
        elif Config.BROWSER == "Firefox":
            self.driver = webdriver.Firefox()
        else:
            raise Exception("Unsupported browser")

        self.driver.maximize_window()
        self.driver.implicitly_wait(Config.WAIT_TIME)

    def __enter__(self):
        return self.driver

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.driver.quit()

5,页面对象模型

pages 目录下,您可以创建一个 login_page.py 文件,表示登录页面的对象模型:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from utils.config import Config


class LoginPage:
    def __init__(self, driver):
        self.driver = driver

    def open(self):
        self.driver.get(Config.URL)

    def login(self, username, password):
        self.driver.find_element(By.NAME, "username").send_keys(username)
        self.driver.find_element(By.NAME, "password").send_keys(password)
        self.driver.find_element(By.NAME, "submit").click()

    def is_logged_in(self):
        try:
            WebDriverWait(self.driver, Config.WAIT_TIME).until(
                EC.presence_of_element_located((By.LINK_TEXT, "Logout"))
            )
            return True
        except:
            return False

6,测试用例

tests 目录下,您可以创建一个 test_login.py 文件,编写一个测试用例来测试登录功能:

from utils.driver import WebDriver
from pages.login_page import LoginPage


def test_login():
    with WebDriver() as driver:
        login_page = LoginPage(driver)
        login_page

在 test_login.py 中,您可以编写测试函数 test_successful_login,该函数使用预定义的用户名和密码进行登录,并验证登录成功后页面是否显示了预期的内容。如果验证成功,则测试通过。如果出现任何错误,则测试失败。

示例代码如下:

import unittest
from selenium import webdriver

class TestLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://www.example.com/login")
        
    def test_successful_login(self):
        username = "example_user"
        password = "example_password"
        
        # Enter username and password
        self.driver.find_element_by_id("username").send_keys(username)
        self.driver.find_element_by_id("password").send_keys(password)
        self.driver.find_element_by_id("login_button").click()
        
        # Verify that the user is redirected to the dashboard page
        dashboard_header = self.driver.find_element_by_xpath("//h1[contains(text(),'Dashboard')]")
        self.assertEqual(dashboard_header.text, "Dashboard")
        
    def tearDown(self):
        self.driver.quit()

在上面的示例中,setUp() 和 tearDown() 函数是测试框架提供的用于初始化和清理测试环境的函数。test_successful_login() 函数包含要测试的代码,并使用断言来验证测试结果是否正确。

编写完测试用例后,您可以使用测试框架来执行测试。对于unittest,可以使用以下命令执行测试:

python -m unittest discover -v tests/

该命令将发现 tests 目录下的所有测试,并输出测试结果。

这是一个简单的示例,实际情况可能更加复杂。但是,使用类似的步骤和工具,您可以构建一个自动化测试框架,用于测试您的应用程序的不同方面。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值