如果未使用Cypress在CLI命令中声明参数之一,则会引发异常?

问题描述 投票:1回答:2

实际结果是:我只是跑了一下,即使等了半个小时也没反应。“实际结果”

预期结果是:如果缺少特定参数,则抛出特定异常。

我目前没有任何错误消息。例如,如果我尝试使用等于未定义的PAGE_TITLE来运行它,则不会发生任何事情。

已尝试的场景:我将所有位于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'" 

如果是dynamic_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不能正常工作,因为如果缺少特定的参数,我需要一条特定的消息,但是如果缺少特定的参数,我将尝试获取此消息,然后再获取特定的消息。

实际结果是:我只是跑步,即使等待了半个小时,也没有任何反应。预期的结果是:如果缺少特定参数,则抛出特定异常。我不...

javascript command-line-interface cypress package.json
2个回答
0
投票

问题是,在validateParameters函数中,您传递了常量数组。每个常数都有值,例如PAGE_TITLE = Cypress.env('PAGE_TITLE');因此它将是.env中定义的任何内容。可以是字符串,数字,未定义或其他内容。


0
投票

您已经在上面使用Cypress.env('...')设置了requiredFields变量,因此您无需再次调用它。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.