如何将UI数据解析到Cucumber feature文件的变量中?
Selenium+Cucumber获取UI数据并绑定变量的实现方案
1. 编写.feature场景文件
如果是单条测试用例,直接写普通场景即可;如果要批量测试多个customerID,用场景大纲结合示例表格。
单条测试用例写法
Feature: 客户订单信息验证 Scenario: 获取并验证指定客户的姓名与最后订单ID Given 搜索customerID为3434423432的客户 When 从客户总览页面提取客户姓名和最后订单ID Then 对比订单列表数据,验证客户姓名和最后订单ID匹配
批量测试用例写法
Feature: 客户订单信息验证 Scenario Outline: 批量验证客户姓名与最后订单ID Given 搜索customerID为<customerID>的客户 When 从客户总览页面提取客户姓名和最后订单ID Then 对比订单列表数据,验证客户姓名和最后订单ID匹配 Examples: | customerID | | 3434423432 | | 9876543210 |
2. 步骤定义与Selenium数据获取(Java示例)
通过自定义数据类存储UI获取的数据,实现步骤间的变量传递:
第一步:定义数据存储类
// 存储客户信息的实体类 public class CustomerInfo { private String customerName; private String lastOrderId; // Getter和Setter方法 public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public String getLastOrderId() { return lastOrderId; } public void setLastOrderId(String lastOrderId) { this.lastOrderId = lastOrderId; } }
第二步:编写步骤定义类
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.junit.Assert; import io.cucumber.java.After; import io.cucumber.java.Before; import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; import io.cucumber.java.en.When; import java.time.Duration; public class CustomerVerificationSteps { private WebDriver driver; private CustomerInfo customerInfo = new CustomerInfo(); // 初始化浏览器驱动 @Before public void setupBrowser() { driver = new ChromeDriver(); driver.manage().window().maximize(); } @Given("搜索customerID为{string}的客户") public void searchCustomer(String customerId) { // 打开客户搜索页面(替换为你的实际URL) driver.get("https://your-app-url/customer-search"); // 输入customerID并搜索 driver.findElement(By.id("customer-id-input")).sendKeys(customerId); driver.findElement(By.id("search-submit-btn")).click(); // 显式等待客户总览页面加载完成 new WebDriverWait(driver, Duration.ofSeconds(10)).until( ExpectedConditions.visibilityOfElementLocated(By.id("customer-overview-container")) ); } @When("从客户总览页面提取客户姓名和最后订单ID") public void extractCustomerInfo() { // 从UI获取客户姓名,替换为你的实际元素定位器 String name = driver.findElement(By.cssSelector(".customer-profile .name")).getText().trim(); customerInfo.setCustomerName(name); // 从UI获取最后订单ID,替换为你的实际元素定位器 String orderId = driver.findElement(By.xpath("//div[@class='last-order']/span[@class='order-id']")).getText().trim(); customerInfo.setLastOrderId(orderId); } @Then("对比订单列表数据,验证客户姓名和最后订单ID匹配") public void verifyCustomerInfoAgainstOrderList() { // 跳转到订单列表页面 driver.findElement(By.linkText("订单列表")).click(); // 等待订单列表加载 new WebDriverWait(driver, Duration.ofSeconds(10)).until( ExpectedConditions.visibilityOfElementLocated(By.id("order-table")) ); // 获取订单列表中该客户的最后一条订单信息(替换为你的实际定位逻辑) String actualName = driver.findElement(By.xpath("//table[@id='order-table']/tbody/tr[last()]/td[2]")).getText().trim(); String actualOrderId = driver.findElement(By.xpath("//table[@id='order-table']/tbody/tr[last()]/td[1]")).getText().trim(); // 断言验证 Assert.assertEquals("客户姓名不匹配", customerInfo.getCustomerName(), actualName); Assert.assertEquals("最后订单ID不匹配", customerInfo.getLastOrderId(), actualOrderId); } // 关闭浏览器 @After public void teardownBrowser() { if (driver != null) { driver.quit(); } } }
3. 核心注意事项
- 数据传递:用自定义实体类存储UI获取的数据,避免步骤间变量混乱;如果是多线程并行测试,改用Cucumber依赖注入(如PicoContainer)传递上下文对象,保证线程安全。
- 显式等待:必须用
WebDriverWait等待元素加载,避免因页面未渲染完成导致的元素查找失败。 - 定位器准确性:替换示例中的元素定位器(如
By.id、By.xpath)为你项目中实际的UI元素定位符。 - 断言逻辑:根据业务需求调整验证逻辑,比如订单列表需要根据customerID筛选后再取最后一条,而不是直接取表格最后一行。
内容的提问来源于stack exchange,提问作者Long Phạm Hoàng
相关产品推荐
相关产品推荐

