我正在嘗試在 Javascript 中使用 Selenium 設定一些測驗用例,以進行簡單的登錄頁面測驗。我正試圖通過在 Promise 中拒絕來設定正確的錯誤捕獲。這是我的承諾代碼:
//Test Case 1: Valid Email / Valid Password
async function tc1() {
announceTC();
//Create promise to handle execution of async test case
let promise = new Promise(async function (resolve, reject) {
//Create the webdriver (Chrome) and open an instance of the test page
const driver = createDriver();
//Enter valid email / valid password
await validEmail(driver, reject);
await validPassword(driver);
//Click login button
await clickLoginButton(driver);
//Test for a successful login
await testSuccessfulLogin(driver, resolve, reject);
//Exit the driver instance.
await driver.quit();
//Trigger the callback functions
promise
.then(value => console.log(value),
value => console.log(value))
.catch(value => console.log(value))
.finally(() => tc2());
});
};
在我的 validEmail 函式中,如果發生錯誤,我會嘗試拒絕。這是我的該功能的代碼:
//Find and enter valid email on login page
async function validEmail(driver, reject) {
try{
//Click email input box.
let emailBox = await driver.wait(until.elementLocated(By.xpath("//*[@id='email']/iput")), 5000);
await emailBox.click();
//Input email.
driver.findElement(By.xpath("//*[@id='email']/input")).sendKeys("[email protected]");
} catch(error) {
console.log(error);
reject(tcErrorMsg());
}
}
我對這個錯誤的理解是,當您沒有提供處理被拒絕案例的承諾時,就會發生這種錯誤。
我的 testSuccessfulLogin 函式使用了 resolve 并且 promise 正確地通過了 .then ,所以我不確定為什么當我以同樣的方式使用 reject 時會出現未處理的拒絕錯誤。
uj5u.com熱心網友回復:
下面的代碼應該移到異步函式之外
//Trigger the callback functions
promise
.then(value => console.log(value),
value => console.log(value))
.catch(value => console.log(value))
.finally(() => tc2());
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/447578.html
標籤:javascript 硒 测试 es6-承诺
上一篇:在testcafe中訪問環境變數
