如果其中一个参数未在使用 Cypress 的 CLI 命令中声明,则抛出异常?
Throw an exception if one of the parameters is not declared in the CLI command using Cypress?
实际结果是:
我刚收到一个运行,等了半个小时也没有任何反应。
预期结果是:
如果缺少特定参数,则抛出特定异常。
我目前没有任何错误消息。如果我尝试 运行 它与例如 PAGE_TITLE 等于 undefined 没有任何反应。
尝试过的场景:
我将位于 package.json、docker:test-data-creation:add-widgets 中的所有常量放在一个名为 requiredFields 的常量数组中,然后从 validateParameters 函数调用 requiredFields,该函数不适用于未知原因。
显示一些代码:
这是我使用特定参数调用 --spec 的地方
"docker:test-data-creation:add-widgets": "XSOCK=/tmp/.X11-unix && XAUTH=/tmp/.docker.xauth && xauth nlist :0 | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge - && chmod 755 $XAUTH && docker run --rm -it -v $XSOCK:$XSOCK -v $XAUTH:$XAUTH -e XAUTHORITY=/tmp/.docker.xauth -e DISPLAY=$DISPLAY --network=sity_static-network -v $PWD:/test -w /test cypress/included:3.8.1 --browser chrome --env baseUrl=http://symfony.local,PAGE_TITLE='Slider Swiper Widget Test',MAX_WIDGETS_IN_PAGE=50,TOTAL_WIDGETS_COUNT=250,WIDGET_TYPE='SliderSwiper',WIDGETS_WITH_ROWS=50 --spec 'cypress/integration/creation/dynamically_add_widgets_spec.js'"
部分如果 dynamically_add_widgets_spec.js
* Name of the page where you like to save this widgets
*/
const PAGE_TITLE = Cypress.env('PAGE_TITLE');
/**
* Maximum widgets in the page
*/
const MAX_WIDGETS_IN_PAGE = Cypress.env('MAX_WIDGETS_IN_PAGE');
/**
* Number of total widgets to be added to the page
*/
const TOTAL_WIDGETS_COUNT = Cypress.env('TOTAL_WIDGETS_COUNT');
/**
* Just change the type of the widget to select your needed widget
*/
const WIDGET_TYPE = Cypress.env('WIDGET_TYPE');
/**
* This variable is needed because we want to build some widgets with rows and some widgets don't need rows. The number below is how many widgets one need with rows.
*/
const WIDGETS_WITH_ROWS = Cypress.env('WIDGETS_WITH_ROWS');
/**
* This is a variable that is used to count needed widgets with rows. This variable should always stay 0.
*/
let ROWS_COUNTER = 0;
/**
* This constant is used to determine if all required fields are declared in the cli command
*/
const requiredFields = [
PAGE_TITLE,
MAX_WIDGETS_IN_PAGE,
TOTAL_WIDGETS_COUNT,
WIDGET_TYPE,
WIDGETS_WITH_ROWS
];
这是我调用它的地方
context("Dynamically create widgets", () => {
validateParameters(requiredFields);
createWidgets(WIDGET_TYPE, TOTAL_WIDGETS_COUNT, MAX_WIDGETS_IN_PAGE, WIDGETS_WITH_ROWS);
});
validateParameters 函数 -->
function validateParameters(requiredFields) {
requiredFields.forEach((field) => {
if (Cypress.env(field) !== undefined) {
return;
}
throw {message: `Field ${field} is required. Please provide it using --env ${field}. Please look at README.md for an example.`};
})
}
P.S。我知道这个 forEach 无法工作,因为如果缺少特定参数我需要特定消息,但我试图获取此消息,然后如果缺少特定参数则获取特定消息。
问题是在 validateParameters
函数中您传递了常量数组。每个常量都会有值,例如 PAGE_TITLE = Cypress.env('PAGE_TITLE')
;所以它将是 .env
中定义的任何内容。这可能是字符串、数字、未定义或其他内容。
要解决问题,您需要检查传递的字段是否未定义。
function validateParameters(requiredFields) {
requiredFields.forEach((field) => {
if (field !== undefined) {
return;
}
throw {message: `Field ${field} is required. Please provide it using --env ${field}. Please look at README.md for an example.`};
})
}
您已经使用 Cypress.env('...') 设置了上面的 requiredFields 变量,因此您不需要再次调用它。
赛普拉斯文档声明 Cypress.env('...') returns,变量或未定义。
/**
* Returns specific environment variable or undefined
* @see https://on.cypress.io/env
* @example
* // cypress.json
* { "env": { "foo": "bar" } }
* Cypress.env("foo") // => bar
*/
env(key: string): any
在此处查看有关 Cypress 环境的更多信息:https://docs.cypress.io/guides/guides/environment-variables.html#Overriding-Configuration
实际结果是:
我刚收到一个运行,等了半个小时也没有任何反应。
预期结果是: 如果缺少特定参数,则抛出特定异常。
我目前没有任何错误消息。如果我尝试 运行 它与例如 PAGE_TITLE 等于 undefined 没有任何反应。
尝试过的场景: 我将位于 package.json、docker:test-data-creation:add-widgets 中的所有常量放在一个名为 requiredFields 的常量数组中,然后从 validateParameters 函数调用 requiredFields,该函数不适用于未知原因。
显示一些代码: 这是我使用特定参数调用 --spec 的地方
"docker:test-data-creation:add-widgets": "XSOCK=/tmp/.X11-unix && XAUTH=/tmp/.docker.xauth && xauth nlist :0 | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge - && chmod 755 $XAUTH && docker run --rm -it -v $XSOCK:$XSOCK -v $XAUTH:$XAUTH -e XAUTHORITY=/tmp/.docker.xauth -e DISPLAY=$DISPLAY --network=sity_static-network -v $PWD:/test -w /test cypress/included:3.8.1 --browser chrome --env baseUrl=http://symfony.local,PAGE_TITLE='Slider Swiper Widget Test',MAX_WIDGETS_IN_PAGE=50,TOTAL_WIDGETS_COUNT=250,WIDGET_TYPE='SliderSwiper',WIDGETS_WITH_ROWS=50 --spec 'cypress/integration/creation/dynamically_add_widgets_spec.js'"
部分如果 dynamically_add_widgets_spec.js
* Name of the page where you like to save this widgets
*/
const PAGE_TITLE = Cypress.env('PAGE_TITLE');
/**
* Maximum widgets in the page
*/
const MAX_WIDGETS_IN_PAGE = Cypress.env('MAX_WIDGETS_IN_PAGE');
/**
* Number of total widgets to be added to the page
*/
const TOTAL_WIDGETS_COUNT = Cypress.env('TOTAL_WIDGETS_COUNT');
/**
* Just change the type of the widget to select your needed widget
*/
const WIDGET_TYPE = Cypress.env('WIDGET_TYPE');
/**
* This variable is needed because we want to build some widgets with rows and some widgets don't need rows. The number below is how many widgets one need with rows.
*/
const WIDGETS_WITH_ROWS = Cypress.env('WIDGETS_WITH_ROWS');
/**
* This is a variable that is used to count needed widgets with rows. This variable should always stay 0.
*/
let ROWS_COUNTER = 0;
/**
* This constant is used to determine if all required fields are declared in the cli command
*/
const requiredFields = [
PAGE_TITLE,
MAX_WIDGETS_IN_PAGE,
TOTAL_WIDGETS_COUNT,
WIDGET_TYPE,
WIDGETS_WITH_ROWS
];
这是我调用它的地方
context("Dynamically create widgets", () => {
validateParameters(requiredFields);
createWidgets(WIDGET_TYPE, TOTAL_WIDGETS_COUNT, MAX_WIDGETS_IN_PAGE, WIDGETS_WITH_ROWS);
});
validateParameters 函数 -->
function validateParameters(requiredFields) {
requiredFields.forEach((field) => {
if (Cypress.env(field) !== undefined) {
return;
}
throw {message: `Field ${field} is required. Please provide it using --env ${field}. Please look at README.md for an example.`};
})
}
P.S。我知道这个 forEach 无法工作,因为如果缺少特定参数我需要特定消息,但我试图获取此消息,然后如果缺少特定参数则获取特定消息。
问题是在 validateParameters
函数中您传递了常量数组。每个常量都会有值,例如 PAGE_TITLE = Cypress.env('PAGE_TITLE')
;所以它将是 .env
中定义的任何内容。这可能是字符串、数字、未定义或其他内容。
要解决问题,您需要检查传递的字段是否未定义。
function validateParameters(requiredFields) {
requiredFields.forEach((field) => {
if (field !== undefined) {
return;
}
throw {message: `Field ${field} is required. Please provide it using --env ${field}. Please look at README.md for an example.`};
})
}
您已经使用 Cypress.env('...') 设置了上面的 requiredFields 变量,因此您不需要再次调用它。
赛普拉斯文档声明 Cypress.env('...') returns,变量或未定义。
/**
* Returns specific environment variable or undefined
* @see https://on.cypress.io/env
* @example
* // cypress.json
* { "env": { "foo": "bar" } }
* Cypress.env("foo") // => bar
*/
env(key: string): any
在此处查看有关 Cypress 环境的更多信息:https://docs.cypress.io/guides/guides/environment-variables.html#Overriding-Configuration