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

I am trying to do my first autotest and I am getting this error:

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

...

Can anyone please help?

My 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 this, I get the error:

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

      

Not sure what to do to fix this. Please, help. thanks in advance.

+4


source to share


5 answers


This error occurs when an extension method calls a LINQ query for which there are no returned items.

So calling .ToList () in the following code will throw an error if there are no ul elements with id mytodos

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

      

Also, without throwing an error, but the id attribute must be unique. Instead of using

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

      



You must use the class attribute:

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

      

And you have to call the elements of find first and check that it is not null and contains elements.

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

      

0


source


I know the answer is already accepted, but I found another solution.

I think the problem was that you listed two script sketches in sequence and then put examples below them. When using a script schema, the system expects a block of samples before specifying another script. That way, if you just move the first block of examples between the two paths of the script, the error should no longer occur. This is how your 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     | 

      

+8


source


I had the same problem with SpecFlow, but I had a slightly different one: Since I wanted to use the same Examples section for two Scripts, I thought I could put it at the end for both. But it didn't work.

The problem is the correct order of the elements (see also: Steps in Cucumbers in the Examples section ): After the Script Script, the Examples section should follow, regardless of whether you use the same Examples section for several Scripting scripts "! Otherwise, you will receive the described error.

You cannot put all Examples sections or have one Examples section for multiple Scripts at the end of a function file .; -)

Hope this helps others who are facing the same problem.

0


source


You can also get this error when you have Scenario Outline

but no section with Example

.

In this case, you need to change it from Scenario Outline

to simple Scenario

.

0


source


I think because your list was not returning any items / elements here

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

      

But in your code here Assert.AreEqual(todoText, elem.Text);

you are referring to a null object that is causing this error.

You need to check if elem is not invalid:

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

      

-1


source







All Articles