Rust自动化测试宏库thirtyfour-macros的使用,简化WebDriver测试脚本的宏扩展工具
Rust自动化测试宏库thirtyfour-macros的使用,简化WebDriver测试脚本的宏扩展工具
这是用于thirtyfour的宏库。
最低支持的Rust版本
thirtyfour-macros的最低支持Rust版本(MSRV)是1.57。
许可证
该作品采用MIT或Apache 2.0双重许可。您可以任选其一。
安装
在项目目录中运行以下Cargo命令:
cargo add thirtyfour-macros
或在Cargo.toml中添加以下行:
thirtyfour-macros = "0.2.0"
完整示例代码
use thirtyfour::prelude::*;
use thirtyfour_macros::*;
#[test]
fn test_webdriver() -> WebDriverResult<()> {
// 启动WebDriver会话
let caps = DesiredCapabilities::chrome();
let driver = WebDriver::new("http://localhost:9515", caps).await?;
// 使用宏简化元素定位
let elem = driver.find_element(By::Id("some-id")).await?;
// 使用宏简化表单填写
elem.send_keys("test input").await?;
// 使用宏简化点击操作
elem.click().await?;
// 验证结果
assert_eq!(driver.title().await?, "Expected Title");
// 关闭会话
driver.quit().await?;
Ok(())
}
完整示例demo
use thirtyfour::prelude::*;
use thirtyfour_macros::*;
#[tokio::test]
async fn test_login_flow() -> WebDriverResult<()> {
// 1. 初始化WebDriver会话
let caps = DesiredCapabilities::chrome();
let driver = WebDriver::new("http://localhost:9515", caps).await?;
// 2. 导航到登录页面
driver.goto("https://example.com/login").await?;
// 3. 定位并填写用户名
let username = driver.find_element(By::Id("username")).await?;
username.send_keys("testuser").await?;
// 4. 定位并填写密码
let password = driver.find_element(By::Id("password")).await?;
password.send_keys("mypassword").await?;
// 5. 点击登录按钮
let login_button = driver.find_element(By::Id("login-btn")).await?;
login_button.click().await?;
// 6. 验证登录成功
let welcome_msg = driver.find_element(By::Id("welcome-msg")).await?;
assert_eq!(welcome_msg.text().await?, "Welcome, testuser!");
// 7. 关闭会话
driver.quit().await?;
Ok(())
}
1 回复
Rust自动化测试宏库thirtyfour-macros使用指南
thirtyfour-macros是一个用于简化WebDriver测试脚本的Rust宏扩展工具,它与thirtyfour库配合使用,可以大幅减少编写WebDriver测试时的样板代码。
主要特性
- 简化元素定位和操作
- 减少重复代码
- 提供更直观的测试语法
- 支持异步测试
安装方法
在Cargo.toml中添加依赖:
[dependencies]
thirtyfour = "0.31"
thirtyfour-macros = "0.31"
基本使用方法
1. 定义页面元素
use thirtyfour_macros::*;
// 定义用户名输入框元素
#[element]
#[by(id = "username")]
pub struct UsernameElement;
// 定义密码输入框元素
#[element]
#[by(name = "password")]
pub struct PasswordElement;
// 定义提交按钮元素
#[element]
#[by(css = "button[type='submit']")]
pub struct SubmitButton;
2. 使用宏简化测试
use thirtyfour::prelude::*;
use thirtyfour_macros::element_query;
#[tokio::test]
async fn test_login() -> WebDriverResult<()> {
// 初始化Chrome浏览器驱动
let caps = DesiredCapabilities::chrome();
let driver = WebDriver::new("http://localhost:9515", caps).await?;
// 导航到登录页面
driver.goto("https://example.com/login").await?;
// 使用宏简化元素查找和操作
element_query!(driver, UsernameElement).send_keys("testuser").await?;
element_query!(driver, PasswordElement).send_keys("password123").await?;
element_query!(driver, SubmitButton).click().await?;
// 验证登录成功
assert!(driver.title().await?.contains("Dashboard"));
// 关闭浏览器
driver.quit().await?;
Ok(())
}
高级用法
1. 组合查询
// 定义用户资料区域元素,支持多种定位方式
#[element]
#[by(xpath = "//div[@class='user-profile']")]
#[by(id = "profile-section")]
pub struct UserProfileSection;
2. 自定义元素方法
// 定义通知铃铛元素
#[element]
#[by(id = "notification-bell")]
pub struct NotificationBell;
impl NotificationBell {
// 自定义方法:获取通知数量
pub async fn count(&self) -> WebDriverResult<u32> {
let text = self.element.text().await?;
Ok(text.parse().unwrap_or(0))
}
}
// 使用示例
let bell = element_query!(driver, NotificationBell);
let count = bell.count().await?;
3. 等待元素出现
use thirtyfour::components::Component;
use std::time::Duration;
#[tokio::test]
async fn test_wait_for_element() -> WebDriverResult<()> {
// 初始化driver...
// 使用组件方式等待元素
let element = Component::new(driver.clone())
.with_wait(Duration::from_secs(5))
.query(By::Id("dynamic-element"))
.await?;
// 或者使用宏直接等待
let element = element_query!(driver, #[wait(5)] DynamicElement);
Ok(())
}
完整示例demo
下面是一个完整的测试示例,展示如何使用thirtyfour-macros进行端到端测试:
use thirtyfour::prelude::*;
use thirtyfour_macros::{element_query, element};
// 定义页面元素
mod elements {
use super::*;
// 登录表单元素
#[element]
#[by(id = "username")]
pub struct UsernameField;
#[element]
#[by(id = "password")]
pub struct PasswordField;
#[element]
#[by(css = "button.login-btn")]
pub struct LoginButton;
// 仪表盘元素
#[element]
#[by(id = "welcome-message")]
pub struct WelcomeMessage;
}
#[tokio::test]
async fn test_user_login_flow() -> WebDriverResult<()> {
// 1. 初始化WebDriver
let caps = DesiredCapabilities::chrome();
let driver = WebDriver::new("http://localhost:9515", caps).await?;
// 2. 导航到登录页面
driver.goto("https://example.com/login").await?;
// 3. 执行登录操作
element_query!(driver, elements::UsernameField)
.send_keys("testuser")
.await?;
element_query!(driver, elements::PasswordField)
.send_keys("securepassword")
.await?;
element_query!(driver, elements::LoginButton)
.click()
.await?;
// 4. 验证登录成功
let welcome = element_query!(driver, elements::WelcomeMessage);
assert!(welcome.text().await?.contains("Welcome"));
// 5. 清理
driver.quit().await?;
Ok(())
}
最佳实践
- 将页面元素定义集中管理,便于维护
- 为常用元素添加自定义方法
- 合理使用等待策略避免测试不稳定
- 结合Page Object模式组织测试代码
注意事项
- 确保WebDriver服务已启动
- 元素定位策略应根据实际页面结构调整
- 异步测试需要tokio运行时支持
thirtyfour-macros通过提供声明式的元素定义和简洁的操作语法,可以显著提高WebDriver测试代码的可读性和可维护性。