c# - 使用 Browser.Current 在 C# 中使用 Baseclass.Contrib.Specflow 调用浏览器

标签 c# selenium specflow

我目前正在尝试使用 Selenium Grid 2 在多个浏览器上运行自动化测试。在我的研究过程中,我遇到了使用 Baseclass.Contrib.Specflow 的方法,它使我能够将浏览器用作功能文件中的标签,而无需在我的主驱动程序类中声明它。我遇到的问题是我阅读的其中一篇博客将以下内容作为设置代码

[SetUp]
public void Test_Setup(){
CurrentDriver = Browser.Current;}

我的应用配置文件看起来包含以下内容:

   <components>
  <!-- <component name="Firefox" type="OpenQA.Selenium.Firefox.FirefoxDriver, WebDriver" service="OpenQA.Selenium.IWebDriver, WebDriver" instance-scope="per-dependency">
  </component>-->
  <component name="Firefox" 
             type="Baseclass.Contrib.SpecFlow.Selenium.NUnit.RemoteWebDriver, Baseclass.Contrib.SpecFlow.Selenium.NUnit.SpecFlowPlugin" 
             service="OpenQA.Selenium.IWebDriver, WebDriver" 
            instance-scope="per-dependency">
    <parameters>
      <parameter name="url" value=" http://localhost/wd/hub" />
      <parameter name="browser" value="Firefox" />
    </parameters>
  </component>
  <component name="Safari" type="Baseclass.Contrib.SpecFlow.Selenium.NUnit.RemoteWebDriver, Baseclass.Contrib.SpecFlow.Selenium.NUnit.SpecFlowPlugin" service="OpenQA.Selenium.IWebDriver, WebDriver" instance-scope="per-dependency">
    <parameters>
      <parameter name="url" value=" http://localhost/wd/hub" />
      <parameter name="desiredCapabilities" value="Chrome" />
    </parameters>
  </component>

当我尝试使用上述设置方法运行脚本时出现错误。

错误:

System.Collections.Generic.KeyNotFoundException: 字典中不存在给定的键

我从中获得此解决方案的博客似乎没有回答与此相关的问题,所以我有点绝望。这基本上允许我在功能文件上进行以下操作,并根据标签运行测试

@Browser:Firefox
@Browser:Chrome

希望这些信息足以给我建议。

最佳答案

您在这里犯的错误是您使用标记 @Browser 注释整个功能文件。

Baseclass.Contrib.Specflow 允许您使用支持浏览器的场景来注释场景。因此,您必须对每个场景进行注释。

如果您不这样做,则没有为该测试设置当前浏览器,并且尝试访问 Browser.Current 将抛出 System.Collections.Generic.KeyNotFoundException .

当生成的单元测试将包含浏览器名称作为单元测试名称的一部分时,您知道自己做对了

<Test Name> on <Browser> with: <parameters>

例子:

@Browser:IE
@Browser:Chrome
@Browser:Firefox
Scenario Outline: Add Two Numbers
>Given I navigated to / using
And I have entered <summandOne> into summandOne calculator
And I have entered <summandTwo> into summandTwo calculator
When I press add
Then the result should be <result> on the screen
Scenarios:
| summandOne| summandTwo|result|
| 10 | 20 | 30 |
| 3 | 4 | 7 |

关于c# - 使用 Browser.Current 在 C# 中使用 Baseclass.Contrib.Specflow 调用浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31184992/

相关文章:

c# - TechTalk.SpecFlow.BindingException : 'Parameter count mismatch! 绑定(bind)方法

c# - 在 C# 中,override 修饰符在派生类中对于基类的虚拟/抽象方法是强制性的

c# - MVC RouteConfig "CatchAll"{*url} 不工作

c# - 如何以编程方式设置 WPF DataGrid 的模板和填充内容?

java - 如何跳过表格行

c# - 将通用类与 SpecFlow 绑定(bind)一起使用

c# - 反射 - 带参数调用构造函数

java - Firefox 无法使用 FirefoxDriver Selenium 打开

c# - C# 中的 Selenium Grid 与 Nunit 并行执行

c# - 带有两个标签的 Specflow 场景打开两个窗口