【问题标题】:How to apply a conditional to an E2E Test in CodeceptJS and NightmareJS如何在 CodeceptJS 和 NightmareJS 中将条件应用于 E2E 测试
【发布时间】:2019-08-02 08:48:09
【问题描述】:

我需要什么:

在 CodeceptJS 中以 Nightmare 作为 Main Helper 进行 E2E 测试,验证元素是否存在,并根据结果继续执行一系列操作或其他操作。

示例代码:

class EventsHelper extends Helper {

  isExistsElement(selector) {
    let browser = this.helpers['Nightmare'].browser;
    return browser.evaluate((selector) => {
      return new Promise(resolve => {
        let element = document.querySelector(selector);
        resolve(element || false);
      });
    }, selector);
  }
}
module.exports = EventsHelper;

Scenario('Test 1', async (I) => {
  const isButtonRendered = await I.isExistsElement('#button');

  if (isButtonRendered) {
    I.see('Message that is displayed only if the button exists.');
    I.click('#button');
  } else {
    I.see('Alternative message that appears if this button does not exist.');
  }
});

此示例代码的当前结果是: - 如果按钮存在。

Evaluation timed out after 30000msec.  Are you calling done() or resolving your promises?
  • 否则按钮不存在: 通过。

我愿意接受建议、更正或不同的想法来解决这个问题。 谢谢你们! (如果我的英语不是很清楚,请原谅)。

【问题讨论】:

    标签: javascript node.js e2e-testing codeceptjs


    【解决方案1】:

    您必须为条件本身使用自定义帮助器,根据 codeceptjs 开发人员,他们不支持主场景函数中的条件。

    这是一个自定义帮助器示例:

    'use strict';
    import assert from 'assert';
    
    let Helper = codecept_helper;
    
    class MyHelper extends Helper {
      async clickIfVisible(selector, ...options) {
        const helper = this.helpers['Puppeteer'];
        try {
          const numVisible = await helper.grabNumberOfVisibleElements(selector);
    
          if (numVisible) {
            return helper.click(selector, ...options);
          }
        } catch (err) {
          console.log('Skipping operation as element is not visible');
        }
      }
    }
    
    module.exports = MyHelper;
    

    更多信息:https://github.com/Codeception/CodeceptJS/issues/648

    【讨论】:

      猜你喜欢
      • 2018-11-29
      • 2022-12-16
      • 1970-01-01
      • 2023-02-01
      • 2020-04-08
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      相关资源
      最近更新 更多