2017-07-27 69 views
1

我有一段代碼在量角器做一個等待:量角器等待超時漁獲物和繼續

public waitForElement() { 
    return browser.wait(
     ExpectedConditions.visibilityOf(element(by.id('#someEl'))), 
     10000, 
     'Unable to find the element' 
     ); 
} 

我的問題是,我似乎不能,如果超時捕獲此異常。我嘗試添加一個catch()子句,這是不行的,如:

this.waitForElement() 
    .then(() => { /* do something */ }) 
    .catch(() => { /* handle the error -- this code never happens if there is a timeout!!! */ }); 

我試圖把代碼放在一個try-catch塊,但這並沒有幫助:

try { this.waitForElement().then(() => { }); } 
catch (ex) { /* this exception is never caught, the test just fails!! */ } 

我很難過:我怎麼能趕上等待超時,並繼續測試,而不會失敗?

+0

嗨,在量角器的日誌中拋出的錯誤是什麼。其次,如果你使用'waifForElement()'方法而不是在方法外部捕獲錯誤,會發生什麼? – wswebcreation

+0

好主意,但結果是一樣的 - 測試失敗,沒有給我機會去發現異常 – riqitang

回答

1

我創建這個簡單的測試用例,見下文

// conf 
 
exports.config = { 
 
    capabilities: { 
 
    'browserName': 'chrome' 
 
    }, 
 

 
    // Framework to use. Jasmine is recommended. 
 
    framework: 'jasmine', 
 

 
    // Spec patterns are relative to the current working directory when 
 
    // protractor is called. 
 
    specs: ['example_spec.js'], 
 

 
    // Options to be passed to Jasmine. 
 
    jasmineNodeOpts: { 
 
    defaultTimeoutInterval: 30000 
 
    }, 
 
    allScriptsTimeout: 30000, 
 
}; 
 

 
// Spec 
 
describe('Wait for element',() => { 
 
    it('element will be found',() => { 
 
    browser.get('http://www.angularjs.org'); 
 

 
    waitForElement(element(by.css('.hero'))) 
 
     .then(() => { 
 
     console.log('element is found'); 
 
     }) 
 
     .catch((error) => { 
 
     console.log('error = ', error); 
 
     }); 
 
    }); 
 
    it('element will NOT be found',() => { 
 
    browser.get('http://www.angularjs.org'); 
 

 
    waitForElement(element(by.css('.heroic'))) 
 
     .then(() => { 
 
     console.log('element is found'); 
 
     }) 
 
     .catch((error) => { 
 
     console.log('element is not found, do something different'); 
 
     }); 
 
    }); 
 
}); 
 

 
function waitForElement(element) { 
 
    return browser.wait(
 
    ExpectedConditions.visibilityOf(element), 
 
    3000, 
 
    'Unable to find the element' 
 
); 
 
}

這給了我下面的輸出

~/wswebcreation/contributions/protractor npm run test.example 

> [email protected] test.example /Users/wswebcreation/contributions/protractor 
> node ./bin/protractor example/conf.js 

[10:32:17] I/launcher - Running 1 instances of WebDriver 
[10:32:17] I/hosted - Using the selenium server at http://localhost:4444/wd/hub/ 
Started 
element is found . 
element is not found, do something different. 


2 specs, 0 failures 
Finished in 6.917 seconds 

[10:32:24] I/launcher - 0 instance(s) of WebDriver still running 
[10:32:24] I/launcher - chrome #01 passed 

所以它看起來像它的工作原理

你也可以做點什麼IKE在此與您的方法

function waitForElement(element, maxWaitTime, failOnError) { 
 
    maxWaitTime = maxWaitTime || 10000; 
 
    failOnError = failOnError || false; 
 

 
    return browser.wait(ExpectedConditions.visibilityOf(element), maxWaitTime) 
 
    .then((found) => Promise.resolve(found)) 
 
    .catch((waitError) => { 
 
     if (failOnError) { 
 
     return Promise.reject(`waitForElement: ${waitError} for expected condition ${expectation} for the element: ${element}`); 
 
     } else { 
 
     return Promise.resolve(false); 
 
     } 
 
    }); 
 
}

這使得更多的「flexibel」,你可以在方法捕獲錯誤,如果需要讓它失敗出現,否則傳遞一個布爾值,你可以使用走得更遠。

+0

我的測試中存在一個缺陷,它在waitForElement()的調用中表現出來 - 感謝您花時間幫助 – riqitang

+0

由於我在beforeAll()方法中調用waitForElement(),並且waitForElement()拋出異常時,Jasmine開始執行beforeEach(),而beforeAll()仍在進行時,我正在觀察奇怪。 beforeAll()方法仍然繼續,但僅在beforeEach()完成執行之後。我不知道爲什麼,但茉莉花似乎有異常被拋出beforeAll() – riqitang

+0

你可以把你完整的腳本在你最初的問題嗎? – wswebcreation