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

現在測試解決並且完美地工作!