我正在使用 Protractor
编写测试,我的目标是单击一个元素并检查它是否具有特定的类
。问题是我使用 click()
,然后使用 then()
,但出现以下错误:
Cannot read property 'getAttribute' of null
问题位于以下代码块中:
element(by.css('#region1 polygon:first-child')).click()
.then(function(selected){
expect(selected.getAttribute('class')).toContain('highlighted');
});
您知道如何解决这个问题吗?预先感谢您的回复!!
最佳答案
click()
回调没有元素本身作为参数。换句话说,您的情况下的 selected
不是一个元素。
相反,只要一步步做,让Control Flow队列完成这项工作:
var elm = element(by.css('#region1 polygon:first-child'));
elm.click();
expect(elm.getAttribute('class')).toContain('highlighted');
请注意,toContain()
并不是应用于 class
属性值的最佳匹配器。例如,如果元素具有 not-highlighted
类,则此测试将通过。更好的方法是引入自定义 toHaveClass
匹配器,请参阅:
关于javascript - Click() 和 then() 不起作用 - Protractor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38569269/