Angularjs 角度量角器测试:如何在没有ng绑定和ng模型的情况下检查范围内的变量?
如何在没有ng绑定和ng模型的情况下检查作用域中的变量Angularjs 角度量角器测试:如何在没有ng绑定和ng模型的情况下检查范围内的变量?,angularjs,protractor,bdd,cucumberjs,Angularjs,Protractor,Bdd,Cucumberjs,如何在没有ng绑定和ng模型的情况下检查作用域中的变量 <span ng-if="user.code === 'YELLOW'"> <span class="glyphicons glyphicons-circle-remove text-danger"></span> Yellow </span> <span ng-if="user.code === 'GREEN'"> <span class="glyphico
<span ng-if="user.code === 'YELLOW'">
<span class="glyphicons glyphicons-circle-remove text-danger"></span> Yellow
</span>
<span ng-if="user.code === 'GREEN'">
<span class="glyphicons glyphicons-circle-exclamation-mark text-warning"></span> Green
</span>
或
错误:
NoSuchElementError: No element found using locator: by.binding("user.code")
NoSuchElementError: No element found using locator: by.model("user.code")
第一种解决方案
如果你能通过CSS选择器找到你的元素
$('.glyphicons-circle-remove').isPresent(); // For user.code == 'YELLOW'
$('.glyphicons-circle-exclamation-mark').isPresent(); // For user.code == 'GREEN'
黑道
仅在使用ng If
$('[ng-if="user.code === \'YELLOW\'"]').isPresent(); // For user.code == 'YELLOW'
$('[ng-if="user.code === \'GREEN\'"]').isPresent(); // For user.code == 'GREEN'
因为该元素仅在满足<代码>ng条件时出现。所以你也可以这样做
注意:
.isPresent()
因为ng if
将从DOM中删除元素。
如果您使用的是
ng show
,则需要改用.isDisplayed()
(这不适用于第二种解决方案)。您不应该在量角器中测试此类内容。您应该从用户的角度编写测试
但是,如果您想测试,请使用评估。获取对DOM元素的引用,然后调用evaluate:
// Choose a DOM element bound to a scope that contains the value you
// want to test
expect($('span').evaluate('user.code')).toBe('some value');
是的,但我的网页上有几个图标。更多CSS选择器详细信息或使用我在答案中更新的第二个解决方案。它很脏,但肯定能用。
$('[ng-if="user.code === \'YELLOW\'"]').isPresent(); // For user.code == 'YELLOW'
$('[ng-if="user.code === \'GREEN\'"]').isPresent(); // For user.code == 'GREEN'
// Choose a DOM element bound to a scope that contains the value you
// want to test
expect($('span').evaluate('user.code')).toBe('some value');