我試圖創建一個函數,我可以調用任何測試,將看看被傳遞的元素(鏈接文本,Css選擇器,Xpath,ID),點擊元素,然後驗證出現的URL加載後。我遇到的問題是它在函數完成之前返回。如何在返回前等待函數完成?
我知道我需要實現異步和回調,但我很難理解結構。
clickIDverifyURL: function clickByID (elementVar, elementURL){
var rem = this.remote;
// if statements to look at elementVar and select the right one.. example:
// rem.setFindByTimeout(10000)
// .findByXpath (elementVar)
// .click()
// .end()
return this.remote
// if I code it right, I shouldn't need this sleep right?
.sleep(30000)
.getCurrentUrl()
.then(function(currURL) {
console.log(currURL);
try {
assert.strictEqual(currURL, elementURL, "This test checks to see if the current URL is correct.")
}
catch (e)
{
console.log(e)
}
});
}
感謝任何幫助或意見。
感謝jason0x43。我現在對如何構建它有了更好的瞭解。爲元素類型創建單獨的方法是否有意義?我希望能夠使用正則表達式來分解第一個字符,以便開發人員只需將幾個元素(css選擇器,xpath或div id)交給我們即可。難道去: 'myModule.findElement( 'someselector ') 。然後(myModule.clickIDverifyURL(' expectedURL'))' 若然,應findElement是回報上以維持鏈條? – whatthestack
爲此,你可以做一些類似......恩,評論對此不太好。我會更新答案... – jason0x43