简体   繁体   English

Serenity-JS从列表中选择一个元素

[英]Serenity-JS Select an Element from a list

I have filtered drop down list which list all tasks. 我已经过滤了下拉列表,其中列出了所有任务。 When I type letters into the search box I am presented with a list of tasks that start with those letters. 当我在搜索框中键入字母时,会看到以这些字母开头的任务列表。

My Serenity-JS / Cucumber test inputs the first two characters in the 'Given' see below cucumber. 我的Serenity-JS / Cucumber测试在“ Given”中输入前两个字符,请参见下面的黄瓜。 But I am trying to use Serenity to select an Item from the list of options. 但是我试图使用Serenity从选项列表中选择一个项目。

Given James has entered 'Ta' into the tasks box
When he selects 'Take out the Trash' from the task list options
Then he sees 'Take Out the Trash' in the heading

The Code I am using to find the tasks is something like this: 我用来查找任务的代码是这样的:

static List_Of_All_Tasks = Target.the('List of all tasks').located(by.className('task'));

This returns a list of 'tasks' 这将返回“任务”列表

My question is using the normal Serenity-js Pattern. 我的问题是使用普通的Serenity-js模式。 How do I select an Item in the list? 如何在列表中选择一个项目?

The Click.on() takes a Target but how do I specify something like List_Of_All_Tasks.located(by.id='Take_Out_The_Trash') Click.on()采用目标,但如何指定类似List_Of_All_Tasks.located(by.id='Take_Out_The_Trash')

You have several options here, so assuming that each item in the list has a CSS class task and an ID derived from its name: 您在这里有几个选项,因此假设列表中的每个项目都有一个CSS类task和一个从其名称派生的ID:

  1. You can generate the selector dynamically using Target.of 您可以使用Target.of动态生成选择器

     const TodoList = { Task: Target.the('task to {0}').located(by.css('.task#{0}'), } 

    and then: 接着:

     actor.attemptsTo( Click.on(TodoList.Task.of('Take_Out_The_Trash')) ); 

    Have a look at the test cases for the Target class to see how this can be accomplished. 查看一下Target类的测试用例 ,以了解如何实现。

  2. Alternatively, you can generate the whole Target dynamically: 另外,您可以动态生成整个Target

     const TodoList = { TaskTo: (name: string) => Target.the(`task to ${name}`) .located(by.css(`.task#${name}`) } 

    and then: 接着:

     actor.attemptsTo( Click.on(TodoList.TaskTo('Take_Out_The_Trash')) )); 
  3. If you can't do any of the above or need to do something a bit more sophisticated like filtering the list for example, you could define a custom Interaction, resolve the element using locate(target) or locateAll(target) , which will give you an instance of Protractor's ElementFinder or ElementArrayFinder respectively, and take it from there: 如果您无法执行上述任何操作,或者需要做一些更复杂的操作(例如,过滤列表),则可以定义一个自定义的Interaction,使用locate(target)locateAll(target)解析该元素,您分别是量角器的ElementFinder或ElementArrayFinder的实例,并从那里获取它:

     const Tasks = Target.the('List of all tasks').located(by.className('task')); const SelectTask = { called: (name: string) => Interaction.where(`#actor selects a task to ${name}`, actor => BrowseTheWeb.as(actor).locateAll(Tasks)./* continue with ElementArrayFinder methods */ } 

    and then: 接着:

     actor.attemptsTo( SelectTask.called('Take_Out_The_Trash') ); 

    Have a look at the those examples to see how protractor's $ and $$ selectors can be used. 看一下这些例子 ,看看如何使用量角器的$$$选择器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM