Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# specflow can't fix : Error CS1029 #error: 'Generation error: Sequence contains no elements'

I'm trying to make my first autotest and I keep getting this error:

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

My specflow feature:

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     | 

My config:

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

My steps:

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

After all of this I get an error:

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

What can I do to fix this?

like image 287
Max Skliar Avatar asked Dec 19 '22 06:12

Max Skliar


2 Answers

I know the answer has already been accepted, but I found a different solution.

I think the problem was that you specified two scenario outlines in sequence, and then you put the examples below them. When you use the Scenario Outline, the system expects an examples block before you specify another scenario. So, if you just move the first examples block in between the two scenario outlines, the error shouldn't happen anymore. Here's how you feature file should look like:

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     | 
like image 134
rod0302 Avatar answered Jan 01 '23 11:01

rod0302


You can also get this error when you have a Scenario Outline but don't have an Example section.

In this case you need to change it from a Scenario Outline to just Scenario.

like image 33
codersl Avatar answered Jan 01 '23 10:01

codersl