DS_*_*per 8 protractor angular
我正在尝试简单地计算我的旋转木马e2e组件测试
carousel.po.ts
import { browser, element, by, Key } from 'protractor';
export class CarouselDemoPage {
navigateTo() {
return browser.get('/design/carousel');
}
getCarouselComponent(index: number) {
return element.all(by.css('cfc-carousel')).get(index);
}
getCarouselIndicators(index: number) {
return this.getCarouselComponent(index).element(by.css('.indicators')).all(by.repeater('item in items'));
}
}
Run Code Online (Sandbox Code Playgroud)
我的spec文件:
import { CarouselDemoPage } from './carousel.po';
describe('Carousel component', () => {
let page: CarouselDemoPage;
beforeEach(() => {
page = new CarouselDemoPage();
page.navigateTo();
});
it('At least one carousel component should exist', () => {
expect<any>(page.getCarouselComponent(0)).toBeDefined();
});
it('Check correct number of indicators displayed', () => {
expect<any>(page.getCarouselIndicators(0).count()).toEqual(4);
});
});
Run Code Online (Sandbox Code Playgroud)
我有最新的或接近最新的至少包
"@ angular/core":"^ 5.0.0-beta.7","jasmine-core":"~2.8.0","量角器":"~5.1.2"
第一次测试运行正常,第二次测试结束
1)轮播组件检查显示的指示器的正确数量 - 失败:等待异步角度任务在20秒后完成的超时.这可能是因为当前页面不是Angular应用程序.有关详细信息,请参阅常见问题解答:https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular
在等待带定位器的元素时 - 定位器:By(css selector,cfc-custom-select)
免责声明 我在这里有ngAfterViewInit()中的setTimeout:
ngAfterViewInit() {
// Needs the timeout to avoid the "expression has changed" bug
setTimeout(() => {
this.items = this.viewItems.toArray().concat(this.contentItems.toArray());
this.totalItems = this.items.length;
this._first = this.items[0];
this._last = this.items[this.totalItems - 1];
this._setItemsOrder(this.currentFrame);
this._setInterval();
}, 0);
}
Run Code Online (Sandbox Code Playgroud)
因此,我尝试了
browser.ignoreSynchronization = true;
Run Code Online (Sandbox Code Playgroud)
和
browser.driver.sleep(50);
Run Code Online (Sandbox Code Playgroud)
和
browser.waitForAngular();
Run Code Online (Sandbox Code Playgroud)
但后来我把数量计算为0
所以经过一些调试我发现我的轮播组件中的setInterval会破坏测试
我应该使用browser.ignoreSynchronization = true; ??
有任何想法吗?
因此,由于轮播组件中的 setInterval 和其他超时功能,我需要添加
browser.ignoreSynchronization = true;
Run Code Online (Sandbox Code Playgroud)
我将getCarouselIndicators函数稍微修改为:
getCarouselIndicators(index: number) {
browser.ignoreSynchronization = true;
return this.getCarouselComponent(index).all(by.css('.indicators li'));
}
Run Code Online (Sandbox Code Playgroud)
现在测试已解决并且完美运行!