[英]Protractor- Changing ControlFlow to async-await
一個更詳細的解釋是describe
bloks僅用於分組。 async
函數是“用於返回的承諾鏈的語法糖”,並且describe
塊通常不支持(如等待解決)返回的承諾。
另外,最好將所有require
聲明放在describe
塊之外。 我將像下面那樣重組該代碼,然后重試:
const projectsPage = require('../pages/Projects_Page.js');
const commonFunctions = require('../utils/CommonFunctions.js');
describe('Projects CRUD', () => {
it('Rename Project - Duplicate name check', async () => {
//your code here
});
});
像Jasmine,Mocha之類的測試框架包含這些關鍵字,例如describe,before,beforeEach等。 測試框架具有內置的執行順序這些塊的定義。 對於describe和It塊,函數定義為
export const describe: {
/**
* Registers a new test suite.
* @param name The suite name.
* @param fn The suite function, or {@code undefined} to define a pending test suite.
*/
(name: string, fn: Function): void;
export const it: {
/**
* Add a test to the current suite.
* @param name The test name.
* @param fn The test function, or {@code undefined} to define a pending test case.
*/
(name: string, fn: Function): void;
它表示描述,它的塊取兩個參數。 名稱和功能。 在describe的情況下,您不需要使用async / await,因為此函數在內部使用“ return”。 因此它將在描述塊內等待整個事情完成。 換句話說,如果您僅在describe塊中編寫異步,那么您將必須在describe函數下編寫await,這不是必需的。
在It塊的情況下,您必須像異步function(){}一樣編寫,因為您必須等待在它下面編寫的步驟。 請參考以下示例。
describe('angularjs homepage', function() {
it('should greet the named user', async function() {
await browser.get('http://www.angularjs.org');
await element(by.model('yourName')).sendKeys('Julie');
var greeting = element(by.binding('yourName'));
expect(await greeting.getText()).toEqual('Hello Julie!');
});
注意:如果函數不返回Promise,則不需要使用await。 這沒用。 在上面的代碼片段中,元素之前沒有等待。 這意味着element不返回promise,它僅返回ElementFinder的對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.