如何验证量角器中表中所有行的列数据

时间:2018-03-30 08:23:19

标签: html protractor

我尝试使用以下代码验证所有行的特定列的数据。

以下是我用过的代码:

 var mytable = element(By.xpath("//div[@class='col-sm-12']/table/tbody"));
 let rows_table: any = mytable.findElements(By.tagName("tr"));
 console.log("rows_table"+rows_table);
 browser.sleep(10000);
 let rows_count: any = rows_table.size();

 for (var row = 0; row < rows_count; row++) {
    var Columns_row = rows_table.get(row).findElements(By.tagName("td"));
    var columns_count = Columns_row.size();
    console.log("Number of cells In Row " + row + " are " + columns_count);
    for (var column = 0; column < columns_count; column++) {
         var celtext = Columns_row.get(column).getText();
         console.log("Cell Value of row number " + row + " and column number 
         " + column + " Is " + celtext);`

但它不起作用。

1 个答案:

答案 0 :(得分:0)

您可以使用xpath直接查找一列的所有td。

var util = require('util'); // it's nodejs build-in module
var expect= require('chai').expect;

function readColumn(columnName) {
    let columnIndexes = {
        'columnA': 0,
        'columnB': 1
    };

    let columnIndex = columnIndexes[columnName];

    let xpath = util.format(
       "//div[@class='col-sm-12']/table/tbody/tr/td[%s]",
        ++columnIndex);

    return element.all(by.xpath(xpath)).getText();
}

let expectValues = ['Car', 'Cat'];

readColumn('columnA').then(function(values) {
    // use Chai do assertion
    expect(values).to.deep.equal(expectValues)
});