Detox - 如何在不使用 expect 的情况下检查元素是否存在

标签 detox

有没有一种方法可以在不使用 Detox 的情况下检查元素是否存在?现在,我必须将我的逻辑嵌套在 try/catch block 中以控制测试流程,以减轻在继续测试之前检查屏幕状态时的不稳定。我更愿意使用 if/else。

提前感谢您的任何建议。

最佳答案

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

在帮助文件中,我创建了包装 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 的能力之前,它在紧要关头就足够了。

关于Detox - 如何在不使用 expect 的情况下检查元素是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53245252/

相关文章:

react-native - 找不到预设 "module:react-native-dotenv"

reactjs - 构建失败 : exit code 127

android - Detox + React Native Android 启动到黑屏

react-native - 如何在排毒测试中返回屏幕

react-native - 如何在 Detox 中启用 `debug` 的情况下运行 RN 测试?

ios - 如何将 id 添加到 native ios 屏幕

react-native - 排毒构建因抛出 YogaKit.modulemap' 未找到 fatal error 而失败

react-native - 使用 React-Navigation 时排毒测试挂起

react-native - Bitrise + Detox + React-native - Hello World 示例卡在 detox.init 上

ios - 使用 detox 测试 e2e 无法启动 iPhone 模拟器,而是启动 Apple TV