Javascript 量角器:读取表格内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29501976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:28:32  来源:igfitidea点击:

Protractor : Read Table contents

javascripttestingprotractorend-to-end

提问by JDunn

I've been writing e2e tests for my angular js app and am unable to figure this out. I've got a table with data in it. I want to extract the first rows data.

我一直在为我的 angular js 应用程序编写 e2e 测试,但无法弄清楚这一点。我有一张表,里面有数据。我想提取第一行数据。

<table>
    <tr>
        <td><\td>
        <td><\td>
        <td><\td>
    </tr>
</table>

I did this in protractors elementExplorerand it prints out the values of all 3 columns

我用量角器做了这个,elementExplorer它打印出所有 3 列的值

element.all(by.repeater('item in items.list')).get(0).getText()
James
Byrne
1

If I do this, it prints out the first column value

如果我这样做,它会打印出第一列值

element.all(by.repeater('item in items.list')).get(0).element(by.css('td')).getText()
WARNING - more than one element found for locator By.cssSelector("td") - the first result will be used
James

My Question is, how do I get the values of the other columns?

我的问题是,如何获取其他列的值?

回答by alecxe

Use all()in conjunction with map():

使用all()会同map()

var row = element.all(by.repeater('item in items.list')).first();
var cells = row.all(by.tagName('td'));

var cellTexts = cells.map(function (elm) {
    return elm.getText();
});

Then, you can assert it to be an array of column texts:

然后,您可以将其断言为列文本数组:

expect(cellTexts).toEqual(["The first text", "The second text", "The third text"]);

回答by Abbas ALI

Easiest way would be as below:

最简单的方法如下:

var tabledata = element.all(by.css("./table"));

// get rows 
var rows = tabledata.all(by.tagName("tr"));

// get cell values
var cells = rows.all(by.tagName("td"));

expect(cells.get(0).getText()).toEqual("something")
expect(cells.get(1).getText()).toEqual("something")
expect(cells.get(2).getText()).toEqual("something")

I implemented it and it is working for me.

我实施了它,它对我有用。