将变量作为数组specflow传递给C# [英] pass variables as array specflow c#

查看:107
本文介绍了将变量作为数组specflow传递给C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Specflow使用Selenium自动化Web测试。到目前为止,一切都很好,但是我现在遇到了问题。我的步骤之一允许用户输入变量,该步骤如下所示:

I am attempting to use Specflow to automate web tests using Selenium. So far, things are going mostly fine, but I am now running into a problem. One of my steps allow for a user to input a variable, the step looks like this:

     Given I click the (VARIABLE) Menu

其背后的代码非常简单,只需单击基于文本的链接即可传递:

And the code behind it is fairly simple, just clicking on a link based on the text that is passed:

     driver.FindElement(By.XPath("Xpath to get to the variable")).Click();

但是,后面的步骤必须使用此信息。很好,您可以使用 ScenarioContext.Current.Add(string,variable),我对此有所了解并一直在使用它。

However, there is a later step that must use this information. That is fine, you can use "ScenarioContext.Current.Add(string, variable)" and I know about that and have been using it. It functions for the needs that I was first informed of.

我的问题是,现在企业希望能够同时添加多个项目。这带来了两个问题。尝试再次调用该步骤会引发异常:已经添加了具有相同键的项。并且如果我将其放入方案纲要中,该方案纲要允许我在第二次运行中再次调用该变量,那么我将无法在最后一步中使用第一个变量。

My problem is that now the business wants to be able to add multiple items at the same time. This presents two problems. Attempting to just call the step a second time throws an exception: "An item with the same key has already been added." and if I put this into a Scenario Outline, which would allow me to call the variable a second time in a second run, I cannot use the first variable in the final step.

从逻辑上讲,这意味着多次传递变量是个问题(考虑到它以字符串形式传递,这是有意义的),因此将变量作为数组传递似乎是一种逻辑方法。这个想法是,当我将参数从一个步骤以数组而不是字符串的形式从一个步骤传递到另一步骤时,从理论上说不会遇到此错误,然后我就可以遍历其中的项目该数组在后面的步骤中带有for循环。这似乎是SpecFlow应该能够做的事情,但是我在寻找如何实现这一点方面遇到了问题。有谁知道如何做到这一点?我只是尝试使用:

Logically, this means that passing in a variable multiple times is the problem (which makes sense, given it's passing in as a string) and so passing the variable in as an array seems the logical way to go. The idea is that when I pass the parameter from one step to another as an array instead of as a string I theoretically won't run into this error and then I will be able to iterate through the items in the array in that later step with a for loop. This seems like something that SpecFlow should be able to do, but I am having issues finding out just how to achieve this. Does anyone have an idea on how to do this? I attempted to merely use:

     Scenario.Context.Current.Add(string, variable).ToArray();

但是,这不起作用,并且所有 ToArray示例都可以在SpecFlow文档似乎并没有真正将您从一个步骤传递到另一个步骤的变量更改为数组,它似乎仅在单个步骤内部使用,而从未在步骤之间传递。是否可以在SpecFlow中使用ScenarioContext.Current.Add(string,variable)作为数组传递参数?

However, that does not work, and all of the examples of "ToArray" I can find in the SpecFlow documentation doesn't seem to be actually changing the variables you pass from one step to another into an array, it seems to be used solely inside of individual steps and never passed between steps. Is passing parameters using ScenarioContext.Current.Add(string, variable) as an array possible in SpecFlow?

预先感谢。

推荐答案

解决问题的最简单方法是,首先在上下文中添加一个数组(或列表),然后将其取出并添加到其中,然后替换在以后的步骤中再次输入:

the simplest solution to your problem is to add an array (or list) to the context in the first step and then to get it out and add to it and then replace it again in future steps:

List<string> list = new List<String>();
list.Add(variable)
ScenarioContext.Current.Add(name, list);

然后稍后

List<String> currentList = (List<String>) ScenarioContext.Current[string];
currentList.Add(variable);
ScenarioContext.Current[name]=list;

但是我有义务指出当前解决方案中的一些问题。您应该调查 PageObject模式,并将元素选择XPath隐藏在页面对象内。想象一下,企业决定更改存储信息的元素。现在,您必须更改执行此操作的每个测试:

However I feel duty bound to point out some issues with your current solution. You should investigate the PageObject pattern and hide your element selection XPath inside your page objects. Imagine the business decides to change the element that information is stored in. Now you have to change every test that does this:

driver.FindElement(By.XPath("Xpath to get to the variable")).Click();

该变量。使用页面对象模式,它隐藏在页面对象内部,您将只能更改一个地方。

for that variable. Using the page object pattern this is hidden inside the page object and you would only have a single place to change.

我个人也考虑使用上下文注入,因为我发现这允许对数据进行强类型输入(因此不进行强制类型转换就像上面的示例一样),它可以让您知道存储了什么数据,而不仅仅是随机的东西。

I personally would also consider sharing data using context injection as I find this allows strong typing of the data (so no cast is required like in the example above) and it allows you to know what data is stored, its not just a random bag of stuff).

这篇关于将变量作为数组specflow传递给C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆