Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#specflow can';t fix:错误CS1029 #错误:';生成错误:序列不包含元素';_C#_Linq_Xpath_Specflow_Qa - Fatal编程技术网

C#specflow can';t fix:错误CS1029 #错误:';生成错误:序列不包含元素';

C#specflow can';t fix:错误CS1029 #错误:';生成错误:序列不包含元素';,c#,linq,xpath,specflow,qa,C#,Linq,Xpath,Specflow,Qa,我正在尝试进行第一次自动测试,但不断出现以下错误: 错误CS1029#错误:“生成错误:序列不包含元素” 有人能帮忙吗 我的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 numbe

我正在尝试进行第一次自动测试,但不断出现以下错误:

错误CS1029#错误:“生成错误:序列不包含元素”

有人能帮忙吗

我的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 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);
        }
    }
}
不知道该怎么做才能解决这个问题。 请帮忙。
提前谢谢。

我想这是因为您的列表中没有返回任何项目/元素

WebElement elem = list.Find(x => x.Text.Equals(todoText));
但是在这里的代码中,
Assert.AreEqual(todoText,elem.Text)
您访问的对象为空,这会导致此错误

您需要检查elem是否不为null:

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

当对没有返回元素的LINQ查询调用扩展方法时,会引发此错误

因此,如果没有id为mytodos的ul元素,则在以下代码中调用.ToList()将导致错误

List<IWebElement> list = driver.FindElements(By.XPath("//ul[contains(@id, 'mytodos')]//span[contains(@id, 'mytodo')]")).ToList()
List List=driver.FindElements(By.XPath(//ul[contains(@id,'mytodo'))]//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();
    }
    
    List=null;
    var elements=driver.FindElements(By.XPath(//ul[contains(@class,'mytodo'))]//span[contains(@id,'mytodo')]);
    if(元素!=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     | 
    
    功能:SpecFlowFeature1
    为了查看和检查我的待办事项
    作为规划用户
    我想创建并查看我的待办事项和已完成待办事项
    @我的标签
    场景:检查TODO的默认数量
    给定用户位于todolist.me主页上
    然后用户看到7个待办事项的列表
    场景大纲:检查TODO创建
    给定用户位于todolist.me主页上
    用户使用内容创建新todo时:
    然后用户看到内容的todo:
    示例:
    |内容|
    |纯文本|
    | 1234567890      |
    | ~!@#$%^&*()_-+|
    场景概述:可以检查Chech todos并将其标记为已完成
    给定用户位于todolist.me主页上
    当用户使用文本创建新todo时:
    然后用户用文本检查todo:
    然后,用户看到文本没有待办事项:
    然后,用户看到已完成的文本操作:
    示例:
    |正文|
    |自定义文本|
    
    我的SpecFlow也有同样的问题,但我的有点不同: 因为我想对两个“场景大纲”使用相同的“示例”部分,所以我想我可以将这两个部分都放在末尾。但这不起作用

    问题实际上在于元素的正确顺序 (另请参见“示例”下的: 在“场景大纲”之后,必须跟随“示例”部分,无论您是否对多个“场景大纲”使用相同的“示例”部分!否则您将得到所描述的错误

    您不能将所有“示例”部分都放在功能文件的末尾,也不能将多个“场景大纲”放在一个“示例”部分中。;-)


    我希望,这能帮助其他遇到同样问题的人。

    如果您有
    场景大纲,但没有
    示例
    部分,也会出现此错误


    在这种情况下,您需要将其从
    场景大纲
    更改为仅
    场景

    此答案是错误的,因为问题说明了编译时错误。此答案是错误的,因为问题说明了编译时错误。@TomW“您需要检查元素是否不为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     |