• BDD - SpecFlow Driver Pattern 驱动模式


    引言

    前面 《 BDD - SpecFlow Page Object Model POM 》介绍了 POM 模式,用于提取 Web UI 元素封装成 Page Object 类,今天介绍另外一种 Driver Pattern 驱动模式,这两种模式都是为了提高 Automation 的维护性,可读性,有利于Automation 框架设计。

    Driver Pattern 的优势

    Driver 模式只不过是 Step Definitions 和 Automation Code 之间附加层。

    多年实践经验表明,组织好 Bindings 类和 Automation Code,保持 Bindings 类中的 Code 非常短(一个 Step definition 不超过 10 行 Code),这样可读性强。

    Driver Pattern 具有下列优势:

    • 易维护 Autoamtion Code
      将 Code 分成多个部分,使得 Code 方便维护

    • 不同的 Step Definitions 方便重用方法,或则合并多个 Steps 成单个的 Step
      我们经常看到一组 Steps 适用于多个 Scenarios,将 Automation Code 开成不同的类,更方便调用这些方法。

    • Step Definitions 易读
      使得没有 Coding 技术的人也有理解一个 Step Definiton 的实现作用。特别是大的项目,没有人想了解每单个 Step 的实现。

    当然 Driver Patter 利用 Context Injection 将多个类联系在一起,详情请参考 《 BDD - SpecFlow Context Injection 上下文依赖注入

    举例

    这个例子,对比运用 Driver Patern 前后的变化。

    不用 Driver Pattern

    这是一些 Automation Code 利用 Page Object Model 来检查一些 WebElements 是否存在。Step Definition 中大量的 code 行,不够简短,可读性不强。

    在这里插入图片描述

    [Then(@"it is possible to enter a '(.*)' with label '(.*)'")]
    public void ThenItIsPossibleToEnterAWithLabel(string inputType, string expectedLabel)
    {
        var submissionPageObject = new SubmissionPageObject(webDriverDriver);
    
        switch (inputType.ToUpper())
        {
            case "URL":
                submissionPageObject.UrlWebElement.Should().NotBeNull();
                submissionPageObject.UrlLabel.Should().Be(expectedLabel);
                break;
            case "TYPE":
                submissionPageObject.TypeWebElement.Should().NotBeNull();
                submissionPageObject.TypeLabel.Should().Be(expectedLabel);
                break;
            default:
                throw new NotImplementedException(inputType + " not implemented");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    运用 Driver Pattern

    我们将上面 Step Definition 中大量的 Code 移到一个 Driver 类SubmissionPageDriver 中,这样就能减少 Step Definition 中的 Code 行数到一行。我们将命名一个 CheckExistenceOfInputElement 方法,团队中每个成员都能从名字上理解这个方法的作用。

    为了在 Step Definition 中调用 Driver 类中的封装的方法 SubmissionSteps,我们利用 SpecFlow 自带支持的 Context Inject 依赖注入,在 public 构造函数中定义 Driver 类的实例做为参数。

    在这里插入图片描述

    [Binding]
    public class SubmissionSteps
    {
        private readonly SubmissionPageDriver submissionPageDriver;
    
        public SubmissionSteps(SubmissionPageDriver submissionPageDriver)
        {
            this.submissionPageDriver = submissionPageDriver;
        }
    
        [Then(@"it is possible to enter a '(.*)' with label '(.*)'")]
        public void ThenItIsPossibleToEnterAWithLabel(string inputType, string expectedLabel)
        {
            submissionPageDriver.CheckExistenceOfInputElement(inputType, expectedLabel);
        }
    
        // ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    public class SubmissionPageDriver
    {
        // ...
    
        public void CheckExistenceOfInputElement(string inputType, string expectedLabel)
        {
            var submissionPageObject = new SubmissionPageObject(webDriverDriver);
    
            switch (inputType.ToUpper())
            {
                case "URL":
                    submissionPageObject.UrlWebElement.Should().NotBeNull();
                    submissionPageObject.UrlLabel.Should().Be(expectedLabel);
                    break;
                case "TYPE":
                    submissionPageObject.TypeWebElement.Should().NotBeNull();
                    submissionPageObject.TypeLabel.Should().Be(expectedLabel);
                    break;
                default:
                    throw new NotImplementedException(inputType + " not implemented");
            }
        }
    
        // ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    qt学习之旅--QToolBar的使用(一)
    JAVA内存模型与JVM内存结构
    铁威马NAS如何开启二次验证提高系统安全性
    2018.7-2019.7一周年Java进阶架构师技术文章整理 建议收藏
    神器 SpringDoc 横空出世!最适合 SpringBoot 的API文档工具来了
    LeetCode 26. 删除有序数组中的重复项 简单
    自动化工具:PyAutoGUI的鼠标与键盘控制,解放双手的利器
    十二张图:从0开始理解对称/非对称加密、CA认证、以及K8S各组件颁发证书原由
    QT-day1
    CompletableFuture 异常与事务【无标题】
  • 原文地址:https://blog.csdn.net/wumingxiaoyao/article/details/127899690