英文:
Manually override the child using nightwatch.js
问题
我目前正在编写一个Nightwatch测试,以从列表中选择一个新文档。我需要能够手动覆盖需要选择的子项编号吗?
例如,当前使用的选择器是:
<ul class="dv-packdocs">
<li class="dv-packdoc"><div class="icon-todo"></div></li>
<li class="dv-packdoc"><div class="icon-todo"></div></li>
<li class="dv-packdoc"><div class="icon-todo"></div></li>
<li class="dv-packdoc"><div class="icon-todo"></div></li>
</ul>
测试可能类似于:
viewer.selectNewDocument([2])
这会选择ul
下的第二个子元素吗?
还是我需要指定每个子元素?
英文:
I am currently writing a Nightwatch test to select a new document from a list. And I will need to be able to select the next in the list. Is there a way to manually override the child number that needs selecting?
For example the current selector being used is :
<ul class="dv-packdocs">
<li class="dv-packdoc"<div class="icon-todo"></li>
<li class="dv-packdoc"<div class="icon-todo"></li>
<li class="dv-packdoc"<div class="icon-todo"></li>
<li class="dv-packdoc"<div class="icon-todo"></li>
</ul>
and the test would be something like :
viewer.selectNewDocument([2])
would this select the second child under the ul
?
Or would I have to specify each child element?
答案1
得分: 1
如果我理解正确,您正在尝试从动态长度的列表(ul
,该列表根据用户输入或其他站点操作进行填充)中动态查找适当的子元素(li
)。正确吗?
我看到两种不同的情况,分别有两种不同的方法:
1. 您有一个固定的条件(用于识别目标元素的方式):例如,在您的列表中,可以使用以下命令来定位第二个li
。
viewer.selectNewDocument('ul.dv-packdocs li:nth-child(2)')
(假设您向selectNewDocument
函数传递完整的选择器)
或者
viewer.selectNewDocument(2)
,将一个number
传递给命令,并在命令内部生成选择器(如果您关心美观性):
selectNewDocument: function(index) {
this.api.perform((done) => {
// Click the second document in the list:
let selector = `ul.dv-packdocs li:nth-child(${index})`;
this.api.click(selector);
done();
});
return this;
},
或者,如果您想要选择最后添加的文档,那么您需要对ul
进行一个elements调用以获取列表的长度,然后以相同的方式使用它来确定要点击哪个li
:viewer.selectNewDocument('ul.dv-packdocs li:nth-child('+length+')')
(其中length
是您的elements
调用的结果)。
2. 您没有固定的条件(如果第一部分没有涵盖,或者稍后会填补这一部分,因为在假期之后,时间有点紧张)
希望这是您正在寻找的答案!祝您好运! Cheers!
英文:
If I understand correctly, you are trying to dynamically find the appropriate child element (li
) from a dynamical length list (ul
, where the list is populated based on user input, or other site actions). Correct?
I see two scenarios with two different approaches:
1. You have a set/fixed condition (way of identifying your target element): for example, in your list, the second li
would be targeted by the below command.
viewer.selectNewDocument('ul.dv-packdocs li:nth-child(2)')
(considering you are passing a complete selector to the selectNewDocument
function)
, or
viewer.selectNewDocument(2)
, passing a number
& form the selector inside the command (if you care for aesthetics):
selectNewDocument: function(index) {
this.api.perform((done) => {
// Click the second document in the list:
let selector = `ul.dv-packdocs li:nth-child(${index})`;
this.api.click(selector);
done();
});
return this;
},
Alternatively, if you would want the last document added, then you would have to issue a elements call on the ul
to retrieve the length of the list, then use that in the same way to determine which li
you have to click: viewer.selectNewDocument('ul.dv-packdocs li:nth-child('+length+')')
(where length
is the result of your elements
call).
2. You don't have a fixed condition (I'll fill this up if the first part doesn't cover it, or later today, kinda slammed after the holidays)
Hope it's what you were looking for! Cheers!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论