javascript - 如何在 Cypress 中编写可重用的“之前”?
问题描述
我想编写可重用的“之前”部分,但未能将变量从主文件(A.js)传递到导入的代码(HelloUtil.js)。提前感谢您提供的任何建议。
js
import * as UTIL from "./HelloUtil"
let variableFileA = "Hello"
// I hope the imported 'before' section is able to get 'variableFileA'
describe(`Do something`, () => {
it('A "It" section', () => {
cy.log("....")
});
})
HelloUtil.js
before('Reusable "before" 1', () => {
cy.log("lets begin....")
});
before('Reusable "before" 2', () => {
cy.log("print variable:"+variableFileA)
});
我收到的结果:
解决方案
不,这是行不通的。
如果要在其中使用变量,则需要在其中定义variableFileA
变量。HelloUtil.js
或者您可以将变量作为参数传递。但你也没有这样做。
可以工作的是:
实用程序.js
export const beforeFunc = () => {
cy
.log('Before func from utils.js');
};
测试.js
import { beforeFunc } from './utils';
describe('My example test suite', () => {
before(beforeFunc);
});
如果你想传递一些参数,你可以这样做:
实用程序.js
export const beforeFunc = (greetings) => {
cy
.log(greetings);
};
测试.js
import { beforeFunc } from './utils';
describe('My example test suite', () => {
before(() => beforeFunc('Hi'));
});
以及来自测试运行器的结果:
推荐阅读
- javascript - 无法在 MeteorJS 中导入模块
- python - 如何从python中的列中获取完整的十进制值?
- oracle - 从 Databricks 连接到 Oracle
- tensorflow - 如何修复 ValueError: None 仅在第一维中受支持。张量“input_1”的形状无效“[None, None, None, 3]”
- reactjs - 首先我在我的组件中创建 Home.js 然后我尝试在我的主组件中呈现 Markdown
- uwp - 如何让 UWP 应用在任务栏上的图标更大更透明?
- javascript - Wait for request to be sent, but not response with Javascript promises
- r - Fit 2 Graphs Side By Side in Shiny Navbar Page Without Using box()
- python-3.x - python中正则表达式的使用
- flutter - main.dart 的 init 方法中的提供程序