我正在尝试简单地计算我的旋转木马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'));
}
}
我的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);
});
});
我有最新的或接近最新的至少包
“@angular/core”: “^5.0.0-beta.7”,
“jasmine-core”: “~2.8.0”,
“protractor”: “~5.1.2”
第一次测试运行正常,第二次测试结束时间
1)轮播组件检查显示的正确指示器数量
– 失败:超时等待异步Angular任务在20秒后完成.这可能是因为当前页面不是Angular应用程序.有关详细信息,请参阅常见问题解答:https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular
在等待带定位符的元素时 – Locator: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);
}
因此,我尝试了
browser.ignoreSynchronization = true;
和
browser.driver.sleep(50);
和
browser.waitForAngular();
但后来我把数量计算为0
所以经过一些调试后我发现我的轮播组件中的setInterval会破坏测试
我应该使用browser.ignoreSynchronization = true; ??
有任何想法吗?
因此,由于carousel组件中的setInterval和其他超时功能,我需要添加
browser.ignoreSynchronization = true;
我稍微修改了我的getCarouselIndicators函数
成为:
getCarouselIndicators(index: number) {
browser.ignoreSynchronization = true;
return this.getCarouselComponent(index).all(by.css('.indicators li'));
}
现在测试解决并完美运行!