我是自动化测试、Protractor 和 angularJS 的新手。我有一个我想计算的列表,可能复制到一个数组,并验证列表文本是否存在。例如,列表向用户显示景点、容量和内容,以便他们知道他们拥有什么权限。
下面是.html
<div class="home-info">
<div class="home-top home-section">
<h3>User Information</h3>
<div class="home-box">
<div class="property-group wide">
<span>
Change Phillips<br />
</span>
</div>
</div>
<div class="home-box">
<div class="property-group wide">
<div>Editors:</div>
<span>
<ul class="property-stack">
<li><span>Attractions</span>
</li>
<li><span>Capacity</span>
</li>
<li><span>Content</span>
</li>
<li><span>Media</span>
</li>
<li><span>Options</span>
</li>
<li></li>
<li></li>
<li><span>Upload CADs</span>
</li>
</ul>
</span>
</div>
</div>
</div>
下面是我写的代码。我可以获得列表中的第一项,但是使用 .all 对我不起作用。
var text = "";
browser.driver.findElement.all(By.xpath("//li/span")).count().then(function(count) {
initialCount = count;
console.log(initialCount);
});
browser.driver.findElement(By.xpath("//li/span")).getText().then(function(text) {
console.log(text);
});
我试图避免使用 xpath,因为我被告知要尽量避免。老实说我迷路了。提前感谢您的帮助。
用于匹配的代码:
expect(myLists).toEqual(['Attractions', 'Capacity', 'Conent',
'Media', 'Options', 'Upload CADs'
]);
最佳答案
我不确定您使用的是哪个版本的 Protractor ,但您应该能够在没有浏览器或驱动程序前缀的情况下调用 element。使用 element.all应该为您提供您正在寻找的元素数组。
如果您想访问该数组中的特定索引,您可以使用 .get(index) element.all 的后缀
所以如下:
1.你得到了元素的数组
2.得到数组的个数
3.我们调用一个for循环来遍历数组的所有索引
4. 数组的每个索引我们调用 getText() 并将其打印到控制台
var j = 0; // using this since the i iterator in the for loop doesn't work within a then function
var textList = [];
var text = "";
var myLists = element.all(by.css("li span"));
myLists.count().then(function(count) {
console.log(count);
for(int i = 0; i < count; i++){
myLists.get(i).getText().then(function(text) {
textList[j++] = text;
console.log(text);
});
}
});
编辑:
在研究过程中,我实际上找到了另一种通过使用 .each() 遍历元素数组的方法。 element.all 的后缀。
var j = 0; // using this since the i iterator in the for loop doesn't work within a then function
var textList = [];
var text = "";
var myLists = element.all(by.css("li span"));
myLists.count().then(function(count) {
console.log(count);
myLists.each(function(element, index) {
element.getText().then(function (text) {
textList[j++] = text;
console.log(index, text);
});
});
});
您应该能够使用 textList 数组来匹配事物。
expect(textList).toEqual(['Attractions', 'Capacity', 'Conent',
'Media', 'Options', 'Upload CADs'
]);
关于javascript - Protractor AngularJS 计数、复制和验证列表跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31658987/