简体   繁体   English

更改方法以断言返回的文本要么包含输入的文本,要么等于输入的文本(C#硒)

[英]Change Method to Assert that the Text Returned Either Contains Text entered or Equals Text Entered (C# Selenium)

I have the following code: 我有以下代码:

   bool ColorExistsByName(string colorNameV)
    {
        var colorName = colorPage.ColorList.CategoryList.FirstOrDefault(c => c.GetText().Contains(colorNameV));
        if (colorName == null) {
            return false;
        }
        return colorName.ExistsAndDisplayed;
    }

I need to edit this so that it can either Contain OR Equal colorNameV... what's the best way to do this? 我需要对其进行编辑,以便它可以包含或等于colorNameV ...最好的方法是什么?

EDIT 编辑

Sorry - I wasn't clear enough. 抱歉-我还不够清楚。 I need to somehow add a parameter (wholeWord) so that sometimes, it can use contains, and sometimes it can use equals (if we're using wholeWord) - is it possible to use parameter like this? 我需要以某种方式添加一个参数(wholeWord),以便有时可以使用contains,有时可以使用equals(如果使用的是WholeWord)-是否可以使用这样的参数? (We already have functionality written for wholeWord parameter in order to check if wholeWord is written) (我们已经为WholeWord参数编写了功能,以便检查是否已编写了WholeWord)

But your command c.GetText().Contains(colorNameV) does exactly what you want. 但是您的命令c.GetText().Contains(colorNameV)确实c.GetText().Contains(colorNameV)您的要求。 If you want to test "Contains", that's what this function does. 如果要测试“包含”,那就是该函数的作用。 If you want to test equals, it also works, since the c.GetText() will contain colorNameV and nothing else (will contain just itself). 如果要测试是否相等,它也可以工作,因为c.GetText()包含 colorNameVc.GetText() 包含其他任何内容(仅包含其自身)。 It works for both of your cases. 它适用于您的两种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM