Github Copilot協助解決cucumber插件不支持async/await

一、提示詞

問題描述

在使用了@badeball/cypress-cucumber-preprocessor插件后,存在不支持nodejs原生的promise和async/await語法問題

執行用例命令

npx cypress run --env configFile=mhesi-staging,TAGS="@API005" --spec "cypress/integration/AL/API/*.feature"

報錯信息

Error: Cucumber preprocessor detected that you returned a native promise from a function handler, this is not supported. Using async / await is generally speaking not supported when using Cypress, period, preprocessor or not.
? ? ? at runStepWithLogGroup (https://thai-portal.staging.mhesi-skill.org/__cypress/tests?p=cypress\integration\AL\API\api.feature:17265:17)
? ? ? at Context.eval (https://thai-portal.staging.mhesi-skill.org/__cypress/tests?p=cypress\integration\AL\API\api.feature:17751:58)

約束條件?

1.在Cypress 10.0.0+框架下的自動化測試項目中,使用了大量的async/await語法,修復工作量比較大,需要維持這種使用

2.cypress-cucumber-preprocessor插件不支持Cypress 10.0.0+版本,因此替換成@badeball/cypress-cucumber-preprocessor插件,請不要建議我替換成cypress-cucumber-preprocessor插件

3.在./cypress/api和./cypress/integration/_utils目錄下的js文件,需要兼容在nodejs和browser環境中運行,因此涉及fs,path和crypto等模塊的調用,代碼需要考慮兼容兩個環境,最好在統一的文件中封裝處理好后,便于項目全局可調用

任務

提供一個解決方案,在Cypress 10.0.0+版本中,能夠支持async/await的原生promise語法,使用@badeball/cypress-cucumber-preprocessor插件的解決方案,或者其它cucumber插件的結局方案均可

方案驗證方法

命令npx cypress run --env configFile=mhesi-staging,TAGS="@API005" --spec "cypress/integration/AL/API/*.feature"能夠執行成功

二、AI給的解決方案

1.編寫一個wrapAsync函數,兼容cucumber preprocessor

cypress/support/async-wrapper.js文件

/*** Utility to wrap async functions for Cypress cucumber compatibility* Converts async/await to Cypress chainable commands*//*** Wraps an async function to make it compatible with Cypress cucumber preprocessor* @param {Function} asyncFn - The async function to wrap* @returns {Function} - A function that returns a Cypress chainable*/
export function wrapAsync(asyncFn) {return function(...args) {return cy.wrap(null).then(() => {return asyncFn.apply(this, args);});};
}/*** Wraps an async function and handles any errors appropriately* @param {Function} asyncFn - The async function to wrap* @returns {Function} - A function that returns a Cypress chainable with error handling*/
export function wrapAsyncWithErrorHandling(asyncFn) {return function(...args) {return cy.wrap(null).then(async () => {try {const result = await asyncFn.apply(this, args);return result;} catch (error) {cy.log(`Error in async function: ${error.message}`);throw error;}});};
}/*** Helper to execute multiple async operations in sequence* @param {Array} asyncOperations - Array of async functions to execute* @returns {Function} - A function that returns a Cypress chainable*/
export function executeAsyncSequence(asyncOperations) {return cy.wrap(null).then(async () => {const results = [];for (const operation of asyncOperations) {const result = await operation();results.push(result);}return results;});
}/*** Helper to execute async operation with retry logic* @param {Function} asyncFn - The async function to retry* @param {number} maxRetries - Maximum number of retries* @param {number} delay - Delay between retries in ms* @returns {Function} - A function that returns a Cypress chainable*/
export function wrapAsyncWithRetry(asyncFn, maxRetries = 3, delay = 1000) {return function(...args) {return cy.wrap(null).then(async () => {let lastError;for (let i = 0; i <= maxRetries; i++) {try {return await asyncFn.apply(this, args);} catch (error) {lastError = error;if (i < maxRetries) {cy.log(`Retry ${i + 1}/${maxRetries} after error: ${error.message}`);await new Promise(resolve => setTimeout(resolve, delay));}}}throw lastError;});};
}

2.提供一個跨環境的適配層,以滿足nodejs和browser兩個運行環境

cypress/support/cross-env-utils.js

/*** Cross-environment compatibility layer for Node.js modules* Works in both Node.js and browser environments*/let fs, path, crypto;// Detect environment and import appropriate modules
if (typeof window === 'undefined') {// Node.js environmenttry {fs = require('fs-extra');path = require('path');crypto = require('crypto');} catch (e) {// Fallback if modules are not availableconsole.warn('Node.js modules not available:', e.message);}
} else {// Browser environment - use polyfills or mocksfs = require('../mocks/fs-mock');path = require('path-browserify');crypto = require('crypto-browserify');
}/*** File system operations compatible with both environments*/
export const FileSystem = {readFileSync: (filePath, encoding = 'utf8') => {if (typeof window === 'undefined' && fs) {return fs.readFileSync(filePath, encoding);}// Browser fallback - could use cy.readFile or fetchthrow new Error('File system operations not supported in browser environment');},writeFileSync: (filePath, data, encoding = 'utf8') => {if (typeof window === 'undefined' && fs) {return fs.writeFileSync(filePath, data, encoding);}throw new Error('File system operations not supported in browser environment');},existsSync: (filePath) => {if (typeof window === 'undefined' && fs) {return fs.existsSync(filePath);}return false;},readJsonSync: (filePath) => {if (typeof window === 'undefined' && fs && fs.readJSONSync) {return fs.readJSONSync(filePath);}// Fallback for browserconst content = FileSystem.readFileSync(filePath);return JSON.parse(content);}
};/*** Path operations compatible with both environments*/
export const PathUtils = {join: (...paths) => {if (path && path.join) {return path.join(...paths);}// Simple fallback for browserreturn paths.join('/').replace(/\/+/g, '/');},resolve: (...paths) => {if (path && path.resolve) {return path.resolve(...paths);}// Simple fallback for browserreturn PathUtils.join(...paths);},dirname: (filePath) => {if (path && path.dirname) {return path.dirname(filePath);}// Simple fallbackreturn filePath.split('/').slice(0, -1).join('/') || '/';},basename: (filePath, ext) => {if (path && path.basename) {return path.basename(filePath, ext);}// Simple fallbackconst name = filePath.split('/').pop() || '';return ext && name.endsWith(ext) ? name.slice(0, -ext.length) : name;}
};/*** Crypto operations compatible with both environments*/
export const CryptoUtils = {createHash: (algorithm) => {if (crypto && crypto.createHash) {return crypto.createHash(algorithm);}throw new Error('Crypto operations not available in this environment');},randomBytes: (size) => {if (crypto && crypto.randomBytes) {return crypto.randomBytes(size);}// Browser fallback using Web Crypto APIif (typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues) {const buffer = new Uint8Array(size);window.crypto.getRandomValues(buffer);return buffer;}throw new Error('Random bytes generation not available in this environment');},createUuid: () => {if (crypto && crypto.randomUUID) {return crypto.randomUUID();}// Fallback UUID v4 generatorreturn 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {const r = Math.random() * 16 | 0;const v = c === 'x' ? r : (r & 0x3 | 0x8);return v.toString(16);});}
};/*** Environment detection utilities*/
export const Environment = {isNode: () => typeof window === 'undefined',isBrowser: () => typeof window !== 'undefined',isCypress: () => typeof window !== 'undefined' && typeof window.Cypress !== 'undefined'
};/*** Async operation wrapper for cross-environment compatibility*/
export const AsyncUtils = {delay: (ms) => new Promise(resolve => setTimeout(resolve, ms)),retryAsync: async (fn, maxRetries = 3, delay = 1000) => {let lastError;for (let i = 0; i <= maxRetries; i++) {try {return await fn();} catch (error) {lastError = error;if (i < maxRetries) {await AsyncUtils.delay(delay);}}}throw lastError;}
};// Export everything as default for easy importing
export default {FileSystem,PathUtils,CryptoUtils,Environment,AsyncUtils
};

.cypress/mocks/fs-mock.js

/*** Mock fs module for browser environment*/const fsMock = {readFileSync: (filePath, encoding = 'utf8') => {throw new Error('File system operations not supported in browser environment. Use cy.readFile() instead.');},writeFileSync: (filePath, data, encoding = 'utf8') => {throw new Error('File system operations not supported in browser environment.');},existsSync: (filePath) => {return false;},readJSONSync: (filePath) => {throw new Error('File system operations not supported in browser environment. Use cy.fixture() instead.');},ensureDirSync: (dirPath) => {// No-op in browser},copySync: (src, dest) => {throw new Error('File system operations not supported in browser environment.');}
};module.exports = fsMock;

3.更新Cucumber測試步驟

原步驟

//添加培訓到頻道
When(/^user blinds Train item "([^"]*)" in Library "([^"]*)" to Channel "([^"]*)"$/, (trainName, libraryName, channelName) => {libraryName = cy.data(libraryName);trainName = cy.data(trainName);channelName = cy.data(channelName);cy.wrap(Library.queryLibrary(libraryName)).then((libraryId) => {cy.wrap(Channel.queryChannel(channelName)).then((channelId) => {cy.wrap(Channel.addTrain(channelId, "Recommended by window", libraryId, trainName)).then(() => {cy.wrap(Channel.saveChannel(channelId));});});});
});

修改后的步驟(代碼可讀性和可維護性提升)

import { wrapAsync, wrapAsyncWithErrorHandling } from '../../../support/async-wrapper';//添加培訓到頻道
When(/^user blinds Train item "([^"]*)" in Library "([^"]*)" to Channel "([^"]*)"$/, wrapAsync(async (trainName, libraryName, channelName) => {libraryName = cy.data(libraryName);trainName = cy.data(trainName);channelName = cy.data(channelName);const libraryId = await Library.queryLibrary(libraryName);const channelId = await Channel.queryChannel(channelName);await Channel.addTrain(channelId, "Recommended by window", libraryId, trainName);await Channel.saveChannel(channelId);
}));

?4.更新Cucumber插件配置

移除package.json中的cucumber插件配置部分

  "cypress-cucumber-preprocessor": {"nonGlobalStepDefinitions": true,"stepDefinitions": ["cypress/integration/**/*.js","cypress/integration/**/**/*.js"]},

根目錄下創建.cypress-cucumber-preprocessorrc文件

{"stepDefinitions": "cypress/integration/**/*.js","filterSpecs": true,"omitFiltered": true,"nonGlobalStepDefinitions": false
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/86964.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/86964.shtml
英文地址,請注明出處:http://en.pswp.cn/web/86964.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C++多線程【Linux】

Linux的多線程 Linux的子線程實際上也是個進程&#xff0c;但是比傳統的進程輕量化。 pthread pthread是用于Linux系統下的線程庫&#xff0c;頭文件是<pthread.h>。C11 之前的多線程開發高度依賴平臺原生 API&#xff0c;Windows 以 CreateThread 和內核對象為核心&am…

Windows 環境下 NVM 命令詳解:多版本 Node.js 管理利器

“一個 Node.js 版本走天下&#xff1f;太局限了&#xff01;試試 nvm&#xff0c;版本切換如絲般順滑。” 什么是 NVM NVM&#xff08;Node Version Manager&#xff09;是一個命令行工具&#xff0c;允許你安裝并在多個 Node.js 版本之間自由切換。 在 Linux/macOS 下常用的…

一二級路由之間的傳參方式以及高亮問題

實現如下圖所示的一二級路由的高亮情況&#xff1a; 在一級路由APP.vue下設置&#xff1a; .head a.router-link-active {background-color: rgb(235, 221, 204); }在二級路由Mycenter.vue下設置&#xff1a; /* 要求在點擊跳轉到mycenter_lianxi頁面時候父路由保持高亮…

前端JavaScript力扣HOT100刷題【51-100】

注&#xff1a;純手打&#xff0c;如有錯誤歡迎評論區交流&#xff01; 轉載請注明出處&#xff1a;https://blog.csdn.net/testleaf/article/details/148953015 編寫此文是為了更好地學習前端知識&#xff0c;如果損害了有關人的利益&#xff0c;請聯系刪除&#xff01; 本文章…

智能制造數字孿生集成交付生態鏈:智慧產線極速克隆,孿生重構生產周期

在智能制造的浪潮中&#xff0c;數字孿生技術正以前所未有的速度重塑制造業的生產模式。從產品設計到生產制造&#xff0c;再到運維管理&#xff0c;數字孿生通過構建物理世界的虛擬鏡像&#xff0c;實現了生產全流程的數字化映射與優化。 山東融谷信息以“智能制造數字孿生集成…

非常詳細版: dd.device.geolocation 釘釘微應用獲取定位,移動端 PC端都操作,Vue實現釘釘微應用獲取精準定位并渲染在地圖組件上

dd.device.geolocation 釘釘微應用獲取定位,釘釘微應用獲取精準定位并渲染在地圖組件上 ,手機端 PC端要都可用 【dd.device.geolocation是需要鑒權的哦】 想要的數據和效果圖 想要的數據格式 代碼 <template><div class="dialogStyles"

鴻蒙5:組件狀態共享

目錄 1. 組件狀態共享 1.1 狀態共享-父子傳值&#xff1a;Local、Param、Event 1.2 狀態共享-父子雙向綁定!! 1.3 跨代共享&#xff1a;Provider和Consumer 1.3.1 aliasName和屬性名 1.3.2 實現跨代共享 1.3.3 裝飾復雜類型&#xff0c;配合Trace一起使用 1.3.4 支持共…

【MySQL】12. C語言與數據庫的連接

1. 下載MySQL的連接庫 sudo apt install -y libmysqlclient-dev 2. MySQL連接庫的常用接口介紹 通過下面的樣例了解MYSQL的常用接口&#xff1a; #include <iostream> #include <mysql/mysql.h> using namespace std;const char *host "localhost";…

[springboot系列] 探秘JUnit 5: Java單元測試利器

介紹 JUnit 5 是一個用于 Java 編程語言的單元測試框架&#xff0c;它是 JUnit 框架的第五個版本&#xff0c;與 JUnit 4 相比&#xff0c;JUnit 5 提供了許多改進和新特性&#xff0c;包括更好的擴展性、靈活性和對現代 Java 特性的支持。 JUnit 5 由三個主要的子模塊組成&a…

開源 java android app 開發(十三)繪圖定義控件、搖桿控件的制作

文章的目的為了記錄使用java 進行android app 開發學習的經歷。本職為嵌入式軟件開發&#xff0c;公司安排開發app&#xff0c;臨時學習&#xff0c;完成app的開發。開發流程和要點有些記憶模糊&#xff0c;趕緊記錄&#xff0c;防止忘記。 相關鏈接&#xff1a; 開源 java an…

Python 庫 包 sentence-transformers

sentence-transformers 是一個非常流行的 Python 庫&#xff0c;專門用于將文本&#xff08;句子、段落、文檔&#xff09;轉換為高質量的語義向量&#xff08;嵌入&#xff09;。它基于 Transformer 架構&#xff08;如 BERT、RoBERTa、DistilBERT 等&#xff09; 的預訓練模型…

《聚類算法》入門--大白話篇:像整理房間一樣給數據分類

一、什么是聚類算法&#xff1f; 想象一下你的衣柜里堆滿了衣服&#xff0c;但你不想一件件整理。聚類算法就像一個聰明的助手&#xff0c;它能自動幫你把衣服分成幾堆&#xff1a;T恤放一堆、褲子放一堆、外套放一堆。它通過觀察衣服的顏色、大小、款式這些特征&#xff0c;把…

AutoGen(五) Human-in-the-Loop(人類在環)實戰與進階:多智能體協作與Web交互全流程(附代碼)

AutoGen Human-in-the-Loop&#xff08;人類在環&#xff09;實戰與進階&#xff1a;多智能體協作與Web交互全流程&#xff08;附代碼&#xff09; 引言&#xff1a;AI自動化的極限與人類參與的價值 在大模型&#xff08;LLM&#xff09;驅動的AI應用開發中&#xff0c;完全自…

并查集 Union-Find

目錄 引言 簡單介紹 淺淺總結 算法圖解 初始化 根節點查找 集合合并 連通性檢查 例題 大概思路 完整代碼&#xff1a; 引言 一個小小的并查集讓我們在ccpc卡了那么久(還有unordered_map,如果不是忘了map自動排序這么一回事也不至于試那么多發)&#xff0c;至今仍然心有…

書籍在行列都排好序的矩陣中找數(8)0626

題目&#xff1a; 給定一個有N*M的整型矩陣matrix和一個整數K&#xff0c;matrix的每一行和每一列都是排好序的。實現一個函數&#xff0c;判斷K是否在matrix中。 0 1 2 5 2 3 4 7 4 4 4 8 5 …

深度學習04 卷積神經網絡CNN

卷積神經網絡與人工神經網絡關系與區別 概念 卷積神經網絡&#xff08;Convolutional Neural Network, CNN&#xff09;是人工神經網絡&#xff08;Artificial Neural Network, ANN&#xff09;的一種特殊形式&#xff0c;兩者在核心思想和基礎結構上存在關聯&#xff0c;但在…

vue基礎之組件通信(VUE3)

文章目錄 前言一、父子組件通信1.父組件向子組件通信2.子組件向父組件通信3.ref父組件直接操作子組件通信。 二、跨代通信1. 跨層級通信2.事件總線通信 總結 前言 vue3的組件通信和vue2相比在語法上會有些差距&#xff0c;且vue3有的通信方式也在功能上比vue2更加完善&#xf…

【RidgeUI AI+系列】中文重復統計器

中文重復統計器 文字重復統計是一個使用文本處理工具&#xff0c; 輸入文本內容并指定最小詞長度后&#xff0c; 就能自動高亮顯示重復的詞。 本教程將會借助AI實現這個應用的開發 頁面腳本編寫 該工具的基礎流程較為清晰&#xff1a;用戶輸入一段文字后&#xff0c;調用提取…

代碼隨想錄|圖論|05島嶼數量(深搜DFS)

leetcode:99. 島嶼數量 題目 題目描述&#xff1a; 給定一個由 1&#xff08;陸地&#xff09;和 0&#xff08;水&#xff09;組成的矩陣&#xff0c;你需要計算島嶼的數量。島嶼由水平方向或垂直方向上相鄰的陸地連接而成&#xff0c;并且四周都是水域。你可以假設矩陣外均…

數據結構-第二節-堆棧與隊列

一、概念&#xff1a; 堆棧與隊列也是線性表&#xff0c;但是&#xff1a; 堆棧&#xff1a;只能在一個端進行插入刪除&#xff0c;此端稱為棧頂。&#xff08;特點&#xff1a;后來居上&#xff09; 隊列&#xff1a;在一端進行插入&#xff08;隊尾&#xff09;&#xff0…