通过绑定 'ng-src' 获取元素不起作用,Protractor
Get element by binding for 'ng-src' not working, Protractor
在 Angular Phone cat 应用程序(第 10 步)中,我无法使用 element(by.binding('bindingName'))
Protractor locators
教程link:Angular phone cat app (step-10, test)
详情:
// Working
expect(element(by.css('img.phone')).getAttribute('src'))
.toMatch(/img\/phones\/nexus-s.0.jpg/);
// Not Working
expect(element(by.binding('mainImageUrl')).getAttribute('src'))
.toMatch(/img\/phones\/nexus-s.0.jpg/);
<img ng-src="{{mainImageUrl}}" class="phone">
by.binding
在这种情况下不起作用 by definition。
根据 source code,它只会匹配一个元素,至少,如果元素上有 ng-binding
class。
我想稍微修饰一下 alecxe 的回答。只要 不是属性的一部分,任何 {{ }}
表达式都有效
例如:
<p class="phone">{{someText}}</p>
可以通过以下方式找到:
element(by.binding('someText'))
换句话说,您的模板不需要包含 ng-binding class,因为它是由 Angular 自动生成的。也就是说,目前有一个 bug,如果绑定是属性或属性值的一部分,则 ng-binding class 不会由 Angular 生成,Protractor 也不会生成能够找到元素。
这也是我的问题,我找不到任何量角器功能来解决这个问题,所以这是我建议的解决方案。 :)
该解决方案基于量角器可以通过ng-bind获取元素并获取输入的值属性。 (我不知道为什么 getText() 输入不起作用 :D)
..
<img ng-src="{{mainImageUrl}}" class="phone">
<input type="hidden" ng-bind="mainImageUrl" value="{{mainImageUrl}}">
..
在javascript中:
element(by.binding('mainImageUrl')).getAttribute('value')
.then(function(text){
expect(text.toMatch(/img\/phones\/nexus-s.0.jpg/));
});
在 Angular Phone cat 应用程序(第 10 步)中,我无法使用 element(by.binding('bindingName'))
Protractor locators
教程link:Angular phone cat app (step-10, test)
详情:
// Working
expect(element(by.css('img.phone')).getAttribute('src'))
.toMatch(/img\/phones\/nexus-s.0.jpg/);
// Not Working
expect(element(by.binding('mainImageUrl')).getAttribute('src'))
.toMatch(/img\/phones\/nexus-s.0.jpg/);
<img ng-src="{{mainImageUrl}}" class="phone">
by.binding
在这种情况下不起作用 by definition。
根据 source code,它只会匹配一个元素,至少,如果元素上有 ng-binding
class。
我想稍微修饰一下 alecxe 的回答。只要 不是属性的一部分,任何 {{ }}
表达式都有效
例如:
<p class="phone">{{someText}}</p>
可以通过以下方式找到:
element(by.binding('someText'))
换句话说,您的模板不需要包含 ng-binding class,因为它是由 Angular 自动生成的。也就是说,目前有一个 bug,如果绑定是属性或属性值的一部分,则 ng-binding class 不会由 Angular 生成,Protractor 也不会生成能够找到元素。
这也是我的问题,我找不到任何量角器功能来解决这个问题,所以这是我建议的解决方案。 :) 该解决方案基于量角器可以通过ng-bind获取元素并获取输入的值属性。 (我不知道为什么 getText() 输入不起作用 :D)
..
<img ng-src="{{mainImageUrl}}" class="phone">
<input type="hidden" ng-bind="mainImageUrl" value="{{mainImageUrl}}">
..
在javascript中:
element(by.binding('mainImageUrl')).getAttribute('value')
.then(function(text){
expect(text.toMatch(/img\/phones\/nexus-s.0.jpg/));
});