如何编写测试用例来检查 table 数据使用量角器 js 按升序和降序排序

how write a test case to check table data is sorted in ascending and descending order using protractor js

如何在量角器 js 中检查 table 数据是否排序

它('check for the presence of Transactions grid',函数(){ // browser.sleep(20000); 元素(by.css('[ng-click="toggleSelection(\'backtest\',\'transactions\', portfolio)"]')).click(); browser.sleep(5000);

    var items = element.all(by.repeater('stock in tableData.none'));

    element(by.xpath('//*[@id="collapse0"]/div[3]/div[2]/div/div[4]/div[1]/div[2]/div[3]/a')).click();
    browser.sleep(50000);

    var unsorted = items.map(function(element) {
        return element.getText();
      });
      // console.log(unsorted);
      var sorted = unsorted.then(function(texts) {

        // Make sure to copy the array to not sort the original
        return texts.slice(0).sort();
      });

      var equals = protractor.promise.all([unsorted, sorted]).then(function(texts) {
        var unsorted = texts[0];
        var sorted = texts[1];
        var allEqual = (unsorted.length === sorted.length);
        sorted.forEach(function(sortedItem, i) {
          allEqual = allEqual && (sortedItem === unsorted[i]);
        });
        return allEqual;
      });

      expect(equals).toBe(true);
  });

如何检查table数据是否排序

我最近刚好有同样的需求。这个想法是

  • 使用适当的xpath/css-selector将每一列数据读入数组
  • 测试该数组是否已排序

这是我使用过的工作示例代码。它假定 table 是按升序排序的,但按其他方式排序也应该很容易。

Table:

<table class="sortedElements">
    <thead>
        <tr>
            <th>Number</th>
            <th>String</th>
        </tr>
    </thead>
    <tbody>
        <tr><td>1</td><td>aaa</td></tr>
        <tr><td>2</td><td>aab</td></tr>
        ... sorted data in both tds goes on ...
    </tbody>
</table>

规格测试(整数):

var integers = [];
var allIntegers = element.all(by.css('.sortedElements > tbody > tr > td:nth-child(1)'));
allIntegers.each((td) => {
    td.getText().then((val) => {
        integers.push(val)
    })
}).then(() => {
    expect(integers.every((val, i) => (i < integers.length - 1 ? parseInt(val) <= parseInt(integers[i + 1]) : true)) === true).toBeTruthy();
});

规范测试(字符串):

var strings = [];
var allStringsTds = element.all(by.css('.sortedElements > tbody > tr > td:nth-child(2)'));
allStringsTds.each((td) => {
    td.getText().then((val) => {
        strings.push(val)
    })
}).then(() => {
    expect(strings.every((val, i) => (i < strings.length - 1 ? val <= strings[i + 1] : true)) === true).toBeTruthy();
});

希望对您有所帮助..