我正在尝试制作我的第一个自动测试,并且我一直收到此错误:
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'
不知道该怎么做才能解决这个问题。请帮忙。 THX提前。
在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>
首先应该调用find元素并检查它是否为null并包含元素。
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):在“场景大纲”之后,必须遵循“示例”部分,无论您是否对几个“场景”使用相同的“示例”部分大纲“S!否则你会得到描述的错误。
您不能在功能文件末尾的几个“场景大纲”中放置所有“示例”部分或具有一个“示例”部分。 ;-)
我希望,这可以帮助遇到同样问题的其他人。
当你有Scenario Outline
但没有Example
部分时,你也可以得到这个错误。
在这种情况下,您需要将它从Scenario Outline
更改为Scenario
。
我认为这是因为你的列表没有在这里返回任何项目/元素
WebElement elem = list.Find(x => x.Text.Equals(todoText));
但是在这里你的代码中Assert.AreEqual(todoText, elem.Text);
你访问一个null对象,这会导致这个错误。
您需要检查元素是否不为null:
if(elem != null)
{
Assert.AreEqual(todoText, elem.Text);
}