在量角器上執行規范時,我注意到我的一些條件在失敗時,規范被標記為通過但它應該失敗。我的 async/await 方法可能有問題,需要有人向我指出為什么我沒有將規范視為失敗以及如何修復它。

static async SelectDocument(){
try{
let docXpath = 'this is an error on purpose. it is not showing as fail in the specs. Why?';
let docElement = element(by.xpath(docXpath));
await browser.wait(.EC.vidibilityOf(docElement),3000);//code stops here but spec is not mark as fail.
await browser.sleep(1000);
docElement.click();
}catch(e){
await logger.log().error(e)}
}
}
輸出:

uj5u.com熱心網友回復:
@Victor在任何時候,“TRY”塊中的任何步驟都失敗,執行進入“CATCH”塊并從中執行步驟。
try-catch 的目的是,如果任何嘗試失敗,執行將進入 catch 塊并且不會發生代碼終止。
你需要從 try 塊中取出你的代碼然后執行,你會看到它在哪里失敗,什么是失敗,你也會看到失敗計數。
https://www.w3schools.com/python/python_try_except.asp
我看到的另一個錯誤是 xpath:
let docElement = element(by.xpath("//locatorTag[contains(text(), docXpath)]")
在這里'locatorTag'將是您的包含特定文本的html標簽
還有一點:腳本報告任何斷言失敗而不是陳述句失敗的失敗。在這里(在您的螢屏截圖中突出顯示),您目前擁有的是陳述句失敗。
uj5u.com熱心網友回復:
感謝@Gaurav Lad,我能夠找到解決方案。
我不知道正如他上面提到的那樣:“腳本報告任何斷言失敗而不是陳述句失敗的失敗。”
多虧了這個評論,我能夠包裝預期的可見性條件,然后做出一個成功失敗規范的斷言。我的測驗中不再出現無聲錯誤!
static async ExpectVisibilityByElement(Elem: ElementFinder, waitTime?: number) {
{
let wait = 3000
if (waitTime != undefined) {
wait = waitTime;
}
let output;
let errorObject;
try {
await browser.wait(EC.visibilityOf(Elem), wait);
await logger.Log().info("Element " Elem.locator() " is visible.");
output = true;
} catch (error) {
output = false;
errorObject = error;
await logger.Log().error(error);
}
expect(output).toBe(true,Elem.locator() 'is not visible.')
if(!output) {
await logger.Log().error('SPEC FAILED. The rest of the spec will not be excecuted.');
throw errorObject.name ': ' errorObject.message;
}
}
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/435276.html
上一篇:如何將串列放入csv檔案python中的一個單元格(在我的情況下是一本書的作者串列),一個單元格中的所有作者
下一篇:如何按文字點擊?
