2017-09-29 25 views
8

我试图做简单的算我的旋转木马端到端组件测试角:量角器 - 计数()不解决,造成超时

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')); 
    } 
} 

而且我的规格文件:

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); 
    }); 
}); 

我有所有最新或接近最新至少包

“@角/芯”: “^ 5.0.0-beta.7”, “茉莉核”: “〜2.8.0”, “量角器”: “〜5.1.2”

第一次测试运行良好,第二次测试超时

1)轮播组件检查显示的指示器的正确数量 - 失败:超时等待异步Angular任务在20秒后完成。这可能是因为当前页面不是Angular应用程序。请参见常见问题解答了解详情:https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular

在等待与定位器部件 - 定位:通过(CSS选择器,CFC-自定义选择)

免责声明 我确实有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; ??

任何想法?

+0

你的web应用程序的任何部分是否利用角? – demouser123

+0

是不是很明显? @ demouser123 –

+0

为什么它会“显而易见”?我没有看到你在问题主体中明确提及它。 – demouser123

回答

4

如此,因为setInterval的,并在转盘成分以外的超时功能,我需要添加

browser.ignoreSynchronization = true; 

,我稍微修改了getCarouselIndicators功能 是:

getCarouselIndicators(index: number) { 
    browser.ignoreSynchronization = true; 
    return this.getCarouselComponent(index).all(by.css('.indicators li')); 
} 

现在测试解决并且完美地工作!