pass variables as array specflow c#

DAP

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();

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.

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();

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?

Thanks in advance.

Sam Holder

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);

then later

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

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).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

pass variables as array specflow c#

From Dev

Pass in object and array variables

From Dev

Pass multiple variables to function array

From Dev

Pass Array by Reference in C

From Dev

How to pass array of arrays as a series of variables?

From Dev

pass variables not in database row through json array

From Dev

Pass a C array to a Rust function

From Dev

Pass array by reference in C++

From Dev

C# Pass data to array

From Dev

Merge bit variables in array in C

From Dev

Pass string as a c_ubyte array to C

From Dev

Pass C array to a objective C method

From Dev

C++ dll pass array to C#

From Dev

Pass a c# array to a c++ method

From Dev

How to pass an array of variables from a laravel controller to my ajax function

From Dev

How to pass an array of variables from a laravel controller to my ajax function

From Java

How to pass this numpy array to C with Ctypes?

From Dev

Pass array by reference and modify values C++

From Dev

How to pass an array of char's to a function in C

From Dev

c++ pass array directy to function

From Dev

Pass Javascript Array of Objects to C# Codebehind

From Dev

how to pass multidimensional array in multithreading c++

From Dev

Pass javascript array to C# via json

From Dev

ctypes: Initialize array of arrays and pass to C function

From Dev

C++ - Pass by Reference, Weird Thing for Array

From Dev

pass array of specific size to a function in C

From Dev

How to pass in function a multidimensional char array in C?

From Dev

C++ pass an array of bitfields by reference

From Dev

cannot pass array to webservice in c#