SpecFlowOSS / SpecFlow

#1 .NET BDD Framework. SpecFlow automates your testing & works with your existing code. Find Bugs before they happen. Behavior Driven Development helps developers, testers, and business representatives to get a better understanding of their collaboration
https://www.specflow.org/
Other
2.23k stars 752 forks source link

Cannot add Scenario Outline Parameter to a multi-line text if it is XML #2571

Closed asimontsev closed 2 years ago

asimontsev commented 2 years ago

SpecFlow Version

3.9.58

Which test runner are you using?

xUnit

Test Runner Version Number

2.4.1

.NET Implementation

.NET Core 3.1

Project Format of the SpecFlow project

Sdk-style project format

.feature.cs files are generated using

SpecFlow.Tools.MsBuild.Generation NuGet package

Test Execution Method

Visual Studio Test Explorer

SpecFlow Section in app.config or content of specflow.json

{ "bindingCulture": { "name": "en-us" }, "language": { "feature": "en-us" } }

Issue Description

I am writing a test which looks like this:

    Given the the package contains the web.config file of the following content
         """
         <?xml version="1.0" encoding="utf-8"?>
         <configuration>
            <system.Web>
                <customErrors mode="<Mode>" />
            </system.Web>
         </configuration>
         """
    When the user gets the debug status
    Then the debug status should be <Expected Debug Status>

    Examples: 
    | Mode          | Expected Debug Status |
    | Off           | true                  |
    | RemoteOnly    | false                 |

However, the <Mode> parameter is not replaced and my step receives the XML content unchanged. According to the docs, multi-line text should support such variables. If I put this variable outside of an XML tag, it is replaced, for example, this one works:

         """
         <?xml version="1.0" encoding="utf-8"?>
                 <Mode>
         <configuration>
            <system.Web>
                <customErrors mode="" />
            </system.Web>
         </configuration>
         """

I have noticed the suggestion to add the single quotes around the variable, but it did not help.

Eventually, I have found out that I can use the ScenarioInfo - I am receiving an instance of ScenarioInfo through Dependency Injection and add the following code to my step definition:

            // fileBody is a step argument receiving the multiline text
            foreach (var arg in scenarioInfo.Arguments.Keys)
            {
                fileBody = fileBody.Replace($"<{arg}>", scenarioInfo.Arguments[arg].ToString());
            }

However, I believe this sort of code should be executed by SpecFlow, rather than the step definitions.

Hope this makes sense.

Steps to Reproduce

  1. Create a Scenario Outline which has a multi-line Doc String with XML.
  2. Add a parameter in angle brackets to this XML inside a tag (e.g. as an attribute value).

Expected: the parameter is replaced by the value from the Examples table

Actual: the Doc String is passed to the step definition unchanged.

Simplified example:

Scenario Outline: 
  Given my XML is
     """
     <node attrib="<Attribute>"></node>
     """ 
  When I process XML
  Then I receive <Result> value

  Examples:
  | Attribute  |  Result  |
  | Foo        |  Bar     |

Link to Repro Project

No response

SabotageAndi commented 2 years ago

Yeah, that looks like a bug. I think it is happening somewhere here: https://github.com/SpecFlowOSS/SpecFlow/blob/master/TechTalk.SpecFlow.Generator/Generation/UnitTestMethodGenerator.cs#L347

If somebody wants to fix this, a good starting point would be to write some unit tests for the method, before fixing it.

clrudolphi commented 2 years ago

I have a first draft of a working prototype for the fix. Putting together unit tests and will submit a PR. The problem stems from the Regex pattern used in the GetSubstitutedString method of the ScenarioPartHelper class. https://github.com/SpecFlowOSS/SpecFlow/blob/ba1aae24ef2389effe5eecdad300e3455886667a/TechTalk.SpecFlow.Generator/Generation/ScenarioPartHelper.cs#L162

SabotageAndi commented 2 years ago

Awesome @clrudolphi

clrudolphi commented 2 years ago

PR submitted. First timer here; let me know what else you'd like to see in the PR.

SabotageAndi commented 2 years ago

Fixed with https://github.com/SpecFlowOSS/SpecFlow/pull/2578

github-actions[bot] commented 2 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.