本文介绍了在WebDriver方法中获取Specflow标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c#,硒和specflow运行自动化测试套件.如果可能的话,我希望能够看到为当前方案分配了哪些标签,因此我可以为每种方案实例化特定的浏览器类型.甚至可以使用XUnit吗?

I am running an automated test suite using c#, selenium, and specflow. If possible, I would like to be able to see what tag(s) are assigned to the current scenarios so I can instantiate a certain browser type per scenario. Is this even possible using XUnit??

登录功能文件:

Feature: Login
    In order to login to DRIVE
    As a user
    We have to enter login details

Background:
    Given I am on the login page

@headless
Scenario: Logging in as a valid user
    And I enter a valid user and password
    When I submit the login form
    Then The user should be logged in

WebDriverContext.cs文件

WebDriverContext.cs file

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.PhantomJS;

namespace Drive.Acceptance.Tests
{
    public interface IWebDriverContext {
        IWebDriver GetDriver();
    }

    public class WebDriverContext : IWebDriverContext
    {
        private static volatile WebDriverContext _instance;
        private static readonly object Lock = new object();

        public static IWebDriverContext Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (Lock)
                    {
                        if (_instance == null)
                            _instance = new WebDriverContext();
                    }
                }

                return _instance;
            }

        }

        public IWebDriver GetDriver()
        {
            lock (Lock)
            {
                // TODO: create headless browser if scenario is tagged with @headless
                if (!TagName.Contains("headless")) {
                    return new ChromeDriver();
                }
                else {
                    return new PhantomJSDriver();
                }
            }
        }
    }
}

推荐答案

您可以在ScenarioContext中获取该场景的标签列表.

You can get a list of tags of the Scenario in the ScenarioContext.

ScenarioContext.ScenarioInfo.Tags

请参见 https://github.com/techtalk/SpecFlow/blob/master/TechTalk.SpecFlow/ScenarioInfo.cs

您可以通过Context-Injection( http://specflow.org/documentation/上下文注入/)或通过ScenarioContext.Current( http://specflow.org/documentation/ScenarioContext)/).
如果可能的话,可以通过上下文注入获得它.这样,如果要并行运行测试,就不会有将来的问题.

You can get the actual ScenarioContext via Context-Injection (http://specflow.org/documentation/Context-Injection/) or via ScenarioContext.Current (http://specflow.org/documentation/ScenarioContext/).
If possible get it via Context- Injection. That way, you will not have future problems if you want to run the tests in parallel.

这篇关于在WebDriver方法中获取Specflow标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 11:11