【问题标题】:Detox - How to check if an element is present without using expect排毒 - 如何在不使用期望的情况下检查元素是否存在
【发布时间】:2019-04-14 04:13:13
【问题描述】:

有没有办法在不使用 Detox 的情况下检查元素是否存在?现在,我不得不将我的逻辑嵌套在 try/catch 块中,以控制测试流程以减轻不稳定,因为它会在继续测试之前检查屏幕的状态。我宁愿能够使用 if/else。

提前感谢您的任何建议。

【问题讨论】:

    标签: detox


    【解决方案1】:

    不是最优雅的解决方案,但我使用以下方法来简化我的代码。

    在帮助文件中,我创建了包装 try/catch 并返回 true/false 的函数:

    例如:

    const expectToBeVisible = async (id) => {
      try {
        await expect(element(by.id(id))).toBeVisible();
        return true;
      } catch (e) {
        return false;
      }
    };
    
    module.exports = { expectToBeVisible };
    

    然后,当我执行依赖于该元素是否可见的测试时,我可以执行以下操作:

    import { expectToBeVisible } from './helpers';
    
    describe('Test', () => {
    
      ...
    
      it('If button is visible do X else do Y', async () => {
        let buttonVisible = await expectToBeVisible('button');
    
        if (buttonVisible) {
          // do something with that button
        } else {
          // do something else as the button isn't visible
        }
      });
    
     ...
    });
    

    这不是最好的解决方案,但在 Detox 提出具有 if/else 的能力之前,它可能就足够了。

    【讨论】:

    • 这会有所帮助。它肯定会清理我现在拥有的东西。对于我必须实现的所有嵌套的 try/catch 块,逻辑变得越来越难以跟踪。而且很难以一种不会使测试始终处于通过状态的方式来构建它。
    猜你喜欢
    • 1970-01-01
    • 2021-04-23
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多