gpt4 book ai didi

javascript - Click() 和 then() 不起作用 - Protractor

转载 作者:行者123 更新时间:2023-11-29 19:11:29 25 4
gpt4 key购买 nike

我正在用 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 类,则此测试将通过。一个更好的方法是引入自定义的 toHaveClass 匹配器,请参阅:

关于javascript - Click() 和 then() 不起作用 - Protractor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38569269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com