C# 为列表指定参数

C# 为列表指定参数,c#,linq,C#,Linq,我总结了以下方法: public static bool compareTableRow(List<string> expected, int rowNumberOfElemets, IWebDriver driver) { List<string> actual = new List<string> { }; for (int i = 1; i < rowNumberOfElemets + 1

我总结了以下方法:

public static bool compareTableRow(List<string> expected,  int rowNumberOfElemets, IWebDriver driver)
    {
        List<string> actual = new List<string>
        { };

        for (int i = 1; i < rowNumberOfElemets + 1; i++)
        {
            actual.Add(driver.FindElementHighlight
              (By.XPath("//*[@id=\"nefi-content\"]/div[2]/section/div/table/tbody/tr[1]/td[" + i + "]/div/input")).GetAttribute("value"));
        }

        if (expected.SequenceEqual(actual)) return true;
        else
            return false;
    }

目前,“预期”列表是硬编码的。我应该输入什么样的方法变量才能调用该方法并传递我试图比较的BLA1123、bla2等字符串

即使使用您的实现,您也不需要硬编码预期参数,这样调用您的方法很容易:

compareTableRow(new List<string> {"bla1", "123", "bla2", "etc", "etc"}, 42, driver);
并像你描述的那样称呼它:

compareTableRow(42, driver, "bla1", "123", "bla2", "etc", "etc");
所以你有一个IWebDriver,它有很多行的概念。您还有一个输入rowNumberOfElements和一个预期字符串列表

您需要检查从IWebDriver中可以找到的第一行NumberOfElements项YelementHighlight是否与所需字符串列表完全相同

我在这里看到几个问题:

首先:期望字符串列表中的顺序重要吗?那么,第一个预期字符串是否应该等于第一个FindByElementHighLight?根据你的代码,顺序很重要

此外:预期字符串的列表是否足够长?它能比RowNumberRelations长吗?或者它通常足够长吗?您能用预期列表中的元素数替换RowNumberRelations吗

如果第一个元素不匹配,您知道检查其他元素是无用的,因为您已经知道返回值将为false

通用解决方案

我将使用它作为IWebDriver的扩展方法来编写它。这就好像你问IWebDriver:你的前X个元素是否等于这个序列的前X个元素?看

首先,我们将展示其用法:

List<string> expectedHighLights = ...
IWebDriver driver = ...
var isExpected = driver.HasExpectedHighLights(expectedHighLights);

但是,在某些LINQ语句的中间也将工作:

IEnumerable<image> images = ...
var imagesWithExpectedHighLights = images
    .Where(image => image.Driver.HasExpectedHighLights(expectedHighLights))
    .Slect(image => ...);
现在是代码。最重要的是我们应该尽快停止

static string FindElementHighLight(this IWebDriver driver, int i)
{
     return driver.FindElementHighlight(By.XPath("//*[@id=\" ... etc);
}

static bool HasExpectedHighLights(this IWebDriver driver,
   IEnumerable<string> expected, int nrOfElements)
{
    // TODO: exception if driver == null? expected == null? nrOfElements < 0?

    IEnumerable<string> actualHighLights = driver
        .Select( (driver,  i) => driver.FindElementHighLight(i)
        .Take(nrOfElements);
    // note: this has only created the enumerable.
    // The sequence has not been enumerated yet

    return actualHighLights.SequenceEqual(expected);
    // sequence equal will stop as soon as a mismatch has been found      
}
因此,如果在i==3处发现不匹配,则不会计算HighLight4

很好的一点是,在不更改IWebDriver的情况下,您为它添加了功能:FindElementHighLightint


这可用于在SequenceEqual中创建IEnumerable,这样即使检测到第一个高光不是预期的,也不必获取所有高光。

List?请注意,第一个参数当前没有名称。使其成为预期并删除方法内部的预期声明。Opps,对@Sweeper.进行了更改。我应该如何更改方法内部?@entrup-List expected不是硬编码的。您正在将其作为参数传递。您可以在运行时将其设置为任意值。
IEnumerable<image> images = ...
var imagesWithExpectedHighLights = images
    .Where(image => image.Driver.HasExpectedHighLights(expectedHighLights))
    .Slect(image => ...);
static string FindElementHighLight(this IWebDriver driver, int i)
{
     return driver.FindElementHighlight(By.XPath("//*[@id=\" ... etc);
}

static bool HasExpectedHighLights(this IWebDriver driver,
   IEnumerable<string> expected, int nrOfElements)
{
    // TODO: exception if driver == null? expected == null? nrOfElements < 0?

    IEnumerable<string> actualHighLights = driver
        .Select( (driver,  i) => driver.FindElementHighLight(i)
        .Take(nrOfElements);
    // note: this has only created the enumerable.
    // The sequence has not been enumerated yet

    return actualHighLights.SequenceEqual(expected);
    // sequence equal will stop as soon as a mismatch has been found      
}