Click() 和 then() 不起作用 - 量角器

Click() and then() not working - Protractor

我正在用 protractor 编写测试,我的目标是单击一个元素并检查它是否具有特定的 class。问题是我正在使用 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 class,则此测试将通过。更好的方法是引入自定义 toHaveClass 匹配器,请参阅:

  • How to test if an element has class using Protractor?