如何比较 SpecFlow 中的多行参数?

How can I compare the multiline parameter in SpecFlow?

我有一个包含步骤

的功能文件
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

和步骤定义

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

每次我 运行 调试这一步时,我都会看到输出

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

我知道我可以 trim \r\n 并在功能文件中调整我的输入参数。但这将是一种黑客攻击,我不想这样做。是否有机会断言多行文本?

您可以使用 Doc Strings 来指定您的字符串参数:

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

并使用以下步骤定义:

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