使用nightwatch.js手动覆盖子项

huangapple go评论83阅读模式
英文:

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 :

&lt;ul class=&quot;dv-packdocs&quot;&gt;
 &lt;li class=&quot;dv-packdoc&quot;&lt;div class=&quot;icon-todo&quot;&gt;&lt;/li&gt;
 &lt;li class=&quot;dv-packdoc&quot;&lt;div class=&quot;icon-todo&quot;&gt;&lt;/li&gt;
 &lt;li class=&quot;dv-packdoc&quot;&lt;div class=&quot;icon-todo&quot;&gt;&lt;/li&gt;
 &lt;li class=&quot;dv-packdoc&quot;&lt;div class=&quot;icon-todo&quot;&gt;&lt;/li&gt;
&lt;/ul&gt;

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调用以获取列表的长度,然后以相同的方式使用它来确定要点击哪个liviewer.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(&#39;ul.dv-packdocs li:nth-child(2)&#39;) (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) =&gt; {
    // 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(&#39;ul.dv-packdocs li:nth-child(&#39;+length+&#39;)&#39;) (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!

huangapple
  • 本文由 发表于 2020年1月3日 19:23:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577739.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定