首页 > 解决方案 > 如何在不重启浏览器实例的情况下迭代场景中的小黄瓜步骤

问题描述

我有案例检查表中的过滤,但没有必要重新启动浏览器每个字段值检查。如何在不丢失每个字段的测试结果的情况下循环执行此操作?

我有类似的东西:

Background: 
    Given I started app and logged in
    And I go to page with table

Scenario Outline: Filter tests
    When I clear filters
    And I filter by field value equals <FieldValue>
    Then I check if all rows has <FieldValue> in table
    
    Examples: 
    | FieldValue |
    | val1       |
    | val2       |
    | val3       |
    | val4       |

我想在不重新启动应用程序的情况下重复 X 次“场景大纲”。

驱动初始化:

[BeforeScenario]
        public void Initialize()
        {
            Driver = _factory.Value.GetBrowser(GlobalSettings.browserType);
            var login = new Login(Driver);
            try
            {
                login.TryToLoginAgainIfUserIsNotLoggedIn(10);
            }
            catch (WebDriverTimeoutException)
            {
                Driver.WebdriverCustom.Refresh();
                login.LogIn();
            }
            _objectContainer.RegisterInstanceAs(Driver);
        }

[AfterScenario]
        public void CleanUp()
        {
            try
            {
                if (TestContext.CurrentContext.Result.Outcome.Status != ResultState.Success.Status)
                {
                    string filePath = Driver.Utils.TakeScreenshot();
                    TestContext.AddTestAttachment(filePath);
                    CloseAndQuit();
                }
                else
                {
                    CloseAndQuit();
                }
            }
            catch (Exception)
            {
                string filePath = Driver.Utils.TakeScreenshot();
                TestContext.AddTestAttachment(filePath);
                CloseAndQuit();
            }
        }

标签: c#seleniumbddspecflowgherkin

解决方案


使用适当且可重复的内容标记您的场景以跟踪它不会在每次迭代时使用新的浏览器。

@SingleBrowserInstance
Scenario Outline: Filter tests
    When I clear filters
    And I filter by field value equals <FieldValue>
    Then I check if all rows has <FieldValue> in table
    
    Examples: 
    | FieldValue |
    | val1       |
 ...etc

对于您的通用 catch-all [before],向该类添加一个构造函数以提取场景上下文对象,然后过滤掉这个新标签。这可以节省您向所有其他测试添加标签以弥补这种新方法的麻烦。

[Binding]
    public class WhateverYourHooksClassIsCalled
    {
        ScenarioContext _scenarioContext;
        public WhateverYourHooksClassIsCalled(ScenarioContext scenarioContext)
        {
            _scenarioContext = scenarioContext;
        }
   
        // This is your existing before scneario
        [BeforeScenario]
        public void Initialize()
        {
            if(_scenarioContext.ScenarioInfo.Tags.Contains("SingleBrowserInstance"))
                return; // we don't want it to run...

            // etc...  rest of your init.
        }
}

为这个标签创建另外几个钩子:

//note this is before/after FEATURE
[BeforeFeature("SingleBrowserInstance")]
public void CleanUpSingleBrowser()
    {
        //initialise your browser. 
        // As you filter it out in the before scenario you'll need to 
        // refactor the original init into a reusable component (ask if you want help)
    }

[AfterFeature("SingleBrowserInstance")]
public void CleanUpSingleBrowser()
    {
        CleanUp();
    }
]

您需要的大部分信息都在这里

最后的评论是期望表的每一行都会影响其他行。单个浏览器实例意味着应用程序状态现在将在测试之间保持不变。这些测试不再是孤立的。更改数据的顺序、添加和删除数据表行可以改变整体结果。当然,这不太可能,但需要注意。


推荐阅读