2017-07-01 73 views
2

我試圖做我的第一次自動測試,並不斷收到此錯誤:錯誤CS1029 #error:'Generation error:Sequence contains沒有元素「。C#specflow無法修復:錯誤CS1029 t #error:'生成錯誤:序列不包含任何元素'

有人可以幫忙嗎?

我specflow feture:

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  | 
    | [email protected]#$%^&*()_-+<>| 

    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提前。

回答

1

時的擴展方法是LINQ查詢是針對那些還沒有返回的元素調用這個錯誤被拋出。

所以在下面的代碼調用.ToList()將導致錯誤,如果有id爲mytodos

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

而且,不會造成錯誤沒有UI元素,但id屬性應該是獨特。除了使用

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

的您應該使用class屬性:

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

你應該先打電話找到的元素,並檢查它是不是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(); 
} 
0

我想這是因爲你的名單並沒有在這裏

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

但返回代碼中的任何物品/元素在這裏Assert.AreEqual(todoText, elem.Text); 你訪問一個空的對象,這將導致此錯誤。

您需要檢查是否ELEM不爲空:

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

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

我認爲問題在於您按順序指定了兩個場景輪廓,然後您將它們放在它們下面。當您使用場景大綱時,系統在指定其他場景之前需要一個示例塊。因此,如果您只是在兩個場景大綱之間移動第一個示例塊,則錯誤不應再發生。這裏是你如何擁有文件應該是這樣:

特點:

SpecFlowFeature1

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

@mytag

場景:待辦事項

檢查默認數量

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  | 
| [email protected]#$%^&*()_-+<>| 

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  | 
+1

我有這個錯誤,這是問題 – user515867

0

我與SpecFlow有同樣的問題,但是我有點不同: 因爲我想對兩個「場景大綱」使用相同的「示例」部分,所以我認爲我可以將它放在兩端。但是這沒有奏效。

真正的問題在於元素 的正確順序(參見:Step Arguments in Cucumber下「例子」): 一‘方案綱要’的‘示例’部分必須遵循後,如果不管你使用相同的幾個「情景大綱」的「示例」部分!否則,你會得到所描述的錯誤。

您不能放置所有「示例」部分,或者在功能文件的末尾放置幾個「場景大綱」的「示例」部分。 ;-)

我希望,這有助於其他人遇到同樣的問題。

相關問題