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

Related searches

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

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

Can someone help?

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'

Dunno what to do to fix this. Pls help. THX in advance.

This error is thrown when an extension method is call on a LINQ query for which there are no elements returned.

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

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

Also, not causing the error, but the id attribute is supposed to be unique. Instead of using

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

You should be using the class attribute:

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

And you should first be calling find elements and checking 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();
}

BDD Practice Archives - SpecFlow, BDD Practice � Introducing BDD to your team – how does it affect your role as a tester? by Bas Dijkstra ・ 6 August, 2020. BDD Practice � Why is BDD one of the� A SpecFlow feature is a file with a.feature file extension, describing the intended behaviour of a specific component or feature of the application you are going to write tests for.

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     | 

SpecFlow, SpecFlow is an open-source .NET utility which allows you to write tests using Cucumber-compatible Gherkin syntax. Prerequisites. You can install SpecFlow via� Your first C# test with SpecFlow Prerequisites. Create a new project in Visual Studio. In the Visual Studio Tools menu, go to Library Package Manager > Configuring capabilities. To let TestingBot know on which browser/platform you want to run your test on, you need to Testing Internal/Staged

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.

Getting Started with SpecFlow in 10 Minutes, If it doesn't feel free to reach out with any questions, and I will try to assist as much as I can. Install the SpecFlow Visual Studio Extension. This� TL;DR; How can I create a specflow test that calls another test as its first step? Given I already have one specflow test And I want to run another test that goes deeper than the first test Then I create a second test that runs the first test as its first step And I add additional steps to test the deeper functionality

I had the same problem with SpecFlow, but mine was a little different: Because I wanted to use the same "Examples" section for two "Scenario Outline"s, I thought I could put it at the end for both. But this didn't worked.

The problem really lies in the correct order of the elements (see also: Step Arguments in Cucumber under "Examples"): After a "Scenario Outline" an "Examples" section must follow, regardless if you use the same "Examples" section for several "Scenario Outline"s! Otherwise you get the described error.

You can't put all "Examples" sections or have one "Examples" section for several "Scenario Outline"s at the end of the feature file. ;-)

I hope, this helps others which run into the same problem.

SpecFlow C# Automated Selenium Testing Framework, Before you can start running your Selenium tests with SpecFlow, ensure you have the SpecFlow libraries installed: Ensure you have Microsoft Visual Studio 2015 Update 2 or above installed and running on your machine. Install SpecFlow using the NuGet Gallery. Run your first test. To understand how to integrate with BrowserStack, we will look at

I think it's because your list did not return any items/elements in here

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

But in your code here Assert.AreEqual(todoText, elem.Text); You access a null object, which causes this error.

You need to check if elem is not null:

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

BDD UI Automation via SpecFlow, C#, and Selenium, Before we can use SpecFlow.Assist, we need to define a C# object representation of the API response. This allows us to deserialize the JSON document received from the API into an actual C# object, which is much easier to work with and perform operations on than JSON (or XML) documents.

SpecFlow is a testing framework that supports Behaviour Driven Development (BDD). It lets us define application behavior in plain meaningful English text using a simple grammar defined by a language called Gherkin. In this series of SpecFlow Tutorial, we will be covering

This can be used to inject the same context into several step-definitions. Browse other questions tagged c# specflow or ask your own question.

With SpecFlow 3.0, we marked ScenarioContext.Current and FeatureContext.Current as obsolete, to make clear that you that you should avoid using these properties in future. The reason for moving away from these properties is that they do not work when running scenarios in parallel.

Comments
  • This answer is wrong because the question illustrates a compile-time error.
  • This answer is wrong because the question illustrates a compile-time error.
  • @TomW "You need to check if elem is not null:"