2017-09-25 157 views
0

我爲Intern上的Web應用程序編寫功能測試。 我有我描述的測試的所有操作的文件,也有一個測試,這些動作被稱爲函數setFindTimeout不適用於我

例如:

有一個Action.ts文件

它的功能,其在測試被順序地稱爲

//1 
//open the registration window 
openRegistration(): Command<void> { 
    return Action.openRegistration(this.parent); 
} 

static openRegistration(command: Command<any>): Command<void> { 
    return command 
     // click on the authorization menu 
     .setPageLoadTimeout (10000) 
     .get(intern.args.url) 
     .end() 
} 

//2 
inputTextByCssSelector(selector: string, value: string): Command <void> { 
    return Input.inputTextByCssSelector(this.parent, selector, value); 
} 

static inputTextByCssSelector(
    command: Command<any>, 
    selector: string, 
    value: string 
): Command<void> { 
    return command 
     .setFindTimeout(10000) 
     .findByCssSelector(selector) 
     .click() 
     .type(value) 
     .end() 
     .end() 
} 

這樣

.then(() => action.openRegistration()) 
.then(() => input.inputTextByCssSelector(
    "input [name = userName]", 
    intern.args.username 
)) 
.then(() => input.inputTextByCssSelector(
    "input [name = password]", 
    intern.args.password 
)) 

但是當我運行測試時,它會下降。

如果我設置在openRegistration結束例如一個明確的延遲這樣

openRegistration(): Command<void> { 
    return Action.openRegistration(this.parent); 
} 

static openRegistration(command: Command<any>): Command<void> { 
    return command 
     .setPageLoadTimeout(10000) 
     .get(intern.args.url) 
     .sleep(7000) 
     .end() 
} 

然後一切正常

爲什麼不inputTextByCssSelector工作setFindTimeout(10000),但在openRegistrationsleep(7000)工作

回答

0

「它掉落」是什麼意思?測試是否引發超時錯誤?

一個潛在的問題是組件可見性。當頁面加載和你試圖與之交互的元素變得可見時(例如,動畫中的JS淡入淡出),有沒有延遲? findBy命令返回找到的第一個元素,但該元素可能不可見。如果不可見,實習生無法與其交互,並且像type這樣的命令將失敗。要等到元素可見,請使用findDisplayedByCssSelector

請注意,間距在CSS選擇器中很重要。選擇器"input [name = userName]"實際上是在查找包含在input元素中的屬性爲name=userName的元素。假設實際意圖是選擇具有特定名稱屬性的輸入,則其格式應爲'input[name="userName"]'

另請注意,end()命令僅在find命令之後需要,通常不需要在幫助程序命令(從this.parent開始)中的命令鏈末尾。因此,例如,在openRegistrationget之後不需要end,並且在inputTextByCssSelector(對於findByCssSelector命令)中最多需要一個end

0

當我第一次開始學習如何使用實習生並碰到一些類似的問題時,我嘗試了一些類似的東西(只是沒有使用TypeScript)。

我的問題是Promise鏈在執行測試時沒有正確維護。您應該嘗試對代碼進行如下細微更改,以提高Promise鏈的一致性。

所以只是舉例,你的測試腳本是這樣我們開始之前:

return this.remote 
    .then(() => action.openRegistration()) 
    .then(() => input.inputTextByCssSelector("input[name = userName]", intern.args.username)) 
    .then(() => input.inputTextByCssSelector("input[name = password]", intern.args.password)) 

你需要做的第一件事就是刪除這些箭頭的功能。我使用箭頭功能,即this.remote leadfoot時,有幾個問題/我這樣做的時候會話沒有傳來傳去始終方法之間。

所以,你的第一.then()語句調用一個名爲openRegistration()方法,對嗎?編輯您的方法,而是返回function它執行你正在尋找的步驟:

static openRegistration(): Command<void> { 
    return function() { 
     return this.parent 
      .setPageLoadTimeout (10000) 
      .get(intern.args.url) 
      .end() 
    }; 
} 

所以,現在你的測試腳本會是這個樣子(假設你重複這種模式您所要求的所有方法) :

return this.remote 
    .then(action.openRegistration()) 
    .then(input.inputTextByCssSelector("input[name = userName]", intern.args.username)) 
    .then(input.inputTextByCssSelector("input[name = password]", intern.args.password)) 

這應該可以解決您的問題。