Can't fix: Error CS1029 'Generation error: Sequence contains no elements'

Can't fix: Error CS1029 'Generation error: Sequence contains no elements'

我正在尝试进行第一次自动测试,但我一直收到此错误:

Error CS1029 #error: 'Generation error: Sequence contains no elements'.

我的 specflow 功能:

Feature: SpecFlowFeature1
    In order to see and check my todos
    As planning user
    I want to create and see my todos and done todos

@mytag
Scenario: Check default number of todos
    Given user is on todolist.me main page
    Then user sees list of 7 todo''s

Scenario Outline: Check todos creation
    Given user is on todolist.me main page
    When user creates new todo with content: <content>
    Then user sees todo with content: <content>
    
Scenario Outline: Chech todos can be checked and mark as done
    Given user is on todolist.me main page
    When user creates new todo with text: <text>
    Then user checks the todo with text: <text>
    Then user sees no todo with text: <text>
    Then user sees done todo with text: <text>

    Examples: 
    | content         |
    | just plain text |
    | 1234567890      |
    | ~!@#$%^&*()_-+<>|

    Examples: 
    | text               |
    | customToDoText     | 

我的配置:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechTalk.SpecFlow;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace UnitTestProject1
{
    [Binding]
    public class Conf
    {
        private static IWebDriver driver = new ChromeDriver();

        public static IWebDriver GetDriver()
        {
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            driver.Manage().Window.Maximize();
            return driver;
        }

        [AfterTestRun]
        public static void AfterTestRun()
        {
            driver.Quit();
        }
    }
}

我的步数:

using System;
using TechTalk.SpecFlow;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using NUnit.Framework;
using OpenQA.Selenium.Interactions;
using System.Collections.Generic;
using System.Linq;

namespace UnitTestProject1
{
    [Binding]
    public class SpecFlowFeature1Steps
    {
        private static IWebDriver driver = Conf.GetDriver();

        [Given(@"user is on todolist.me main page")]
        public void NavigateToTodoList()
        {
            driver.Navigate().GoToUrl("http://todolistme.net");
        }

        [When(@"user creates new todo with content: (.*)")]
        public void WhenUserCreatesNewTodoWithContent(String todoContent)
        {
            driver.FindElement(By.Id("newtodo")).SendKeys(todoContent);
            new Actions(driver).SendKeys(Keys.Enter).Build().Perform();
        }

        [When(@"user creates new todo with text: (.*)")]
        public void WhenUserCreatesNewTodoWithText(String todoText)
        {
            driver.FindElement(By.Id("newtodo")).SendKeys(todoText);
            new Actions(driver).SendKeys(Keys.Enter).Build().Perform();
        }


        [Then(@"user sees list of (.*) todo's")]
        public void ThenUserSeesListOfTodoS(int count)
        {
            Assert.AreEqual(count, driver.FindElements(By.XPath("//span[contains(@id, 'mytodo')]")).Count);
        }


        [Then(@"user sees todo with content: (.*)")]
        public void ThenUserSeesTodoWithContent(String todoContent)
        {
            List<IWebElement> list = driver.FindElements(By.XPath("//span[contains(@id, 'mytodo')]")).ToList();
            IWebElement elem = list.Find(x => x.Text.Equals(todoContent));
            Assert.AreEqual(todoContent, elem.Text);
        }

        [Then(@"user checks the todo with text: (.*)")]
        public void ThenUserChecksTheTodoWithText(String todoText)
        {
            var listItem = driver.FindElement(By.XPath("//li[./span[contains(text(), 'customToDo')]]/input"));
            new Actions(driver).Click(listItem);
        }

        [Then(@"user sees no todo with text: (.*)")]
        public void ThenUserSeesNoTodoWithText(String todoText)
        {
            List<IWebElement> list = driver.FindElements(By.XPath("//ul[contains(@id, 'mytodos')]//span[contains(@id, 'mytodo')]")).ToList();
            IWebElement elem = list.Find(x => x.Text.Equals(todoText));
            Assert.AreNotEqual(todoText, elem.Text);
        }

        [Then(@"user sees done todo with text: (.*)")]
        public void ThenUserSeesDoneTodoWithText(String todoText)
        {
            List<IWebElement> list = driver.FindElements(By.XPath("//ul[contains(@id, 'mydonetodos')]//span[contains(@id, 'mytodo')]")).ToList();
            IWebElement elem = list.Find(x => x.Text.Equals(todoText));
            Assert.AreEqual(todoText, elem.Text);
        }
    }
}

所有这些之后我得到一个错误:

Error   CS1029  #error: 'Generation error: Sequence contains no elements'

我该怎么做才能解决这个问题?

我认为这是因为您的列表 return 没有任何 items/elements 在这里

WebElement elem = list.Find(x => x.Text.Equals(todoText));

但是在你的代码中Assert.AreEqual(todoText, elem.Text); 您访问了一个空对象,这导致了这个错误。

您需要检查 elem 是否不为空:

if(elem != null) 
{
   Assert.AreEqual(todoText, elem.Text);
}

在没有返回任何元素的 LINQ 查询上调用扩展方法时会引发此错误。

所以如果不存在id为mytodos的ul元素,在下面的代码中调用.ToList()就会报错

List<IWebElement> list = driver.FindElements(By.XPath("//ul[contains(@id, 'mytodos')]//span[contains(@id, 'mytodo')]")).ToList()

此外,不会导致错误,但 id 属性应该是唯一的。而不是使用

<ul id='mytodo'></ul>

您应该使用 class 属性:

<ul class='mytodo'></ul>

您应该首先调用查找元素并检查它是否不为空且包含元素。

List<IWebElement> list = null;
var elements = driver.FindElements(By.XPath("//ul[contains(@class, 'mytodos')]//span[contains(@id, 'mytodo')]"));
if (elements!=null && elmenents.Count>0){
    list = elements.ToList();
}

我知道答案已被接受,但我找到了不同的解决方案。

我认为问题在于您按顺序指定了两个场景大纲,然后将示例放在它们下面。当您使用场景大纲时,系统需要在您指定另一个场景之前有一个示例块。因此,如果您只是将第一个示例块移动到两个场景大纲之间,错误就不会再发生了。您的功能文件应如下所示:

Feature: SpecFlowFeature1
    In order to see and check my todos
    As planning user
    I want to create and see my todos and done todos

@mytag
Scenario: Check default number of todos
    Given user is on todolist.me main page
    Then user sees list of 7 todo''s

Scenario Outline: Check todos creation
    Given user is on todolist.me main page
    When user creates new todo with content: <content>
    Then user sees todo with content: <content>

    Examples: 
    | content         |
    | just plain text |
    | 1234567890      |
    | ~!@#$%^&*()_-+<>|

Scenario Outline: Chech todos can be checked and mark as done
    Given user is on todolist.me main page
    When user creates new todo with text: <text>
    Then user checks the todo with text: <text>
    Then user sees no todo with text: <text>
    Then user sees done todo with text: <text>

    Examples: 
    | text               |
    | customToDoText     | 

我在使用 SpecFlow 时遇到了同样的问题,但我的情况略有不同: 因为我想对两个“场景大纲”使用相同的“示例”部分,所以我想我可以把它放在两个的最后。但这没有用。

问题真的出在元素的正确顺序上 (另请参阅:“示例”下的 Step Arguments in Cucumber): 在“场景大纲”之后必须跟随“示例”部分,无论您是否对多个“场景大纲”使用相同的“示例”部分!否则你会得到描述的错误。

您不能将所有“示例”部分或多个“场景大纲”的一个“示例”部分放在功能文件的末尾。 ;-)

我希望,这可以帮助 运行 遇到同样问题的其他人。

当您有 Scenario Outline 但没有 Example 部分时,您也可能会遇到此错误。

在这种情况下,您需要将其从 Scenario Outline 更改为 Scenario