You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Selenium UI测试的Fluent API分支流程处理方案问询

解决Fluent API在Selenium UI测试中的页面跳转限制问题

你已经找对了Fluent API的设计方向,核心问题就是ISecondPage.FillForm()的返回类型没处理好,导致无法限制后续的合法操作。咱们直接重构接口结构,让链式调用的每一步都只能看到当前页面允许的操作:

第一步:重新设计接口,明确每个阶段的合法操作

首先,让每个接口只负责对应页面的操作,或者特定阶段的合法动作。重点是给第二页填完表单后的状态专门定义一个接口,只包含返回第一页和跳转到最后页这两个选项:

// 登录后进入的第一页接口
public interface IFirstPage { 
    void WithText(); 
    IGoToSecondPage FillForm(); 
} 

// 第一页填完表单后,用来跳转到第二页的接口
public interface IGoToSecondPage { 
    ISecondPage GoToSecondPage(); 
} 

// 第二页接口:填完表单后返回专属操作接口
public interface ISecondPage { 
    ISecondPagePostActions FillForm(); 
} 

// 第二页填完表单后的合法操作:只能返回第一页或跳去最后一页
public interface ISecondPagePostActions { 
    IFirstPage GoBackToFirstPage(); 
    ILastPage ProceedToLastPage(); 
} 

// 最后一页接口
public interface ILastPage { 
    void SetSomething(); 
    ISecondPage GoBackToSecondPage(); 
} 

第二步:实现各个页面类,完成链式调用逻辑

接下来实现这些接口,让每个方法执行对应的Selenium操作后,返回下一个阶段的接口实例,确保链式调用的流畅性:

public class FirstPage : IFirstPage, IGoToSecondPage { 
    public void WithText() { 
        // 这里写第一页输入文本的Selenium操作,比如:
        // driver.FindElement(By.Id("text-input")).SendKeys("test text");
    } 

    public IGoToSecondPage FillForm() { 
        // 这里写第一页填写表单的Selenium操作
        return this; // 返回当前实例,因为它实现了IGoToSecondPage
    } 

    public ISecondPage GoToSecondPage() { 
        // 这里写跳转到第二页的操作,比如点击下一步按钮
        // driver.FindElement(By.Id("next-btn")).Click();
        return new SecondPage(); 
    } 
} 

public class SecondPage : ISecondPage, ISecondPagePostActions { 
    public ISecondPagePostActions FillForm() { 
        // 这里写第二页填写表单的Selenium操作
        return this; // 返回当前实例,因为它实现了ISecondPagePostActions
    } 

    public IFirstPage GoBackToFirstPage() { 
        // 这里写返回第一页的操作,比如点击返回按钮
        // driver.FindElement(By.Id("back-btn")).Click();
        return new FirstPage(); 
    } 

    public ILastPage ProceedToLastPage() { 
        // 这里写跳转到最后一页的操作,比如点击完成按钮
        // driver.FindElement(By.Id("finish-btn")).Click();
        return new LastPage(); 
    } 
} 

public class LastPage : ILastPage { 
    public void SetSomething() { 
        // 这里写最后页的设置操作
        // driver.FindElement(By.Id("setting-input")).SendKeys("config");
    } 

    public ISecondPage GoBackToSecondPage() { 
        // 这里写从最后页返回第二页的操作
        // driver.FindElement(By.Id("back-to-second-btn")).Click();
        return new SecondPage(); 
    } 
} 

第三步:验证链式调用的合法性

现在你可以按照预期的流程编写测试脚本,而且IDE会自动提示当前阶段的合法操作,完全符合你的需求:

示例1:从第二页返回第一页继续操作

DoLogin()
    .WithText()
    .FillForm()
    .GoToSecondPage()
    .FillForm()
    .GoBackToFirstPage()
    .WithText(); // 回到第一页后可以继续执行第一页的操作

示例2:从第二页跳转到最后页

DoLogin()
    .WithText()
    .FillForm()
    .GoToSecondPage()
    .FillForm()
    .ProceedToLastPage()
    .SetSomething(); // 进入最后页后执行对应操作

关键调整说明

  1. 解决核心问题:把原来返回Object的ISecondPage.FillForm()改成返回ISecondPagePostActions,这个接口只包含两个合法方法,确保后续只能选择返回第一页或跳转到最后页。
  2. 优化链式流畅性:调整跳转方法的返回值,比如GoToSecondPage()直接返回ISecondPage,让调用链可以无缝衔接,不需要额外的转换。
  3. 增强可读性:给方法起了语义化的名字(比如GoBackToFirstPage()、ProceedToLastPage()),让测试脚本读起来更像自然语言,完全符合Fluent API提升可读性的目标。

内容的提问来源于stack exchange,提问作者Rea B.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:48:55