如何通过nightwatch.js测试搜索结果

时间:2017-06-04 09:09:49

标签: javascript nightwatch.js

我使用nightwatch.js做一些端到端的测试。现在我想检查特定的搜索结果是否符合我的期望。结果如下:

var myMovie= [];
var first= {id:"tt3783958", title:"La La Land", year:"2016",
            type:"Comedy, Drama, Music"};
myMovie.push(first);

localStorage.setItem('myMovie', JSON.stringify(myMovie));
var output = JSON.parse(localStorage.getItem('myMovie'));

这意味着有三个结果: Sample One Sample Subcategory Sample Three 。第二个还有子结果 Sub 1 Sub 2

我找不到类似<div id="results" role="list" class="ui list"> <div role="listitem" class="item"> <span><strong>Samp</strong>le One</span> </div> <div role="listitem" class="item multiple pointer "> <div class="ui accordion"> <div class="title pointer"> <span><strong>Samp</strong>le Subcategory</span> </div> <div class="content pointer"> <div role="list" class="ui list"> <div role="listitem" class="item pointer">Sub 1</div> <div role="listitem" class="item pointer">Sub 2</div> </div> </div> </div> </div> <div role="listitem" class="item pointer"> <span><strong>Samp</strong>le Three</span> </div> </div> 的内容,并检查结果元素的数量和每个值。所以我尝试使用getElements,但我认为这是错误的方式......

getText

1 个答案:

答案 0 :(得分:0)

确实不存在

getElements。但您可以使用elements代替!

以下脚本为您提供每个结果的结果数和文本内容:

module.exports = {
  'getElements': function (browser) {

    function getInfo(elements) {
      console.log('Number of results: ' + elements.value.length);

      elements.value.forEach(function (element) {
        browser.elementIdText(element.ELEMENT, function (res) {
          console.log('RESULT ' + element.ELEMENT + '\n', res.value);
        });
      });
    }

    browser
      .url('http://www.website.com/') // <--- Don't forget to change this!
      .waitForElementPresent('body', 1000)
      .elements('css selector', '#results > div[role="listitem"]', getInfo)
      .end();

  }
};