Search code examples
c#specflow

How can I compare the multiline parameter in SpecFlow?


I have a feature file with step

Then I must see the specific text 'Lorem ipsum.\r\nLorem ipsum, lorem ipsum - lorem ipsum\r\nLorem ipsum.' in the text .error-message block on the page

And step definition

    [Then(@"I must see the specific text '(.*)' in the text (.*) block on the page")]
    public void ThenISeeTheSpecificTextOnThePage(string expectedText, string textBlockName)
    {
        var actualText = driver.FindElement(By.CssSelector(textBlockName)).Text;
        ///var removeCaret = actualText.Replace(Environment.NewLine, string.Empty);
        Assert.Contains(expectedText, actualText);
    }

Every time I run this step in debug I see the output

> Assert.Contains() Failure
Not found: Lorem ipsum. Lorem ipsum, Lorem ipsum
In value:  Lorem ipsum.
Lorem ipsum, Lorem ipsum
Lorem ipsum

I know that I can trim \r\n and adapt my input parameter in the feature file. But this would be a hack, which I don't want to do. Is there any chance to assert multiline text?


Solution

  • You can use Doc Strings for specifying your string parameter:

    Then I must see the specific text in the .error-message block on the page:
        """
        Lorem ipsum.
        Lorem ipsum, lorem ipsum - lorem ipsum
        Lorem ipsum.
        """
    

    And using the following step definition:

    [Then(@"I must see the specific text in the (.*) block on the page:")]
    public ThenIMustSeeTheSpecificTextInTheBlockOnThePage(string textBlockName, string expectedText)
    {
        var actualText = driver.FindElement(By.CssSelector(textBlockName)).Text;
    
        Assert.Contains(expectedText, actualText);
    }